两个对象叠在一起时,谁画在上面由 z_index 决定,而不是由 add() 的先后顺序决定。示例用不同颜色的方块把这一层序直观展示出来,做遮挡、高亮、背景板时都必须用到。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
from manim import *
import numpy as np
class MobjectZOrder(Scene):
def construct(self):
circle = Circle().shift(LEFT)
square = Square().shift(UP)
triangle = Triangle().shift(RIGHT)
circle.set_stroke(color=GREEN, width=20)
square.set_fill(YELLOW, opacity=1.0)
triangle.set_fill(PINK, opacity=0.5)
self.play(*[FadeIn(_o) for _o in [triangle, square, circle]], run_time=1.0)
self.wait(1)
self.wait(3.60)

演示视频

下载 Python 脚本

要点

  • z_index 越大越靠前(越晚绘制),默认值都是 0
  • 可以在构造时传 z_index=,也可以事后用 set_z_index() 调整
  • 需要按 z_index 重排绘制顺序时调用 sort(),或把对象统一放进 VGroup

代码来源:Manim Community 官方 Manim’s building blocks 示例 MobjectZOrder,已在 Manim CE 0.20.1 实跑验证并生成上方视频。