gpt4 book ai didi

python - 在 manim 中异步运行不同的动画

转载 作者:行者123 更新时间:2023-12-04 17:28:46 28 4
gpt4 key购买 nike

我主要尝试运行两个动画(引用以下代码):

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 秒后(trainrun_time=18 开始此时仍会移动),我想要 train2 弹出屏幕并开始运动。我不确定这是如何完成的,希望得到任何帮助。

最佳答案

玩了一段时间后,我想通了如何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

关于python - 在 manim 中异步运行不同的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61578782/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com