2.4 配置

Manim有着非常广泛的配置系统,可以根据调整命令行参数(command-line interface, CLI)、更改ManimConfig类属性、创建配置文件。其中最为常用的就是调整命令行参数。

2.4.1 命令行参数

Manim中最为常用的命令就是render,也就是渲染命令。其一般的格式为manim render [OPTIONS] FILE [SCENES]。如果没有详细指名命令的话,则Manim默认使用render命令,也就是我们常见的manim [OPTIONS] FILE [SCENES]

在终端处输入manim --help后,就可以发现除了render外的其他命令及选项,通过manim [command]manim [command] --help查询详情。这里主要介绍render命令的相关设置。在终端处输入manim render --help即可查看渲染命令的一些参数设置,如下所示。

Manim Community v0.18.1

Usage: manim render [OPTIONS] FILE [SCENE_NAMES]...

  Render SCENE(S) from the input FILE.

  FILE is the file path of the script or a config file.

  SCENES is an optional list of scenes in the file.

Global options:
  -c, --config_file TEXT         Specify the configuration file to use for
                                 render settings.
  --custom_folders               Use the folders defined in the [custom_folders]
                                 section of the config file to define the output
                                 folder structure.
  --disable_caching              Disable the use of the cache (still generates
                                 cache files).
  --flush_cache                  Remove cached partial movie files.
  --tex_template TEXT            Specify a custom TeX template file.
  -v, --verbosity [DEBUG|INFO|WARNING|ERROR|CRITICAL]
                                 Verbosity of CLI output. Changes ffmpeg log
                                 level unless 5+.
  --notify_outdated_version / --silent
                                 Display warnings for outdated installation.
  --enable_gui                   Enable GUI interaction.
  --gui_location TEXT            Starting location for the GUI.
  --fullscreen                   Expand the window to its maximum possible size.
  --enable_wireframe             Enable wireframe debugging mode in opengl.
  --force_window                 Force window to open when using the opengl
                                 renderer, intended for debugging as it may
                                 impact performance
  --dry_run                      Renders animations without outputting image or
                                 video files and disables the window
  --no_latex_cleanup             Prevents deletion of .aux, .dvi, and .log files
                                 produced by Tex and MathTex.
  --preview_command TEXT         The command used to preview the output file
                                 (for example vlc for video files)

Output options:
  -o, --output_file TEXT         Specify the filename(s) of the rendered
                                 scene(s).
  -0, --zero_pad INTEGER RANGE   Zero padding for PNG file names.  [0<=x<=9]
  --write_to_movie               Write the video rendered with opengl to a file.
  --media_dir PATH               Path to store rendered videos and latex.
  --log_dir PATH                 Path to store render logs.
  --log_to_file                  Log terminal output to file.

Render Options:
  -n, --from_animation_number TEXT
                                 Start rendering from n_0 until n_1. If n_1 is
                                 left unspecified, renders all scenes after n_0.
  -a, --write_all                Render all scenes in the input file.
  --format [png|gif|mp4|webm|mov]
  -s, --save_last_frame          Render and save only the last frame of a scene
                                 as a PNG image.
  -q, --quality [l|m|h|p|k]      Render quality at the follow resolution
                                 framerates, respectively: 854x480 15FPS,
                                 1280x720 30FPS, 1920x1080 60FPS, 2560x1440
                                 60FPS, 3840x2160 60FPS
  -r, --resolution TEXT          Resolution in "W,H" for when 16:9 aspect ratio
                                 isn't possible.
  --fps, --frame_rate FLOAT      Render at this frame rate.
  --renderer [cairo|opengl]      Select a renderer for your Scene.
  -g, --save_pngs                Save each frame as png (Deprecated).
  -i, --save_as_gif              Save as a gif (Deprecated).
  --save_sections                Save section videos in addition to movie file.
  -t, --transparent              Render scenes with alpha channel.
  --use_projection_fill_shaders  Use shaders for OpenGLVMobject fill which are
                                 compatible with transformation matrices.
  --use_projection_stroke_shaders
                                 Use shaders for OpenGLVMobject stroke which are
                                 compatible with transformation matrices.

Ease of access options:
  --progress_bar [display|leave|none]
                                 Display progress bars and/or keep them
                                 displayed.
  -p, --preview                  Preview the Scene's animation. OpenGL does a
                                 live preview in a popup window. Cairo opens the
                                 rendered video file in the system default media
                                 player.
  -f, --show_in_file_browser     Show the output file in the file browser.
  --jupyter                      Using jupyter notebook magic.

Other options:
  --help                         Show this message and exit.

Made with <3 by Manim Community developers.

虽然选项挺多的,但实际考察后,对我来说估计只能用到其中一部分。

下面是一些例子。

manim -pql -o 样例 demo.py Example 输出的视频文件名称为“样例”

manim -pql -n 1,4 demo.py Example 只渲染第2个到第4个动画

manim -apql demo.py 输出demo.py文件中所有的类

manim -pql –format gif demo.py Example 输出格式为gif

manim -pqh –progress_bar display demo.py Example 视频质量高,并显示渲染进度条

manim -pql -r 1920,1080 –fps 54 demo.py Example 设置分辨率为1920x1080,帧率为54FPS

2.4.2 ManimConfig类

通过更改config对象的属性是确定Manim全局配置最直接的方式。config对象在ManimConfig类中,主要有两种方式(官方文档中建议第一种方式)来设置属性,如下所示。

from manim import *
  
config.background_color = WHITE
config["background_color"] = WHITE

...

config对象的所有属性如下所示。需要注意的是,ManimConfig具有内部一致性,更改了某项属性就会影响与其相关联的属性,例如更改y轴的半径frame_y_radius就会影响到画面的高度frame_height

平面直角坐标系的原点处于画面中心,向上是y轴正半轴,向右是x轴正半轴

['aspect_ratio', 'assets_dir', 'background_color', 'background_opacity',
'bottom', 'custom_folders', 'disable_caching', 'dry_run',
'ffmpeg_loglevel', 'flush_cache', 'frame_height', 'frame_rate',
'frame_size', 'frame_width', 'frame_x_radius', 'frame_y_radius',
'from_animation_number', 'fullscreen', 'images_dir', 'input_file', 'left_side',
'log_dir', 'log_to_file', 'max_files_cached', 'media_dir', 'media_width',
'movie_file_extension', 'notify_outdated_version', 'output_file', 'partial_movie_dir',
'pixel_height', 'pixel_width', 'plugins', 'preview',
'progress_bar', 'quality', 'right_side', 'save_as_gif', 'save_last_frame',
'save_pngs', 'scene_names', 'show_in_file_browser', 'sound', 'tex_dir',
'tex_template', 'tex_template_file', 'text_dir', 'top', 'transparent',
'upto_animation_number', 'use_opengl_renderer', 'verbosity', 'video_dir',
'window_position', 'window_monitor', 'window_size', 'write_all', 'write_to_movie',
'enable_wireframe', 'force_window']

2.4.3 配置文件

配置文件中你可以先创建一个.txt文件,在其中进行设置之后,再修改后缀名为.cfg即可创建配置文件,而且这个配置文件必须命名为manim.cfg。在配置文件中,必须把[CLI]放在第一行,之后再根据前一节的config对象的属性进行设置,设置方法如下所示。注意这里的属性必须为全称,部分属性不能向命令行参数那样简写!

[CLI]
background_color = WHITE

在这个例子中,我们将背景色改成了白色。之后需将该文件放置在与脚本文件的相同目录中。当你按常规方法(如manim -pql demo.py Example)输出视频时,Manim会自动找到.cfg文件进行配置并反映在视频当中。

上述这种放在与脚本文件相同目录中的配置文件叫做“folder-wide config file”,其作用范围就是在一个文件夹内。如果你有多个视频需要产出,而且他们恰好需要有共同的配置,此时你可以选择将该配置文件放置在特定的文件夹中,从而作用在多个视频中,此种配置文件即“user-wide config file”,也就是针对用户而言的。配置文件在不同系统中要放置在不同地方,具体如下所示。其中UserDirectory就是你的主文件夹。

我的UserDirectory就是在C盘里,需要在“查看”里勾选“隐藏的项目”才能找到下面的文件夹。

Windows: UserDirectory/AppData/Roaming/Manim/manim.cfg

MacOS: UserDirectory/.config/manim/manim.cfg

Linux: UserDirectory/.config/manim/manim.cfg

总体而言,调整命令行参数最为便捷,而后两种方法则有更多的选项可供选择。而且各个配置方式的优先级从低到高如下所示。所谓“优先级”就是如果各个配置方式中存在冲突的部分,则以优先级较高的方式为准。

  1. Library-wide config file

    默认的配置。

  2. user-wide config file, if it exists

    在主文件夹的配置文件

  3. folder-wide config file, if it exists OR custom config file, if passed via –config_file

    与脚本文件相同目录的配置文件

  4. other CLI flags

    其余命令行标志

  5. any programmatic changes made after the config system is set

    ManimConfig类