一个复杂 Mobject 往往由若干子对象组成。示例把对象拆开,展示 submobjects 以及 get_center()、get_width() 这类常用查询方法。理解「对象树」,才能解释为什么对 VGroup 的一次设置会作用到全体。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from manim import *
import numpy as np
class MobjectExample(Scene):
def construct(self):
p1 = [-1,-1, 0]
p2 = [ 1,-1, 0]
p3 = [ 1, 1, 0]
p4 = [-1, 1, 0]
a = Line(p1,p2).append_points(Line(p2,p3).points).append_points(Line(p3,p4).points)
point_start = a.get_start()
point_end = a.get_end()
point_center = a.get_center()
self.play(*[FadeIn(_o) for _o in [Text(f"a.get_start() = {np.round(point_start,2).tolist()}", font_size=24).to_edge(UR).set_color(YELLOW)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text(f"a.get_end() = {np.round(point_end,2).tolist()}", font_size=24).next_to(self.mobjects[-1],DOWN).set_color(RED)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Text(f"a.get_center() = {np.round(point_center,2).tolist()}", font_size=24).next_to(self.mobjects[-1],DOWN).set_color(BLUE)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Dot(a.get_start()).set_color(YELLOW).scale(2)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Dot(a.get_end()).set_color(RED).scale(2)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Dot(a.get_top()).set_color(GREEN_A).scale(2)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Dot(a.get_bottom()).set_color(GREEN_D).scale(2)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Dot(a.get_center()).set_color(BLUE).scale(2)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [Dot(a.point_from_proportion(0.5)).set_color(ORANGE).scale(2)]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [*[Dot(x) for x in a.points]]], run_time=1.0)
self.play(*[FadeIn(_o) for _o in [a]], run_time=1.0)
self.wait(1)

演示视频

下载 Python 脚本

要点

  • submobjects 是子对象列表,VGroup 本质上就是对它的封装
  • get_center() / get_width() 返回当前状态,常与 .animate 或 updater 搭配
  • become() 可让一个对象整体替换成另一个对象的当前形态

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