Mobject 的外观由填充(fill)和描边(stroke)两套独立属性决定。示例演示如何用 set_fill() / set_stroke() 改颜色、透明度和线宽。掌握这两组方法,就能不换对象、只换外观地复用同一个图形。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
from manim import *
import numpy as np
class MobjectStyling(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 [circle, square, triangle]], run_time=1.0)
self.wait(1)
self.wait(3.60)

演示视频

下载 Python 脚本

要点

  • set_fill(color, opacity=...) 控制内部,set_stroke(color, width=...) 控制轮廓
  • 颜色可用 Manim 内置常量(RED、BLUE)或十六进制字符串
  • opacity 取 0–1;设为 0 时填充不可见,但对象仍然占位

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