不必每次手写 from manim import * 和目录结构,manim init project 会生成一个能直接渲染的项目骨架。这段视频把它生成的两个文件、交互提问的顺序,以及一个容易踩的配置坑画了出来。

完整代码

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
from manim import *
class InitScaffoldDemo(Scene):
"""manim init project:一条命令生成可渲染的项目骨架"""
def construct(self):
title = Text("manim init project 生成项目骨架", font_size=32).to_edge(UP, buff=0.42)
self.play(Write(title))
cmd = Text("$ manim init project my_proj -d", font="Consolas",
font_size=26, color=GREEN)
cmd.next_to(title, DOWN, buff=0.42)
self.play(FadeIn(cmd, shift=DOWN * 0.2))
prompt = Text("Template (Axes, Default, MovingCamera) [Default]:",
font="Consolas", font_size=19, color=GREY_B)
prompt.next_to(cmd, DOWN, buff=0.32)
self.play(FadeIn(prompt, shift=DOWN * 0.15))
rows = [("my_proj/", 0, BLUE_B), ("|-- main.py", 1, WHITE),
("`-- manim.cfg", 1, YELLOW)]
tree = VGroup()
for name, depth, color in rows:
t = Text(name, font="Consolas", font_size=25, color=color)
t.shift(RIGHT * depth * 0.85)
tree.add(t)
tree.arrange(DOWN, buff=0.2, aligned_edge=LEFT).next_to(prompt, DOWN, buff=0.42)
self.play(LaggedStart(*[FadeIn(t, shift=RIGHT * 0.25) for t in tree],
lag_ratio=0.2))
self.wait(0.4)
tip = VGroup(
Text("main.py 自带一个示例场景,生成即可直接渲染", font_size=21),
Text("manim.cfg 的 [CLI] 段锁定本项目的默认画质与场景名", font_size=21),
Text("-d 写出的 pixel_height=1920 / pixel_width=1080 是竖屏,横屏需互换",
font_size=20, color=RED),
).arrange(DOWN, buff=0.15, aligned_edge=LEFT).next_to(tree, DOWN, buff=0.4)
if tip.width > 12.4:
tip.scale_to_fit_width(12.4)
self.play(LaggedStart(*[FadeIn(t, shift=UP * 0.15) for t in tip], lag_ratio=0.25))
self.wait(1.8)

演示视频

下载 Python 脚本

要点

  • manim init project 名字 一定会询问模板(Axes / Default / MovingCamera),-d 只跳过其余配置项,模板仍需回答;脚本化调用要提前把模板名喂给标准输入
  • 产物只有两个文件:main.py 带着一个可直接渲染的示例场景,manim.cfg 的 [CLI] 段锁定画质与默认场景名
  • -d 写出的 pixel_height=1920 / pixel_width=1080 是竖屏,做横屏视频要手动互换这两个值

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