该类由 凡人忆拾 编写,旨在让用户更加方便地对物件进行切片操作。 该类的实例支持 TransformMatchingStrings 动画。

该类为 MTexMarkupText 共同的抽象基类。如果想要在将来通过子字符串进行物件的切片,用户应当在创建实例时指定这些子字符串。可以通过 isolate 参数指定子串, 派生类的其它参数(包括 MTextex_to_color_map 字典键, MarkupTextt2c 字典键等)也可以指定子串,

该文件定义了 Selector 类型如下:

Selector = Union[
    tuple[Union[int, None], Union[int, None]],
        tuple[Union[int, None], Union[int, None]]


  • isolate 参数(Selector 类型);

  • 派生类的部分参数;

  • 派生类特定的文本内部指定模式。



对于指定了至少一个子串的实例,该类将额外生成一份包含颜色信息的SVG(每种颜色对应一个被指定的子串),并按照位置匹配两个 SVG 的各个物件, 使得原始 SVG 的每个物件对应上该颜色标签,从而完成子串与物件的对应。


class manimlib.mobject.svg.string_mobject.StringMobject(string: str, **kwargs)

An abstract base class for MTex and MarkupText

This class aims to optimize the logic of “slicing submobjects via substrings”. This could be much clearer and more user-friendly than slicing through numerical indices explicitly.

Users are expected to specify substrings in isolate parameter if they want to do anything with their corresponding submobjects. isolate parameter can be either a string, a re.Pattern object, or a 2-tuple containing integers or None, or a collection of the above. Note, substrings specified cannot partially overlap with each other.

Each instance of StringMobject generates 2 svg files. The additional one is generated with some color commands inserted, so that each submobject of the original SVGMobject will be labelled by the color of its paired submobject from the additional SVGMobject.

初始化样式,VMobject 的样式有以下

  • fill 填充样式

    • 颜色: fill_colorcolor

    • 不透明度: fill_opacity

  • stroke 线条样式

    • 颜色: stroke_colorcolor

    • 宽度: stroke_width

    • 不透明度: stroke_opacity

  • gloss 光泽

  • shadow 阴影

  • reflectiveness 反光度

其中, color, opacity, width 属性均可以为一个列表,在渲染时会按照列表对其进行补间

select_part(selector: Selector, index: int = 0) → VGroup

选择 select_parts(selector) 中的第 index 个物件组

select_parts(selector: Selector) → VGroup

选择各个 selector 完全覆盖的预指定子串对应的非空物件组构成的 VGroup

set_parts_color(selector: Selector, color: ManimColor)

selector 对应的物件染色

set_parts_color_by_dict(color_map: dict[Selector, ManimColor])