chamber-video

踩坑指南

Agent 生成 Manim 代码时必须避免的常见错误

1. MathTex 里不能写中文

错误
MathTex(r"\text{割线}", font_size=28)
正确
Text("割线", font_size=22, color=BLUE)

默认 LaTeX 引擎不支持 Unicode 中文字符,会报 LaTeX Error: Unicode character 割 (U+5272)

2. ValueTracker 闭包陷阱

错误
h = 1.5
secant = always_redraw(lambda: Line(
    axes.c2p(0, 0), axes.c2p(h, h),  # h 永远是 1.5
))
self.play(h.animate.set_value(0.01))  # AttributeError!
正确
h = ValueTracker(1.5)
secant = always_redraw(lambda: Line(
    axes.c2p(0, 0), axes.c2p(h.get_value(), h.get_value()),
))
self.play(h.animate.set_value(0.01))  # OK

3. get_riemann_rectangles 参数

错误
axes.get_riemann_rectangles(curve, x_range=[1, 4], n=10, colors=[BLUE_E, BLUE_C])
正确
dx = 3.0 / 10
axes.get_riemann_rectangles(curve, x_range=[1, 4], dx=dx, color=[BLUE_E, BLUE_C])

4. interpolate_color 参数类型

错误
interpolate_color(BLUE_D, "#8b5cf6", 0.5)  # 'str' has no '_internal_space'
正确
interpolate_color(BLUE_D, PURPLE_A, 0.5)  # 两个都是 ManimColor

5. Camera.frame 不存在

错误 — Community 版 Scene 没有 camera.frame
class MyScene(Scene):
    def construct(self):
        self.play(self.camera.frame.animate.scale(0.5))  # AttributeError!
正确 — 用 MovingCameraScene
class MyScene(MovingCameraScene):
    def construct(self):
        self.play(self.camera.frame.animate.scale(0.5))  # OK

6. stretch_to_height 不存在

错误
rect.animate.stretch_to_height(2)
正确
rect.animate.stretch_to_fit_height(2)

7. Code 类构造函数

错误
Code(code="print('hello')", language="python")
正确 — 用 Text + SurroundingRectangle 替代
code_lines = VGroup(
    Text("def f(x):", font_size=24, font="Monospace"),
    Text("    return x**2", font_size=24, font="Monospace"),
).arrange(DOWN, aligned_edge=LEFT)

8. Manim Community vs ManimGL 差异

功能Manim CommunityManimGL (3b1b)
安装pip install manimpip install manimgl
命令manim -ql file.py Classmanimgl file.py Class
3D 相机MovingCameraSceneself.camera 直接操作
Riemanndx= 参数n= 参数
颜色BLUE_D, PURE_BLUEBLUE, BLUE_D

本项目使用 Manim Community v0.20+

9. TinyTeX PATH

渲染含公式的场景必须设置 PATH,否则找不到 xelatexdvisvgm

export PATH="$HOME/Library/TinyTeX/bin/universal-darwin:$PATH"

10. always_redraw 性能

always_redraw 每帧都重新创建对象。对于复杂对象(如大量点的网格),可能导致渲染变慢。


下一步:物理动画 → 电场、波、力学模板