一个脚本里往往放着好几个 Scene,逐个指定场景名很麻烦。-a(--write_all)一次把文件里所有场景都渲出来。

完整代码

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
from manim import *
class WriteAllDemo(Scene):
"""-a / --write_all:一个文件里所有场景一起渲染"""
def construct(self):
title = Text("-a 一次渲染文件里所有场景", font_size=32).to_edge(UP, buff=0.4)
self.play(Write(title))
names = ["Intro", "Demo", "Outro"]
cards = VGroup()
for i, n in enumerate(names):
box = RoundedRectangle(corner_radius=0.1, width=3.05, height=1.5,
stroke_color=TEAL, stroke_width=2,
fill_color=TEAL, fill_opacity=0.1)
cls = Text("class " + n + "(Scene)", font="Consolas", font_size=16, color=TEAL)
inner = VGroup(cls).move_to(box.get_center())
cards.add(VGroup(box, inner))
cards.arrange(RIGHT, buff=0.4).next_to(title, DOWN, buff=0.7)
self.play(LaggedStart(*[FadeIn(c, shift=UP * 0.2) for c in cards], lag_ratio=0.2))
self.wait(0.4)
# 不用 -a:只出一个
solo = VGroup(
Text("不加 -a:只渲染你指定的那个场景", font_size=20, color=GREY_B),
Text("manim -ql f.py Demo -> Demo.mp4", font="Consolas",
font_size=18, color=GREY_B),
).arrange(DOWN, buff=0.1, aligned_edge=LEFT)
# 加 -a:三个都出
allthree = VGroup(
Text("加 -a:三个场景全部渲染", font_size=20, color=GREEN),
VGroup(Text("manim -ql f.py -a -> 3 ", font="Consolas",
font_size=18, color=GREEN),
Text("个 mp4", font_size=18, color=GREEN)).arrange(RIGHT, buff=0.04),
).arrange(DOWN, buff=0.1, aligned_edge=LEFT)
cmps = VGroup(solo, allthree).arrange(DOWN, buff=0.4, aligned_edge=LEFT)
cmps.next_to(cards, DOWN, buff=0.75)
self.play(FadeIn(solo, shift=RIGHT * 0.15))
self.wait(0.3)
self.play(FadeIn(allthree, shift=RIGHT * 0.15))
self.wait(0.3)
outs = VGroup(*[Text(n + ".mp4", font="Consolas", font_size=17, color=GREEN)
.next_to(cards[i], DOWN, buff=0.16) for i, n in enumerate(names)])
self.play(LaggedStart(*[FadeIn(o, shift=DOWN * 0.2) for o in outs], lag_ratio=0.25))
self.wait(0.4)
tail = Text("注意:-a 只按场景类名排序执行,不保证你想要的先后", font_size=19,
color=YELLOW)
tail.next_to(cmps, DOWN, buff=0.45)
self.play(FadeIn(tail, shift=UP * 0.12))
self.wait(1.6)

演示视频

下载 Python 脚本

要点

  • -a / --write_all 渲染文件内所有 Scene 子类,每个场景各出一个同名 mp4
  • 不加 -a 时必须给出场景名,只有那一个会被渲染
  • 多场景产物都进同一个目录,场景名建议加前缀,避免和同目录其它脚本的产物混在一起

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