首页 / 文档中心 / 入门

Manim 入门教程:Scene、Mobject、Animation 三大核心概念

发布于 2026-11-02 · 入门系列 · 约 4 分钟读完 · 中文正文 + English TL;DR
▶ 渲染效果实录(manim 渲染输出,代码见正文,可直接复现)
English TL;DR

Every Manim script follows the same three-layer model. A Scene is the canvas and timeline of one shot; its construct() method describes what happens. Mobjects (mathematical objects) are the actors: Circle, Square, Text, MathTex, Axes. Animations are the verbs: Create, Write, FadeIn, Transform, and the .animate syntax for property changes. Inside construct you either self.play() an animation, self.add() a static object, or self.wait() for a pause. Positioning is done with next_to, to_edge, shift and move_to. This tutorial walks through each concept with runnable examples and rendered previews, ending with a complete mini-scene combining text, shapes and a coordinate system.

一、Scene:一个类就是一段动画

Manim 的每个动画对应一个继承 Scene 的类,construct 方法里写"剧本":

from manim import *

class FirstScene(Scene):
    def construct(self):
        circle = Circle(color=BLUE)
        self.play(Create(circle))   # 播放"画出圆"的动画
        self.wait()                 # 停顿 1 秒

三条核心语句贯穿所有 Manim 代码:

  • self.play(...) — 播放一段动画,占一段时间轴
  • self.add(...) — 直接放置对象,不产生动画
  • self.wait(2) — 停顿 2 秒(留白也是动画语言)

二、Mobject:动画的"演员"

Mobject(Mathematical Object)是画面里一切东西的基类。常用的:

  • 图形:Circle() Square() Rectangle() Line() Arrow() Dot()
  • 文字:Text("中文") MathTex(r"\frac{1}{2}")
  • 组合:VGroup(a, b, c) 把多个对象绑成一组整体操作

摆放位置是最常用的操作:

square = Square(color=YELLOW)
circle = Circle(color=BLUE).next_to(square, RIGHT, buff=0.3)  # 在正方形右边
title  = Text("标题").to_edge(UP)                              # 顶到页面顶端
group  = VGroup(square, circle).move_to(ORIGIN)                # 整组居中

next_to(A, direction) 表示"贴着 A 的某个方向",buff 是间距;shift(UP * 2) 是相对平移;ORIGIN 是屏幕中心,UP/DOWN/LEFT/RIGHT 是方向常量。

三、Animation:play() 里放什么

动画分三类,掌握这些就够写 80% 的场景:

类型常用效果
进场Create / Write / FadeIn / GrowFromCenter描边画出 / 逐字写出 / 淡入 / 长出
变换Transform / ReplacementTransform / FadeTransform形状渐变成目标
属性obj.animate.scale(2).set_color(RED)链式属性动画
class TransformDemo(Scene):
    def construct(self):
        square = Square(color=YELLOW)
        self.play(Create(square))
        # .animate 语法: 改属性就出动画
        self.play(square.animate.scale(1.5).rotate(45 * DEGREES))
        # 变换语法: A 渐变成 B 的形状
        circle = Circle(color=BLUE).scale(1.5)
        self.play(Transform(square, circle))
        self.wait()

注意 Transform(square, circle) 之后画面上的对象仍然是 square(只是长得像 circle了),后续操作要以 square 为准。两者的语义区别详见《Transform 家族详解》。

四、节奏控制:run_time 与同时播放

play() 接受多个动画就是同时播放,配 run_time 控制时长:

self.play(Create(circle), Write(text), run_time=3)  # 3 秒内同时进行

需要错峰依次播放用 LaggedStart,需要严格串行用 Succession,进阶编排见《动画编排与节奏控制》。

五、串起来的完整小场景

class MiniLesson(Scene):
    def construct(self):
        title = Text("勾股定理", font="SimHei", font_size=40).to_edge(UP)
        formula = MathTex("a^2 + b^2 = c^2", font_size=60).shift(UP * 0.3)
        a = Square(2).set_stroke(BLUE)
        b = Square(2).set_stroke(GREEN).next_to(a, RIGHT, buff=0)
        g = VGroup(a, b).move_to(DOWN * 0.8)

        self.play(Write(title))
        self.play(Create(g))
        self.play(Write(formula))
        self.wait()

渲染:manim -pql tutorial.py MiniLesson。这段代码用到了本文全部概念:文字进场、VGroup 组合、位置摆放、同时播放。

下一步