动画大致分三类:把对象显示出来、改变对象状态、把对象移走。这段示例用一个方块串起 FadeIn → Rotate → FadeOut,是理解 self.play() 的最短路径。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
from manim import *
import numpy as np
class SomeAnimations(Scene):
def construct(self):
square = Square()
# some animations display mobjects, ...
self.play(FadeIn(square))
# ... some move or rotate mobjects around...
self.play(Rotate(square, PI/4))
# some animations remove mobjects from the screen
self.play(FadeOut(square))
self.wait(1)
self.wait(1.60)

演示视频

下载 Python 脚本

要点

  • FadeIn / FadeOut 会隐式把对象加入或移出场景,不需要额外 add / remove
  • Rotate(square, PI/4) 按弧度旋转,PI 由 Manim 直接提供
  • 每次 self.play() 是一段独立动画,默认 1 秒,用 run_time 调整

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