配置,Configuration
Manim提供了一个广泛的配置系统,允许它适应许多不同的用例。在场景渲染过程中,可以在不同时间配置许多配置选项。在通过命令行参数调用命令时,或者在第一次通过配置文件导入库时,可以通过ManimConfig类以编程方式配置每个选项。
配置Manim的最常见、最简单和推荐的方法是通过命令行界面(CLI),这将在下面直接介绍。
命令行参数,Command-line arguments
到目前为止,CLI中最常用的命令是render
命令,该命令用于将场景渲染到输出文件。它与以下参数一起使用:
Manim Community v0.16.0.post0
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
的场景类并以中等质量渲染(由-qm
标志指定)。
另一个常用的标志是-p
(“预览”),它使manim在渲染完成后打开渲染的视频。
笔记
-p
标志不会更改全局配置dict的任何属性。-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.16.0.post0
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 Create a new project or insert a new scene.
new (Deprecated) 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.16.0.post0
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
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
-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.
--use_opengl_renderer Render scenes using OpenGL (Deprecated).
-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.
-s, --save_last_frame Save last frame as png (Deprecated).
-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.16.0.post0
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.16.0.post0
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类,The ManimConfig class
配置Manim最直接的方法是通过全局配置对象,它是ManimConfig
的一个实例。此类的每个属性都是一个配置选项,可以使用标准属性语法或类似dict的语法进行访问:
>>> from manim import *
>>> config.background_color = WHITE
>>> config["background_color"] = WHITE
笔记
前者优先;后者用于向后兼容。
大多数类(包括Camera
、Mobject
和Animation
)从全局配置中读取一些默认配置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
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))
配置文件,The config files
如最后一个示例所示,从命令行执行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
与<file.py>
位于同一目录中,
manim <file.py> SceneName
提示
配置文件中允许的配置选项的名称与相应命令行标志的长名称完全相同。例如,-c
和--background_color
标志可以互换,但配置文件只接受backgrounds_color
作为允许的选项。
由于配置文件旨在替换CLI标志,因此可以通过配置文件设置所有CLI标志。此外,可以通过配置文件设置任何配置选项,无论其是否具有相关的CLI标志。有关所有CLI标志和配置选项的列表,请参阅本文档底部。
Manim将寻找Manim.cfg
配置文件与正在呈现的文件位于同一目录中,而不是在执行目录中。例如
manim -o myscene -i -c WHITE <path/to/file.py> SceneName
将使用路径/到/文件中path/to/file.py
的配置文件。py,如有。它不会使用在当前工作目录中找到的配置文件,即使它存在。这样,用户可以为不同的场景或项目保留不同的配置文件,并在系统中的任何位置使用正确的配置执行它们。
这里描述的文件称为文件夹范围的配置文件,因为它会影响在同一文件夹中找到的所有场景脚本。
用户配置文件,The user config file
如前一节所述,manim.cfg
配置文件仅影响其同一文件夹中的场景脚本。但是,用户也可以创建一个特殊的配置文件,该文件将应用于该用户渲染的所有场景。这称为用户范围的配置文件,无论从何处执行Manim,也无论场景脚本存储在何处,它都将应用。
用户范围的配置文件位于一个特殊文件夹中,具体取决于操作系统。
- Windows:
UserDirectory
/AppData/Roaming/Manim/Manim.cfg - MacOS:
UserDirectory
/.config/manim/manim.cfg - Linux:
UserDirectory
/.config/manim/marim.cfg
这里,UserDirectory
是用户的主文件夹。
笔记
一个用户可能有许多文件夹范围的配置文件,每个文件夹一个,但只有一个用户范围配置文件。同一台计算机中的不同用户可能都有自己的用户范围配置文件。
警告
不要将场景脚本存储在与用户范围的配置文件相同的文件夹中。在这种情况下,行为未定义。
无论何时从系统中的任何位置使用Manim,Manim都会查找用户范围的配置文件并读取其配置。
级联配置文件,Cascading config files
如果执行Manim,它同时找到文件夹范围的配置文件和用户范围的配置文档,会发生什么?Manim将读取这两个文件,但如果它们不兼容,则以文件夹范围的文件为准。
例如,以以下用户范围的配置文件为例
# user-wide
[CLI]
output_file = myscene
save_as_gif = True
background_color = WHITE
和以下文件夹范围的文件
# folder-wide
[CLI]
save_as_gif = False
然后,执行manim<file.py> SceneName
将等效于不使用任何配置文件并执行
manim -o myscene -c WHITE <file.py> SceneName
任何命令行标志都优先于任何配置文件。例如,使用前两个配置文件并执行manim-c RED <file.py> SceneName
相当于不使用任何配置文件并执行
manim -o myscene -c RED <file.py> SceneName
还有一个库范围的配置文件,用于确定Manim的默认行为,并应用于库的每个用户。它的优先级最低,因此用户范围和任何文件夹范围的文件中的任何配置选项都将覆盖库范围的文件。这称为级联配置文件系统。
警告
用户不应尝试修改库范围的文件。在修改之前,贡献者应该得到核心开发团队的明确确认。
操作顺序,Order of operations
未能对“XMLSerializer”执行“serializeToString”:参数1不是“Node”类型。
有这么多不同的配置Manim的方法,很难知道何时设置每个配置选项。事实上,这将取决于Manim的使用方式。
如果从模块导入Manim,则配置系统将遵循以下步骤:
1、已加载库范围的配置文件。
2、如果存在用户范围和文件夹范围的文件,则加载它们。
3、在前两个步骤中找到的所有文件都在单个ConfigParser
对象(称为parser
)中进行解析。这就是级联发生的地方。
4、登录中.记录器logging.Logger
被实例化以创建Manim的全局记录器对象。它使用解析器的“logger”部分进行配置,即解析器parser['logger]
。
5、ManimConfig
被实例化以创建全局配置config
对象。
6、步骤3中的解析器parser
通过ManimConfig.digest_parser()
输入步骤5中的配置config
。
7、logger
和config
都向用户公开。
如果从命令行调用Manim,则会执行前面的所有步骤,并由以下步骤补充:
8、CLI标志被解析并通过digest_args()
馈送到config
中。
9、如果使用了--config_file
标志,则会创建一个新的ConfigParser
对象,其中包含库范围的文件、用户范围的文件(如果存在)以及通过--config_file
传递的文件的内容。在这种情况下,将忽略文件夹范围的文件(如果存在)。
10、新的解析器被送入config
。
11、其余的CLI标志将被处理。
总之,配置选项的优先级顺序从最低到最高为:
1、库范围的配置文件,
2、用户范围的配置文件(如果存在),
3、文件夹范围的配置文件(如果存在)或自定义配置文件(通过–config_file
传递),
4、其他CLI标志,以及
5、设置配置系统后进行的任何编程更改。
所有配置选项的列表,A list of all config options
['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命令选项,Accessing CLI command options
输入manim
或manim --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
前一篇:
Thematic Guides
下一篇:
A deep dive into Manim’s internals