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)