同一个项目老是重复敲 -qh --fps 60,说明该用 manim.cfg 了。Manim 的配置是五层覆盖结构,写在越靠上的层优先级越高。这段代码先画出覆盖顺序,再展示真实的 manim.cfg 该怎么写。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from manim import *
class ManimCfgDemo(Scene):
"""manim.cfg:配置项的层级优先级"""
def construct(self):
title = Text("manim.cfg 的配置从哪来:五层覆盖", font_size=32).to_edge(UP, buff=0.4)
self.play(Write(title))
layers = [
("命令行参数 -qh --fps 60", BLUE, "最高"),
("项目目录 ./manim.cfg", TEAL, ""),
("用户目录 ~/manim.cfg", GREEN, ""),
("库默认 default.cfg", GREY_B, ""),
("代码内建默认值", GREY_D, "最低"),
]
stack = VGroup()
for text, color, tag in layers:
bar = Rectangle(width=7.4, height=0.66, fill_color=color,
fill_opacity=0.28, stroke_color=color, stroke_width=2)
label = Text(text, font_size=21).move_to(bar.get_center())
item = VGroup(bar, label)
if tag:
item.add(Text(tag, font_size=18, color=YELLOW)
.next_to(bar, RIGHT, buff=0.22))
stack.add(item)
stack.arrange(DOWN, buff=0.15).next_to(title, DOWN, buff=0.55)
self.play(LaggedStart(*[FadeIn(s, shift=RIGHT * 0.3) for s in stack], lag_ratio=0.16))
self.wait(0.4)
# 左侧竖向箭头 + 说明
arrow = Arrow(ORIGIN, DOWN * stack.height * 0.82, color=YELLOW,
stroke_width=4, buff=0)
arrow.next_to(stack, LEFT, buff=0.5)
arrow.set_y(stack.get_center()[1])
hint = Text("优先级\n由高到低", font_size=17, color=YELLOW, line_spacing=0.75)
hint.next_to(arrow, LEFT, buff=0.12)
self.play(GrowArrow(arrow), FadeIn(hint))
self.wait(1.2)
# 第二幕:真实 manim.cfg 片段
self.play(FadeOut(stack), FadeOut(arrow), FadeOut(hint), FadeOut(title))
cfg = VGroup(
Text("manim.cfg", font_size=26, color=YELLOW),
Text("[CLI]", font="Consolas", font_size=22, color=YELLOW),
Text("frame_rate = 60", font="Consolas", font_size=21, color=WHITE),
Text("pixel_width = 1920", font="Consolas", font_size=21, color=WHITE),
Text("pixel_height = 1080", font="Consolas", font_size=21, color=WHITE),
Text("background_color = #0B1E3A", font="Consolas", font_size=21, color=WHITE),
Text("scene_names = MyScene", font="Consolas", font_size=21, color=WHITE),
).arrange(DOWN, buff=0.16, aligned_edge=LEFT)
box = SurroundingRectangle(cfg, color=YELLOW, buff=0.3, stroke_width=2)
VGroup(box, cfg).move_to(ORIGIN)
self.play(Create(box), LaggedStart(*[FadeIn(c) for c in cfg], lag_ratio=0.12))
self.wait(1.6)

演示视频

下载 Python 脚本

要点

  • 优先级从高到低:命令行参数 > 项目 ./manim.cfg > 用户 ~/manim.cfg > 库 default.cfg > 代码内建默认值
  • manim.cfg 用 [CLI] 段,键名与命令行参数对应:frame_rate、pixel_width、pixel_height、background_color、scene_names
  • 只改一小段代码的行为用 with tempconfig({...}),离开代码块自动还原,不会污染全局配置

代码来源:Manim Community 官方文档 Guides / Configuration 与 CLI 参考 manim cfg(MIT),示例代码按 Manim CE 0.20.1 编写并实跑验证。