本文只做一件事:把 Manim Community 对各个 API 的官方介绍汇总到一处。每条固定三块——介绍功能、代码实操、视频演示,不写成教程,也不再在各篇「边学边做」里重复讲通用概念;文章末尾的内链负责把读者带回来。

本文不是一次写完的。每条 API 标注自己的整理日期,按站点既有时间线见缝插针陆续补齐,已收录 3 条。介绍部分取自 Manim CE 0.22.1 的官方 docstring 与官方示例源码(example_scenes/basic.py、mobject_update_utils 参考页),签名与字段值均为本机内省实测;代码与视频在同一版本实跑,视频规格 1920×1080 / 60fps。

一、add_updater

整理于 2026-10-05 · 官方来源:Mobject.add_updater docstring、官方 example_scenes/basic.py 的 UpdatersExample

1. 介绍功能

官方定义原文:

Add an update function to this mobject. Update functions, or updaters in short, are functions that are applied to the Mobject in every frame.

即:把一个函数登记进对象的更新队列,渲染的每一帧调用一次。它是 Mobject 的方法,不是字段;Mobject 上真正的字段只有两个:

字段 类型 初始值 官方说明
updaters list [] 已登记的更新函数,按登记顺序执行
updating_suspended bool False 为 True 时暂停执行更新函数

回调的第一个参数固定是对象自己;可以再带一个 dt,表示距上一帧的秒数,60fps 时 秒。官方参数说明:

参数 默认值 官方说明
update_function 必填 每次调用 update() 时以 self 为第一参数执行;若声明了第二参数 dt 则一并传入
index None 插入 self.updaters 的位置;None 表示追加到末尾
call_updater False 为 True 时立即以 dt=0 调用一次

返回值为 self,可链式调用。同一家族的 11 个方法,实测签名如下:

方法 签名 官方作用
add_updater (update_function, index=None, call_updater=False) 登记一个每帧调用的函数
remove_updater (update_function) 移除指定的更新函数
clear_updaters (recursive=True) 清空全部更新函数,默认递归到子对象
get_updaters () 返回自身的更新函数列表
get_time_based_updaters () 只返回带 dt 参数的那些
get_family_updaters () 返回自身及全部子对象的更新函数
has_time_based_updater () 判断是否含带 dt 的更新函数
match_updaters (mobject) 把另一个对象的更新函数复制过来
update (dt=0, recursive=True) 手动触发一次更新,默认递归
suspend_updating (recursive=True) 暂停更新
resume_updating (recursive=True) 恢复更新

官方 UpdatersExample 用的正是两个 lambda:一个 next_to 跟随位置,一个 set_value 跟随数值;self.play() 播放方块位移时,DecimalNumber 每帧跟着变。

2. 代码实操

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from manim import *
class AddUpdaterDemo(Scene):
def construct(self):
title = Text("add_updater:每帧调用", font="STLiti", font_size=36).to_edge(UP)
self.add(title)
square = Square(side_length=1.5).to_edge(LEFT).shift(DOWN)
label = DecimalNumber(num_decimal_places=2, font_size=36)
label.add_updater(lambda d: d.set_value(square.get_center()[1]))
label.add_updater(lambda d: d.next_to(square, RIGHT))
self.add(square, label)
self.play(square.animate.shift(UP * 2).set_fill(BLUE, opacity=0.6), run_time=2)
self.wait(0.8)
self.play(square.animate.to_edge(RIGHT), run_time=2)
self.wait(1)

3. 视频演示

下载 Python 脚本


二、always_redraw

整理于 2026-10-12 · 官方来源:mobject_update_utils.always_redraw docstring 与官方示例 TangentAnimation

1. 介绍功能

官方定义原文:

Redraw the mobject constructed by a function every frame. This function returns a mobject with an attached updater that continuously regenerates the mobject according to the specified function.

即:每帧按给定函数重新生成整个对象。它接受一个无参、返回 mobject 的函数 func,返回值就是这个带更新器的对象。官方实现等价于先执行 mob = func(),再登记 mob.add_updater(lambda _: mob.become(func())):每帧用 become() 把新构建的对象整体换进来。

同一模块 mobject_update_utils 提供的 8 个工具,实测签名与官方说明:

工具 签名 官方说明
always (method, *args, **kwargs) 每帧用固定参数调用某个绑定方法
always_redraw (func) 每帧按 func() 重建整个对象
always_rotate (mobject, rate=20 * DEGREES, **kwargs) 按每秒弧度持续旋转,内部用带 dt 的更新器
always_shift (mobject, direction=RIGHT, rate=0.1) 按每秒 Manim 单位持续平移
f_always (method, *arg_generators, **kwargs) always 的函数版:参数由无参函数每帧生成
cycle_animation (animation, **kwargs) 把动画做成循环更新器
turn_animation_into_updater (animation, cycle=False, delay=0, **kwargs) 把动画的插值函数挂成更新器
assert_is_mobject_method (method) 校验传入的是 mobject 的绑定方法

官方示例 TangentAnimation 用两个 always_redraw 分别重建正弦曲线上的动点与切线,配一个 ValueTracker 驱动;SpinningTriangle、ShiftingSquare、WelcomeToManim 分别演示 always_rotate、always_shift、turn_animation_into_updater。

2. 代码实操

1
2
3
4
5
6
7
8
9
10
11
12
13
from manim import *
class AlwaysRedrawDemo(Scene):
def construct(self):
title = Text("always_redraw:每帧重建", font="STLiti", font_size=34).to_edge(UP)
self.add(title)
ax = Axes(x_range=[0, 5, 1], y_range=[-2, 2, 1], x_length=9, y_length=3.5).shift(DOWN * 0.4)
sine = ax.plot(lambda x: np.sin(x), color=BLUE)
alpha = ValueTracker(0)
dot = always_redraw(lambda: Dot(ax.c2p(alpha.get_value(), np.sin(alpha.get_value())), color=YELLOW, radius=0.12))
line = always_redraw(lambda: DashedLine(ax.c2p(alpha.get_value(), np.sin(alpha.get_value())), ax.c2p(alpha.get_value(), -2), color=GREY))
self.add(ax, sine, dot, line)
self.play(alpha.animate.set_value(5), rate_func=linear, run_time=5)
self.wait(1)

3. 视频演示

下载 Python 脚本


三、ValueTracker

整理于 2026-10-19 · 官方来源:ValueTracker 类 docstring 与官方示例 ValueTrackerExample

1. 介绍功能

官方定义原文:

A mobject that can be used for tracking (real-valued) parameters. Useful for animating parameter changes. Not meant to be displayed. Instead the position encodes some number, often one which another animation or continual_animation uses for its update function, and by treating it as a mobject it can still be animated and manipulated just like anything else.

即:一个不用于显示的 mobject,用自身位置编码一个实数;因为仍是 mobject,可以被 animate 插值,于是其它对象的更新器就能逐帧读到连续变化的数值。实测签名:

对象 签名 官方说明
ValueTracker (value=0, **kwargs) 可被动画插值的数值源
DecimalNumber (number=0, num_decimal_places=2, include_sign=False, **kwargs) 显示小数,可用 set_value() 改值
Variable (var, label, var_type=DecimalNumber, num_decimal_places=2, **kwargs) 显示「标签 = 数值」,数值持续取自 ValueTracker

官方 ValueTrackerExample 的写法是先把 tracker 加入场景,再用 DecimalNumber 的 add_updater 读 get_value(),最后用 tracker.animate.set_value(...) 驱动整段动画;本站多条「边学边做」用的也是这条链路。

2. 代码实操

1
2
3
4
5
6
7
8
9
10
11
from manim import *
class ValueTrackerDemo(Scene):
def construct(self):
title = Text("ValueTracker:可插值的数值源", font="STLiti", font_size=32).to_edge(UP)
self.add(title)
tracker = ValueTracker(0)
num = always_redraw(lambda: DecimalNumber(tracker.get_value(), num_decimal_places=2, font_size=60).shift(UP * 0.5))
line = always_redraw(lambda: Line(LEFT * 4, LEFT * 4 + RIGHT * (0.01 + tracker.get_value()), color=YELLOW, stroke_width=10).shift(DOWN * 1.2))
self.add(num, line)
self.play(tracker.animate.set_value(5), rate_func=linear, run_time=5)
self.wait(1)

3. 视频演示

下载 Python 脚本


待补条目

本文按 API 逐条补齐,不集中一次写完。后续条目沿用相同三块结构与相同的视频、封面、脚本保存方式,日期继续在既有时间线里见缝插针。