You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Manim中同时播放不同速度的多个动画?

在Manim中实现多动画同播且速度不同的方法

你已经知道在Manim中可以用self.play(anim1, anim2, ...)同时播放多个动画,但需要让每个动画以不同速度运行,比如实现两个点周期性放大缩小的效果,现有代码无法达成目标。

原代码问题分析

你提供的代码中将所有Transform动画添加到VGroup后一次性播放,这会导致所有动画按顺序执行,而非每组的两个动画同时启动,且run_time参数无法正确作用于并行场景。另外,反复创建point.copy()再做Transform的方式也不够高效,且容易导致对象状态混乱。

解决方案:直接在self.play中指定不同时长的动画

Manim的self.play本身支持并行播放多个动画,只需为每个动画单独指定run_time参数,就能让它们同时启动、各自按设定时长运行。结合循环可实现周期性重复效果,以下是修正后的代码:

from manim import *

class TwoDots(Scene):
    def construct(self):
        point1 = Dot(point=[-3, 0, 0], radius=0.5)
        point2 = Dot(point=[3, 0, 0], radius=0.5)
        self.add(point1, point2)

        # 循环实现9次放大-缩小的周期动画
        for _ in range(9):
            # 同时播放放大动画:point1用时1秒,point2用时2秒
            self.play(
                point1.animate(run_time=1).scale(1.5),
                point2.animate(run_time=2).scale(1.5)
            )
            # 同时播放缩小动画:还原到初始半径0.5
            self.play(
                point1.animate(run_time=1).scale(2/3),
                point2.animate(run_time=2).scale(2/3)
            )

关键说明

  1. 并行动画实现:self.play中传入的多个动画会默认并行执行,每个动画通过run_time独立控制速度,时长越短速度越快。
  2. animate语法:使用.animate()语法可以更直观地给对象添加动画效果,避免手动创建Transform导致的状态问题。
  3. 周期性循环:通过for循环重复放大-缩小动作,实现你需要的周期性效果,循环次数可根据需求调整。

如果你需要更灵活的周期控制,也可以使用Loop动画封装重复的动作组:

from manim import *

class TwoDots(Scene):
    def construct(self):
        point1 = Dot(point=[-3, 0, 0], radius=0.5)
        point2 = Dot(point=[3, 0, 0], radius=0.5)
        self.add(point1, point2)

        # 定义单次放大缩小的动作组
        scale_cycle = AnimationGroup(
            point1.animate(run_time=1).scale(1.5),
            point2.animate(run_time=2).scale(1.5),
            point1.animate(run_time=1).scale(2/3),
            point2.animate(run_time=2).scale(2/3),
            lag_ratio=0  # 确保动作组内的动画按顺序执行
        )

        # 循环执行9次动作组
        self.play(Loop(scale_cycle, times=9))

内容的提问来源于stack exchange,提问作者Anky Physics

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 21:08:14