想让「改属性」这件事本身变成动画,不必自己写插值,直接在对象和中间方法之间插一个 .animate。示例让方块先变色,再同时上移并旋转,两段动画各停一秒。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
from manim import *
import numpy as np
class AnimateExample(Scene):
def construct(self):
square = Square().set_fill(RED, opacity=1.0)
self.play(*[FadeIn(_o) for _o in [square]], run_time=1.0)
# animate the change of color
self.play(square.animate.set_fill(WHITE))
self.wait(1)
# animate the change of position and the rotation at the same time
self.play(square.animate.shift(UP).rotate(PI / 3))
self.wait(1)
self.wait(0.60)

演示视频

下载 Python 脚本

要点

  • square.animate.set_fill(WHITE) 生成的是动画,而不是立即生效的赋值
  • shift(UP).rotate(PI/3) 链式调用会在同一段动画里一并完成
  • .animate 本质是对对象前后状态插值;复杂形变优先用 Transform / ReplacementTransform

代码来源:Manim Community 官方 Manim’s building blocks 示例 AnimateExample,已在 Manim CE 0.20.1 实跑验证并生成上方视频。