配置

Manim 提供了一个广泛的配置系统,使其能够适应许多不同的使用场景。在场景渲染过程中,有许多配置选项可以在不同的时间进行配置。每个选项可以通过ManimConfig 类以编程方式配置,在命令调用时通过命令行参数配置,或者在库首次导入时通过配置文件配置。

配置Manim最常见、最简单且推荐的方式是通过命令行界面(CLI),下面将直接描述。

命令行参数

到目前为止,CLI中最常用的命令是render命令, 它用于将场景渲染到输出文件中。 它使用以下参数:

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.
...

然而,由于Manim在没有指定命令时默认使用render命令,因此以下形式更为常见,可以替代使用:

manim [OPTIONS] FILE [SCENES]

使用上述表单的示例如下:

manim -qm file.py SceneOne

这要求Manim在文件file.py中搜索名为SceneOne的Scene类,并以中等质量渲染它(由-qm标志指定)。

另一个常用的标志是-p(“预览”),它使manim在渲染完成后打开渲染的视频。

注意

-p 标志不会更改全局 config 字典的任何属性。-p 标志仅作为命令行的一个便利功能。

高级示例

要以高质量渲染场景,但只输出场景的最后一帧而不是整个视频,你可以执行

manim -sqh <file.py> SceneName

以下示例指定了输出文件名(使用-o标志),仅渲染前十个动画(使用-n标志),背景为白色(使用-c标志),并将动画保存为.gif文件而不是.mp4文件(使用--format=gif标志)。它使用默认质量,并且在渲染后不会尝试打开文件。

manim -o myscene --format=gif -n 0,10 -c WHITE <file.py> SceneName

所有CLI标志的列表

$ manim --help
Manim Community v0.18.1

Usage: manim [OPTIONS] COMMAND [ARGS]...

  Animation engine for explanatory math videos.

Options:
  --version                      Show version and exit.
  --show-splash / --hide-splash  Print splash message with version information.
  --help                         Show this message and exit.

Commands:
  cfg          Manages Manim configuration files.
  checkhealth  This subcommand checks whether Manim is installed correctly...
  init         Create a new project or insert a new scene.
  plugins      Manages Manim plugins.
  render       Render SCENE(S) from the input FILE.

See 'manim <command>' to read about a specific subcommand.

Note: the subcommand 'manim render' is called if no other subcommand is
specified. Run 'manim render --help' if you would like to know what the '-ql' or
'-p' flags do, for example.

Made with <3 by Manim Community developers.
$ 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 cfg --help
Manim Community v0.18.1

Usage: manim cfg [OPTIONS] COMMAND [ARGS]...

  Manages Manim configuration files.

Options:
  --help  Show this message and exit.

Commands:
  export
  show
  write

Made with <3 by Manim Community developers.
$ manim plugins --help
Manim Community v0.18.1

Usage: manim plugins [OPTIONS]

  Manages Manim plugins.

Options:
  -l, --list  List available plugins.
  --help      Show this message and exit.

Made with <3 by Manim Community developers.

ManimConfig 类

配置Manim的最直接方式是通过全局的config对象, 它是ManimConfig的一个实例。该类的每个属性 都是一个配置选项,可以通过标准属性语法或类似字典的语法访问:

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

注意

前者是首选;后者是为了向后兼容而提供的。

大多数类,包括CameraMobjectAnimation,从全局config中读取一些默认配置。

>>> Camera({}).background_color
<Color white>
>>> config.background_color = RED  # 0xfc6255
>>> Camera({}).background_color
<Color #fc6255>

ManimConfig 旨在保持内部一致性。例如,设置 frame_y_radius 会影响 frame_height

>>> config.frame_height
8.0
>>> config.frame_y_radius = 5.0
>>> config.frame_height
10.0

全局的config对象旨在成为所有配置选项的唯一真实来源。所有其他设置配置选项的方式最终都会改变全局config对象的值。

以下示例说明了为文档中呈现的示例选择的视频分辨率,并附有参考帧。

示例:ShowScreenResolution

../_images/ShowScreenResolution-1.png
from manim import *

class ShowScreenResolution(Scene):
    def construct(self):
        pixel_height = config["pixel_height"]  #  1080 is default
        pixel_width = config["pixel_width"]  # 1920 is default
        frame_width = config["frame_width"]
        frame_height = config["frame_height"]
        self.add(Dot())
        d1 = Line(frame_width * LEFT / 2, frame_width * RIGHT / 2).to_edge(DOWN)
        self.add(d1)
        self.add(Text(str(pixel_width)).next_to(d1, UP))
        d2 = Line(frame_height * UP / 2, frame_height * DOWN / 2).to_edge(LEFT)
        self.add(d2)
        self.add(Text(str(pixel_height)).next_to(d2, RIGHT))
class ShowScreenResolution(Scene):
    def construct(self):
        pixel_height = config["pixel_height"]  #  1080 is default
        pixel_width = config["pixel_width"]  # 1920 is default
        frame_width = config["frame_width"]
        frame_height = config["frame_height"]
        self.add(Dot())
        d1 = Line(frame_width * LEFT / 2, frame_width * RIGHT / 2).to_edge(DOWN)
        self.add(d1)
        self.add(Text(str(pixel_width)).next_to(d1, UP))
        d2 = Line(frame_height * UP / 2, frame_height * DOWN / 2).to_edge(LEFT)
        self.add(d2)
        self.add(Text(str(pixel_height)).next_to(d2, RIGHT))

配置文件

如最后一个示例所示,从命令行执行Manim可能涉及同时使用多个标志。如果您必须在短时间内多次执行相同的脚本,例如在对场景脚本进行小的增量调整时,这可能会变得很麻烦。因此,Manim也可以使用配置文件进行配置。配置文件是以.cfg结尾的文件。

要在渲染场景时使用本地配置文件,您必须在场景代码所在的目录中创建一个名为manim.cfg的文件。

警告

配置文件必须命名为manim.cfg。目前,Manim不支持任何其他名称的配置文件。

配置文件必须以部分标题[CLI]开始。此标题下的配置选项与CLI标志具有相同的名称,并具有相同的用途。以下是一个示例配置文件。

[CLI]
# my config file
output_file = myscene
save_as_gif = True
background_color = WHITE

配置文件使用标准的Python库configparser进行解析。特别是,它们会忽略任何以井号#开头的行。

现在,执行以下命令

manim -o myscene -i -c WHITE <file.py> SceneName

相当于执行以下命令,前提是manim.cfg在同一目录下,

manim <file.py> SceneName

提示

配置文件中允许的配置选项名称与相应命令行标志的长名称完全相同。例如,-c--background_color标志可以互换使用,但配置文件只接受background_color作为允许的选项。

由于配置文件旨在替代命令行标志,所有命令行标志都可以通过配置文件设置。此外,任何配置选项都可以通过配置文件设置,无论它是否有相关的命令行标志。有关所有命令行标志和配置选项的列表,请参阅本文档的底部。

Manim 将在渲染文件所在的目录中查找 manim.cfg 配置文件,而不是在执行目录中。例如,

manim -o myscene -i -c WHITE <path/to/file.py> SceneName

将使用在path/to/file.py中找到的配置文件(如果有的话)。它不会使用当前工作目录中的配置文件,即使该文件存在。通过这种方式,用户可以为不同的场景或项目保留不同的配置文件,并从系统中的任何位置使用正确的配置执行它们。

这里描述的文件被称为文件夹范围的配置文件,因为它影响同一文件夹中的所有场景脚本。

用户配置文件

如前一节所述,manim.cfg 配置文件仅影响其所在文件夹中的场景脚本。然而,用户也可以创建一个特殊的配置文件,该文件将应用于该用户渲染的所有场景。这被称为用户全局配置文件,无论从何处执行 Manim,也无论场景脚本存储在何处,它都将适用。

用户范围的配置文件位于一个特殊文件夹中,具体取决于操作系统。

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

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

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

这里,UserDirectory 是用户的主文件夹。

注意

一个用户可能有许多文件夹范围的配置文件,每个文件夹一个, 但只有一个用户范围的配置文件。同一台电脑中的不同用户 可能各自拥有自己的用户范围配置文件。

警告

不要将场景脚本存储在用户范围的配置文件所在的同一文件夹中。在这种情况下,行为是未定义的。

无论你在系统中的任何地方使用Manim,Manim都会查找用户范围的配置文件并读取其配置。

级联配置文件

如果你执行Manim时,它发现了一个文件夹范围的配置文件和一个用户范围的配置文件会发生什么?Manim会读取这两个文件,但如果它们不兼容,文件夹范围的文件将优先

例如,以下是一个用户范围的配置文件

# user-wide
[CLI]
output_file = myscene
save_as_gif = True
background_color = WHITE

以及以下文件夹范围内的文件

# folder-wide
[CLI]
save_as_gif = False

然后,执行 manim SceneName 将等同于不使用任何配置文件并执行

manim -o myscene -c WHITE <file.py> SceneName

任何命令行标志都优先于任何配置文件。例如,使用前面的两个配置文件并执行 manim -c RED SceneName 等同于不使用任何配置文件并执行

manim -o myscene -c RED <file.py> SceneName

还有一个库范围的配置文件,用于确定Manim的默认行为,并适用于库的每个用户。它的优先级最低,因此用户范围和任何文件夹范围文件中的任何配置选项都将覆盖库范围文件。这被称为级联配置文件系统。

警告

用户不应尝试修改库范围内的文件。 贡献者在修改之前应获得核心开发团队的明确确认。

操作顺序

由于配置Manim的方式多种多样,很难知道每个配置选项何时被设置。实际上,这将取决于Manim的使用方式。

如果从模块中导入Manim,那么配置系统将按照以下步骤进行:

  1. 库范围的配置文件已加载。

  2. 如果存在用户范围和文件夹范围的文件,则会加载它们。

  3. 在前两步中找到的所有文件都在一个名为 parserConfigParser 对象中进行解析。这是级联发生的地方。

  4. logging.Logger 被实例化以创建 Manim 的全局 logger 对象。它使用解析器的“logger”部分进行配置,即 parser['logger']

  5. ManimConfig 被实例化以创建全局的 config 对象。

  6. 步骤3中的parser通过ManimConfig.digest_parser()被输入到步骤5中的config

  7. 用户可以使用loggerconfig

如果从命令行调用Manim,所有前面的步骤都会发生,并且会补充以下内容:

  1. CLI标志被解析并通过digest_args()输入到config中。

  2. 如果使用了--config_file标志,则会创建一个新的ConfigParser对象,该对象包含库范围文件的内容、用户范围文件(如果存在)以及通过--config_file传递的文件。在这种情况下,文件夹范围的文件(如果存在)将被忽略。

  3. 新的解析器被输入到config中。

  4. 其余的CLI标志被处理。

总结一下,配置选项的优先级顺序,从最低到最高优先级是:

  1. 库范围的配置文件,

  2. 用户范围的配置文件,如果存在,

  3. 文件夹范围的配置文件,如果存在,或者自定义配置文件,如果通过 --config_file传递,

  4. 其他CLI标志,以及

  5. 在配置系统设置后所做的任何编程更改。

所有配置选项的列表

['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']

访问CLI命令选项

输入 manimmanim --help,将会打开主帮助页面。

Usage: manim [OPTIONS] COMMAND [ARGS]...

  Animation engine for explanatory math videos.

Options:
  --version  Show version and exit.
  --help     Show this message and exit.

Commands:
  cfg      Manages Manim configuration files.
  init     Sets up a new project in current working directory with default
           settings.

           It copies files from templates directory and pastes them in the
           current working dir.
  new      Create a new project or insert a new scene.
  plugins  Manages Manim plugins.
  render   Render SCENE(S) from the input FILE.

See 'manim <command>' to read about a specific subcommand.

Made with <3 by Manim Community developers.

每个子命令都有自己的帮助页面,可以类似地访问:

manim render
manim render --help