Manim 分析

manim

https://github.com/3b1b/manim

Installation 安装

Manim runs on Python 3.7. You can install it from PyPI via pip:

1
pip3 install manimlib

System requirements are cairo, ffmpeg, sox, latex (optional, if you want to use LaTeX).

You can now use it via the manim command. For example:

1
manim my_project.py MyScene

Manim

manim 库的大部分功能都分解为功能不同的文件夹。用于编写动画脚本的主要工具是 scene, mobject, camera, 和 animation 文件夹,其中包含它们各自的类。在这四个文件夹中,mobject 文件夹是用户大多数时间与之交互的文件夹。它包含动画中使用的所有几何图形,文本和图形的类。

程序的实际结构很容易掌握。通过在 python 文件中声明 Scene 类的子类来创建动画或场景。动画的实际代码进入一种称为 “construct 构造” 的方法。这是manim场景的关键字。每当 Scene 类中的对象被初始化时,它都会调用 self.setup()self.construct() 方法。如果未实现后者,则会出现错误。但是,前者是可选的,但在同时处理多个场景时很有用。

播放和保存动画是从命令行进行的。上面显示了播放动画的基本命令,并将python文件的名称和场景子类传递给 manim.py(实际上是 manimlib/__init__.py)。-pm 标志将预览中等(720p)品质的动画并将其保存在媒体文件夹中。在 config.py 中可以找到更多标志类型,例如裁剪某些帧的标志或导出 .gif 动画的标志。

当命令行调用其文件时,也会在场景声明之外执行任何 python 代码。这意味着可以删除不明确依赖 manim 功能的代码 construct(self)。这很有用,因为它可以防止结构定义混乱。

manim 中的大多数类都带有 CONFIG 与之关联的字典。它始终出现在类定义的顶部。这是 Camera 类的 CONFIG:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Camera(object):

CONFIG = {
"background_image": None,
"pixel_height": DEFAULT_PIXEL_HEIGHT,
"pixel_width": DEFAULT_PIXEL_WIDTH,
"frame_rate": DEFAULT_FRAME_RATE,
"frame_height": FRAME_HEIGHT,
"frame_width": FRAME_WIDTH,
"frame_center": ORIGIN,
"background_color": BLACK,
"background_opacity": 1,
"max_allowable_norm": FRAME_WIDTH,
"image_mode": "RGBA",
"n_channels": 4,
"pixel_array_dtype": 'uint8',
"z_buff_func": lambda m: np.round(m.get_center()[2], 2),
"cairo_line_width_multiple": 0.01,
}

初始化对象时,CONFIG 的条目用作要通过 __init__ 传递的关键字参数。这对于创建场景非常方便,因为在命令行中将几个关键字参数传递给感兴趣的场景很麻烦。通常,这也是用户与 Camera 类进行交互的地方,因为 camera CONFIG是场景CONFIG中的一个条目。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Scene(Container):

CONFIG = {
"camera_class": Camera,
"camera_config": {},
"file_writer_config": {},
"skip_animations": False,
"always_update_mobjects": False,
"random_seed": 0,
"start_at_animation_number": None,
"end_at_animation_number": None,
"leave_progress_bars": False,
}

所有大写变量都来自文件 constants.py,该文件可用作参考,因为许多类和方法都将这些常量用作默认参数。CONFIG 字典尊重类和子类之间的继承,因此 manim 中的许多类具有比其类定义中更大的 CONFIG 参数。这对于大量使用子类化的 mobjects 特别重要。例如,这是的子类结构 mobject/geometry.py

manim

AnimationsWithManim1

Manim

\[ Manim = Python3(核心)+ latex(文字排版) + cairo(生成图形)+ ffmpeg(转码视频)+ sox(音频处理) \]

优点
大纲

具体安装可参考:Installation on MacOS

MacTeX2

MacTeX

下载 MacTeX.pkg 安装

Homebrew3

1
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Python34

可以直接去官网官下载最新版的 Python3

或者用 brew 安装

1
brew install python3

python3

1
2
3
4
Python 3.7.4 (default, Sep 28 2019, 16:39:19) 
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

下载cairo,ffmpeg,sox,latex和其他的包

返回终端的根目录(如果你之前进入了python3,输入exit() 退出,或者重新打开终端),终端输入:

1
2
3
brew install cairo
brew install ffmpeg
brew install sox

Manim5

Manim is an animation engine for explanatory math videos. It's used to create precise animations programmatically, as seen in the videos at 3Blue1Brown.

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

Install list requirements.txt

1
2
3
python3 -m pip install -r requirements.txt
python3 -m pip install pyreadline
python3 -m pip install pydub

Run Manim

With the terminal in manim-master directory run this:

1
2
python3 -m manim example_scenes.py WriteStuff -pl
python3 -m manim example_scenes.py SquareToCircle -pl

The -p flag in the command above is for previewing, meaning the video file will automatically open when it is done rendering.

The -l flag is for a faster rendering at a lower quality.

Some other useful flags include:

1
2
3
-s to skip to the end and just show the final frame.
-n <number> to skip ahead to the n'th animation of a scene.
-f to show the file in finder (for OSX).

Anaconda Install

Install sox and latex as above.
Create a conda environment using

1
conda env create -f environment.yml

Using virtualenv and virtualenvwrapper

After installing virtualenv and virtualenvwrapper

1
2
3
4
pip3 install virtualenvwrapper
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
source /usr/local/bin/
virtualenvwrapper.sh

mk_manim

1
2
3
mkvirtualenv -r requirements.txt mk_manim
(mk_manim) $ python -m pip install pycairo
(mk_manim) $ python3 -m manim example_scenes.py SquareToCircle -pl

Error

Manim ModuleNotFoundError: No module named 'cairo'6

1
python -m pip install pycairo

参考


  1. AnimationsWithManim: https://github.com/Elteoremadebeethoven/AnimationsWithManim↩︎

  2. MacTex: https://www.tug.org/mactex/mactex-download.html↩︎

  3. Homebrew: https://brew.sh↩︎

  4. Python: https://www.python.org/downloads/↩︎

  5. Manim: https://github.com/3b1b/manim↩︎

  6. https://github.com/3b1b/manim/issues/392↩︎