【问题标题】:Asynchronously run different animations in manim在 manim 中异步运行不同的动画
【发布时间】:2022-04-25 04:24:18
【问题描述】:

我正在尝试运行两个动画(参考以下代码):

class RelTrain(Scene):
    def construct(self):
        train = Rectangle(height=1, width=4)
        train2 = Rectangle(height=2, width=2)
        train.move_to(np.array([-10,0,0]))
        train2.move_to(np.array([0,0,0]))
        self.add(train, train2)
        self.play(
            train.move_to, np.array([10,0,0]),
            train2.move_to, np.array([15,0,0]),
            run_time=18,
            rate_func=linear,
        )
        self.wait()

基本上两个矩形在移动,但我不希望它们同时开始移动。我希望train 开始移动,并且在 2 秒后(train 仍然会在这一点上移动,因为run_time=18),我希望train2 在屏幕上弹出并开始它的运动。我不确定这是如何完成的,如果有任何帮助,我将不胜感激。

【问题讨论】:

    标签: python animation manim


    【解决方案1】:

    玩了一段时间后,我想出了如何使用ManimCE (v0.3.0) 做到这一点。这还没有很好的记录,但基本上你可以使用mobject updaters。我不确定这是否是最好的方法(在我看来这太冗长和低级),但它有效:

    代码

    
    import numpy as np
    from manim import *
    
    class DeplayedTrains(Scene):
        def construct(self):
            # create both trains
            trains = (
                Rectangle(height=1, width=4),
                Rectangle(height=2, width=2),
            )
            
            # indicate start and end points
            start_points_X, end_points_X = ((-5, 0), (5, 5))
            # compute movement distances for both trains
            distances = (
                (end_points_X[0] - start_points_X[0]),
                (end_points_X[1] - start_points_X[1]),
            )
            
            # place trains at start points and add to the scene
            for train, start_point in zip(trains, start_points_X):
                train.move_to(np.array([start_point, 0, 0]))
                self.add(train)
            
            # deifine durations of movements for both trains, get FPS from config
            durations, fps = ((5, 3), config["frame_rate"])
            
            # create updaters
            updaters = (
                # add to the current position in X the difference for each frame, 
                # given the distance and duration defined
                lambda mobj, dt: mobj.set_x(mobj.get_x() + (distances[0] / fps / durations[0])),
                lambda mobj, dt: mobj.set_x(mobj.get_x() + (distances[1] / fps / durations[1])),
            )
            
            # add updaters to trains objects, movement begins
            trains[0].add_updater(updaters[0])
            # wait 2 seconds
            self.wait(2)
            
            # start the movement of the second train and wait 3 seconds
            trains[1].add_updater(updaters[1])
            self.wait(3)
            
            # remove the updaters
            trains[0].clear_updaters()  # you can also call trains[0].remove_updater(updaters[0])
            trains[1].clear_updaters()
    

    Output

    【讨论】:

      【解决方案2】:

      对于那些希望制作动画在不同时间开始但仍部分时间重叠的人,请查看LaggedStart 和类似的东西:

      https://docs.manim.community/en/stable/reference/manim.animation.composition.LaggedStart.html#manim.animation.composition.LaggedStart

      https://docs.manim.community/en/stable/reference/manim.animation.composition.html

      不幸的是,它们没有记录在案,但我花了一段时间才意识到它们甚至存在。

      【讨论】:

        猜你喜欢
        • 2021-03-11
        • 2020-07-03
        • 1970-01-01
        • 1970-01-01
        • 2021-09-23
        • 1970-01-01
        • 1970-01-01
        • 2015-12-10
        • 1970-01-01
        相关资源
        最近更新 更多