长场景渲到一半想只改结尾,从头再渲一遍太浪费。-n a,b 只把第 a 到第 b 段动画写进视频,前面的段落不再占用编码时间。

完整代码

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
from manim import *
class FromAnimationNumberDemo(Scene):
"""-n a,b:只渲染第 a 到第 b 段动画"""
def construct(self):
title = Text("-n 只渲染指定区间的动画段", font_size=31).to_edge(UP, buff=0.4)
self.play(Write(title))
blocks = VGroup()
for i in range(1, 6):
box = RoundedRectangle(corner_radius=0.08, width=1.55, height=0.95,
stroke_color=BLUE, stroke_width=2,
fill_color=BLUE, fill_opacity=0.18)
num = Text(str(i), font="Consolas", font_size=26, color=WHITE)
blocks.add(VGroup(box, num.move_to(box.get_center())))
blocks.arrange(RIGHT, buff=0.3).next_to(title, DOWN, buff=0.85)
idx = VGroup(*[Text("Animation %d" % i, font="Consolas", font_size=15,
color=GREY_B).next_to(blocks[i - 1], DOWN, buff=0.28)
for i in range(1, 6)])
self.play(LaggedStart(*[FadeIn(b, shift=UP * 0.2) for b in blocks], lag_ratio=0.12))
self.play(LaggedStart(*[FadeIn(i) for i in idx], lag_ratio=0.1))
self.wait(0.4)
# 全部渲染
full = Text("不加 -n:5 段全渲,成片 20s", font_size=22, color=GREY_B)
full.next_to(idx, DOWN, buff=0.62)
self.play(FadeIn(full, shift=UP * 0.15))
self.wait(0.4)
# -n 3,5
skip = blocks[:2]
self.play(*[s[0].animate.set_stroke(GREY_D).set_fill(GREY_D, opacity=0.12)
for s in skip],
*[s[1].animate.set_color(GREY_D) for s in skip], run_time=0.8)
keep_box = SurroundingRectangle(blocks[2:], color=GREEN, buff=0.14, stroke_width=3)
self.play(Create(keep_box))
res = Text("-n 3,5:只渲第 3-5 段,成片 4s", font_size=22, color=GREEN)
res.next_to(full, DOWN, buff=0.3)
self.play(FadeIn(res, shift=UP * 0.15))
self.wait(0.4)
tip = VGroup(
Text("被跳过的段仍会执行一遍,只是不写视频,用于对齐状态", font_size=19,
color=WHITE),
Text("所以从中间续渲,前序动画不能改,否则后续状态会错位", font_size=19,
color=ORANGE),
).arrange(DOWN, buff=0.14, aligned_edge=LEFT).next_to(res, DOWN, buff=0.45)
self.play(LaggedStart(*[FadeIn(t, shift=RIGHT * 0.15) for t in tip], lag_ratio=0.3))
self.wait(1.8)

演示视频

下载 Python 脚本

要点

  • -n 3,5 只保留第 3 到第 5 段动画,实测同一段代码由 20 秒缩到 4 秒
  • 被跳过的动画仍会执行一遍以对齐画面状态,所以前序代码不能改,否则后续画面会错位
  • 只写起点 -n 3 表示从第 3 段渲到结尾,只改片尾时最省事

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