quote,

Manim is an engine for precise programatic animations, designed for creating explanatory math videos.

clone仓库

1
git clone https://github.com/3b1b/manim.git

运行demo

1
2
cd manim
manimgl example_scenes.py OpeningManimExample

编写一个

  • 在manim目录下新建python文件demo.py,代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    from manimlib.imports import *

    class SquareToCircle(Scene):
    def construct(self):
    circle = Circle()
    circle.set_fill(BLUE, opacity=0.5)
    circle.set_stroke(BLUE_A, width=4)

    square = Square()

    self.play(ShowCreation(square))
    self.play(ReplacementTransform(square, circle))
    self.wait()
    exit()
  • 运行程序,执行manim demo.py SquareToCircle,就会展示如下动画

    • 执行manim demo.py SquareToCircle -wm可以在videos目录下生成视屏文件

代码解析

  • 定义场景类:

    • 需要继承自场景类Scene
    • override函数:construct
    1
    2
    class SquareToCircle(Scene):
    def construct(self):
  • 场景元素添加并设置:

    1
    2
    3
    4
    circle = Circle()
    circle.set_fill(BLUE, opacity=0.5)
    circle.set_stroke(BLUE_A, width=4)
    square = Square()
  • 动画定义及播放:

    1
    2
    self.play(ShowCreation(square))
    self.play(ReplacementTransform(square, circle))

    以上。