API 参考:SVG 与位图(svg)

共 7 个类,按字母排序。每个类含中文说明、继承链、参数表与上手示例,API 文档由 autodoc 从 manim v0.21.0 源码自动生成;带完整中文精讲的类见 API 索引。

ArcBrace

贴合圆弧的大括号标注:沿曲线中点展开的弧形括号。

继承关系

digraph G { graph [rankdir=LR, bgcolor="transparent", splines=spline, concentrate=true, nodesep="0.15", ranksep="0.3"]; node [shape=box, penwidth=0, width=0.05, height=0.05, margin=0.05]; edge [penwidth=1]; "Mobject" -> "VMobject"; "VMobject" -> "VMobjectFromSVGPath"; "VMobjectFromSVGPath" -> "Brace"; "Brace" -> "ArcBrace"; }

参数

arc

None,Arc | None

direction

array([1., 0., 0.]),Vector3DLike

API 文档

class manim.ArcBrace(arc: Arc | None = None, direction: Vector3DLike = array([1., 0., 0.]), **kwargs: Any)

基类:Brace

Creates a Brace that wraps around an Arc.

The direction parameter allows the brace to be applied from outside or inside the arc.

警告

The ArcBrace is smaller for arcs with smaller radii.

备注

The ArcBrace is initially a vertical Brace defined by the length of the Arc, but is scaled down to match the start and end angles. An exponential function is then applied after it is shifted based on the radius of the arc.

The scaling effect is not applied for arcs with radii smaller than 1 to prevent over-scaling.

Parameters

arc

The Arc that wraps around the Brace mobject.

direction

The direction from which the brace faces the arc. LEFT for inside the arc, and RIGHT for the outside.

Example

class ArcBraceExample(Scene):
    def construct(self):
        arc_1 = Arc(radius=1.5,start_angle=0,angle=2*PI/3).set_color(RED)
        brace_1 = ArcBrace(arc_1,LEFT)
        group_1 = VGroup(arc_1,brace_1)

        arc_2 = Arc(radius=3,start_angle=0,angle=5*PI/6).set_color(YELLOW)
        brace_2 = ArcBrace(arc_2)
        group_2 = VGroup(arc_2,brace_2)

        arc_3 = Arc(radius=0.5,start_angle=-0,angle=PI).set_color(BLUE)
        brace_3 = ArcBrace(arc_3)
        group_3 = VGroup(arc_3,brace_3)

        arc_4 = Arc(radius=0.2,start_angle=0,angle=3*PI/2).set_color(GREEN)
        brace_4 = ArcBrace(arc_4)
        group_4 = VGroup(arc_4,brace_4)

        arc_group = VGroup(group_1, group_2, group_3, group_4).arrange_in_grid(buff=1.5)
        self.add(arc_group.center())

Brace

贴合图形边缘的大括号:自动缩放适应目标宽度,配合标签写标注公式。

继承关系

digraph G { graph [rankdir=LR, bgcolor="transparent", splines=spline, concentrate=true, nodesep="0.15", ranksep="0.3"]; node [shape=box, penwidth=0, width=0.05, height=0.05, margin=0.05]; edge [penwidth=1]; "Mobject" -> "VMobject"; "VMobject" -> "VMobjectFromSVGPath"; "VMobjectFromSVGPath" -> "Brace"; }

参数

mobject

—,Mobject

direction

array([ 0., -1., 0.]),Vector3DLike

buff

0.2,float

sharpness

2,float

stroke_width

0,float

fill_opacity

1.0,float

background_stroke_width

0,float

background_stroke_color

ManimColor('#000000'),ParsableManimColor

快速上手

brace = Brace(group, DOWN, label="sum")

API 文档

class manim.Brace(mobject: Mobject, direction: Vector3DLike = array([0., -1., 0.]), buff: float = 0.2, sharpness: float = 2, stroke_width: float = 0, fill_opacity: float = 1.0, background_stroke_width: float = 0, background_stroke_color: ParsableManimColor = ManimColor('#000000'), **kwargs: Any)

基类:VMobjectFromSVGPath

Takes a mobject and draws a brace adjacent to it.

Passing a direction vector determines the direction from which the brace is drawn. By default it is drawn from below.

Parameters

mobject

The mobject adjacent to which the brace is placed.

direction :

The direction from which the brace faces the mobject.

See Also

BraceBetweenPoints

Examples

class BraceExample(Scene):
    def construct(self):
        s = Square()
        self.add(s)
        for i in np.linspace(0.1,1.0,4):
            br = Brace(s, sharpness=i)
            t = Text(f"sharpness= {i}").next_to(br, RIGHT)
            self.add(t)
            self.add(br)
        VGroup(*self.mobjects).arrange(DOWN, buff=0.2)
get_direction() → Vector3D

Returns the direction from the center to the brace tip.

get_tex(*tex: str, **kwargs: Any) → MathTex

Places the tex at the brace tip.

Parameters
tex

The tex to be placed at the brace tip.

kwargs

Any further keyword arguments are passed to put_at_tip() which is used to position the tex at the brace tip.

Returns

MathTex

get_text(*text: str, **kwargs: Any) → Tex

Places the text at the brace tip.

Parameters
text

The text to be placed at the brace tip.

kwargs

Any additional keyword arguments are passed to put_at_tip() which is used to position the text at the brace tip.

Returns

Tex

get_tip() → Point3D

Returns the point at the brace tip.

put_at_tip(mob: Mobject, use_next_to: bool = True, **kwargs: Any) → Self

Puts the given mobject at the brace tip.

Parameters
mob

The mobject to be placed at the tip.

use_next_to

If true, then next_to() is used to place the mobject at the tip.

kwargs

Any additional keyword arguments are passed to next_to() which is used to put the mobject next to the brace tip.

BraceBetweenPoints

两点之间的括号:不依附具体对象,任意位置放标注。

继承关系

digraph G { graph [rankdir=LR, bgcolor="transparent", splines=spline, concentrate=true, nodesep="0.15", ranksep="0.3"]; node [shape=box, penwidth=0, width=0.05, height=0.05, margin=0.05]; edge [penwidth=1]; "Mobject" -> "VMobject"; "VMobject" -> "VMobjectFromSVGPath"; "VMobjectFromSVGPath" -> "Brace"; "Brace" -> "BraceBetweenPoints"; }

参数

point_1

—,Point3DLike

point_2

—,Point3DLike

direction

array([0., 0., 0.]),Vector3DLike

API 文档

class manim.BraceBetweenPoints(point_1: Point3DLike, point_2: Point3DLike, direction: Vector3DLike = array([0., 0., 0.]), **kwargs: Any)

基类:Brace

Similar to Brace, but instead of taking a mobject it uses 2 points to place the brace.

A fitting direction for the brace is computed, but it still can be manually overridden. If the points go from left to right, the brace is drawn from below. Swapping the points places the brace on the opposite side.

Parameters

point_1 :

The first point.

point_2 :

The second point.

direction :

The direction from which the brace faces towards the points.

Examples

class BraceBPExample(Scene):
    def construct(self):
        p1 = [0,0,0]
        p2 = [1,2,0]
        brace = BraceBetweenPoints(p1,p2)
        self.play(Create(NumberPlane()))
        self.play(Create(brace))
        self.wait(2)

BraceLabel

括号 + 标签的组合体:一行代码完成这组等于某公式的标注。

继承关系

digraph G { graph [rankdir=LR, bgcolor="transparent", splines=spline, concentrate=true, nodesep="0.15", ranksep="0.3"]; node [shape=box, penwidth=0, width=0.05, height=0.05, margin=0.05]; edge [penwidth=1]; "Mobject" -> "VMobject"; "VMobject" -> "BraceLabel"; }

参数

obj

—,Mobject

text

—,str

brace_direction

array([ 0., -1., 0.]),Vector3DLike

label_constructor

<class 'manim.mobject.text.…

font_size

48,float

buff

0.2,float

brace_config

None,dict[str, Any] | None

API 文档

class manim.BraceLabel(obj: Mobject, text: str, brace_direction: Vector3DLike = array([ 0., -1., 0.]), label_constructor: type[SingleStringMathTex | Text] = <class 'manim.mobject.text.tex_mobject.MathTex'>, font_size: float = 48, buff: float = 0.2, brace_config: dict[str, Any] | None=None, **kwargs: Any)

基类:VMobject

Create a brace with a label attached.

Parameters

obj

The mobject adjacent to which the brace is placed.

text

The label text.

brace_direction

The direction of the brace. By default DOWN.

label_constructor

A class or function used to construct a mobject representing the label. By default MathTex.

font_size

The font size of the label, passed to the label_constructor.

buff

The buffer between the mobject and the brace.

brace_config

Arguments to be passed to Brace.

kwargs

Additional arguments to be passed to VMobject.

BraceText

Brace 的 Text 标签便捷版:标签自动用 Text 渲染。

继承关系

digraph G { graph [rankdir=LR, bgcolor="transparent", splines=spline, concentrate=true, nodesep="0.15", ranksep="0.3"]; node [shape=box, penwidth=0, width=0.05, height=0.05, margin=0.05]; edge [penwidth=1]; "Mobject" -> "VMobject"; "VMobject" -> "BraceLabel"; "BraceLabel" -> "BraceText"; }

参数

obj

—,Mobject

text

—,str

label_constructor

<class 'manim.mobject.text.…

API 文档

class manim.BraceText(obj: Mobject, text: str, label_constructor: type[SingleStringMathTex | Text] = <class 'manim.mobject.text.text_mobject.Text'>, **kwargs: Any)

基类:BraceLabel

Create a brace with a text label attached.

Parameters

obj

The mobject adjacent to which the brace is placed.

text

The label text.

brace_direction

The direction of the brace. By default DOWN.

label_constructor

A class or function used to construct a mobject representing the label. By default Text.

font_size

The font size of the label, passed to the label_constructor.

buff

The buffer between the mobject and the brace.

brace_config

Arguments to be passed to Brace.

kwargs

Additional arguments to be passed to VMobject.

Examples

class BraceTextExample(Scene):
    def construct(self):
        s1 = Square().move_to(2*LEFT)
        self.add(s1)
        br1 = BraceText(s1, "Label")
        self.add(br1)

        s2 = Square().move_to(2*RIGHT)
        self.add(s2)
        br2 = BraceText(s2, "Label")

        br2.change_label("new")
        self.add(br2)
        self.wait(0.1)

SVGMobject

把 SVG 矢量文件解析成矢量路径的 Mobject:图标、自定义图形直接导入,可控每个子路径。

继承关系

digraph G { graph [rankdir=LR, bgcolor="transparent", splines=spline, concentrate=true, nodesep="0.15", ranksep="0.3"]; node [shape=box, penwidth=0, width=0.05, height=0.05, margin=0.05]; edge [penwidth=1]; "Mobject" -> "VMobject"; "VMobject" -> "SVGMobject"; }

参数

file_name

None,str | os.PathLike | None

should_center

True,bool

height

2,float | None

width

None,float | None

color

None

opacity

None,float | None

fill_color

None

fill_opacity

None,float | None

stroke_color

None

stroke_opacity

None,float | None

stroke_width

None,float | None

svg_default

None,dict | None

path_string_config

None,dict | None

use_svg_cache

True,bool

快速上手

icon = SVGMobject("logo.svg")

API 文档

class manim.SVGMobject(file_name: str | PathLike | None = None, should_center: bool = True, height: float | None = 2, width: float | None = None, color: ManimColor | int | str | NDArray[int64] | tuple[int, int, int] | NDArray[float64] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float] | None = None, opacity: float | None = None, fill_color: ManimColor | int | str | NDArray[int64] | tuple[int, int, int] | NDArray[float64] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float] | None = None, fill_opacity: float | None = None, stroke_color: ManimColor | int | str | NDArray[int64] | tuple[int, int, int] | NDArray[float64] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float] | None = None, stroke_opacity: float | None = None, stroke_width: float | None = None, svg_default: dict | None = None, path_string_config: dict | None = None, use_svg_cache: bool = True, **kwargs: Any)

基类:VMobject

A vectorized mobject created from importing an SVG file.

Parameters

file_name

The path to the SVG file.

should_center

Whether or not the mobject should be centered after being imported.

height

The target height of the mobject, set to 2 Manim units by default. If the height and width are both set to None, the mobject is imported without being scaled.

width

The target width of the mobject, set to None by default. If the height and the width are both set to None, the mobject is imported without being scaled.

color

The color (both fill and stroke color) of the mobject. If None (the default), the colors set in the SVG file are used.

opacity

The opacity (both fill and stroke opacity) of the mobject. If None (the default), the opacity set in the SVG file is used.

fill_color

The fill color of the mobject. If None (the default), the fill colors set in the SVG file are used.

fill_opacity

The fill opacity of the mobject. If None (the default), the fill opacities set in the SVG file are used.

stroke_color

The stroke color of the mobject. If None (the default), the stroke colors set in the SVG file are used.

stroke_opacity

The stroke opacity of the mobject. If None (the default), the stroke opacities set in the SVG file are used.

stroke_width

The stroke width of the mobject. If None (the default), the stroke width values set in the SVG file are used.

svg_default

A dictionary in which fallback values for unspecified properties of elements in the SVG file are defined. If None (the default), color, opacity, fill_color fill_opacity, stroke_color, and stroke_opacity are set to None, and stroke_width is set to 0.

path_string_config

A dictionary with keyword arguments passed to VMobjectFromSVGPath used for importing path elements. If None (the default), no additional arguments are passed.

use_svg_cache

If True (default), the svg inputs (e.g. file_name, settings) will be used as a key and a copy of the created mobject will be saved using that key to be quickly retrieved if the same inputs need be processed later. For large SVGs which are used only once, this can be omitted to improve performance.

kwargs

Further arguments passed to the parent class.

static apply_style_to_mobject(mob: VMobject, shape: GraphicObject) → VMobject

Apply SVG style information to the converted mobject.

Parameters
mob

The converted mobject.

shape

The parsed SVG element.

static ellipse_to_mobject(ellipse: Ellipse | Circle) → Circle

Convert an ellipse or circle element to a vectorized mobject.

Parameters
ellipse

The parsed SVG ellipse or circle.

generate_config_style_dict() → dict[str, str]

Generate a dictionary holding the default style information.

generate_mobject() → Self

Parse the SVG and translate its elements to submobjects.

get_file_path() → Path

Search for an existing file based on the specified file name.

get_mobjects_from(svg: SVG) → tuple[list[VMobject], dict[str, VGroup]]

Convert the elements of the SVG to a list of mobjects.

Parameters
svg

The parsed SVG file.

static handle_transform(mob: VMobject, matrix: Matrix) → VMobject

Apply SVG transformations to the converted mobject.

Parameters
mob

The converted mobject.

matrix

The transformation matrix determined from the SVG transformation.

property hash_seed: tuple

A unique hash representing the result of the generated mobject points.

Used as keys in the SVG_HASH_TO_MOB_MAP caching dictionary.

init_svg_mobject(use_svg_cache: bool) → Self

Checks whether the SVG has already been imported and generates it if not.

See also

SVGMobject.generate_mobject()

static line_to_mobject(line: Line) → Line

Convert a line element to a vectorized mobject.

Parameters
line

The parsed SVG line.

modify_xml_tree(element_tree: ElementTree) → ElementTree

Modifies the SVG element tree to include default style information.

Parameters
element_tree

The parsed element tree from the SVG file.

move_into_position() → Self

Scale and move the generated mobject into position.

path_to_mobject(path: Path) → VMobjectFromSVGPath

Convert a path element to a vectorized mobject.

Parameters
path

The parsed SVG path.

static polygon_to_mobject(polygon: Polygon) → Polygon

Convert a polygon element to a vectorized mobject.

Parameters
polygon

The parsed SVG polygon.

polyline_to_mobject(polyline: Polyline) → VMobject

Convert a polyline element to a vectorized mobject.

Parameters
polyline

The parsed SVG polyline.

static rect_to_mobject(rect: Rect) → Rectangle

Convert a rectangle element to a vectorized mobject.

Parameters
rect

The parsed SVG rectangle.

static text_to_mobject(text: Text) → VMobject

Convert a text element to a vectorized mobject.

警告

Not yet implemented.

Parameters
text

The parsed SVG text.

VMobjectFromSVGPath

由单个 SVG path 数据字符串构造 VMobject 的底层工具,可程序化生成路径。

继承关系

digraph G { graph [rankdir=LR, bgcolor="transparent", splines=spline, concentrate=true, nodesep="0.15", ranksep="0.3"]; node [shape=box, penwidth=0, width=0.05, height=0.05, margin=0.05]; edge [penwidth=1]; "Mobject" -> "VMobject"; "VMobject" -> "VMobjectFromSVGPath"; }

参数

path_obj

—,se.Path

long_lines

False,bool

should_subdivide_sharp_curves

False,bool

should_remove_null_curves

False,bool

API 文档

class manim.VMobjectFromSVGPath(path_obj: Path, long_lines: bool = False, should_subdivide_sharp_curves: bool = False, should_remove_null_curves: bool = False, **kwargs: Any)

基类:VMobject

A vectorized mobject representing an SVG path.

备注

The long_lines, should_subdivide_sharp_curves, and should_remove_null_curves keyword arguments are only respected with the OpenGL renderer.

Parameters

path_obj

A parsed SVG path object.

long_lines

Whether or not straight lines in the vectorized mobject are drawn in one or two segments.

should_subdivide_sharp_curves

Whether or not to subdivide subcurves further in case two segments meet at an angle that is sharper than a given threshold.

should_remove_null_curves

Whether or not to remove subcurves of length 0.

kwargs

Further keyword arguments are passed to the parent class.

generate_points() → Self

Initializes points and therefore the shape.

Gets called upon creation. This is an empty method that can be implemented by subclasses.