【Manim CE】常用Mobject与使用

本文以文档的形式,详细介绍常用的Mobject的使用方法与注意事项。
常用的几何图形、数学图像都涵盖其中。

当前文档版本:v0.16.0.post0

第一部分:向量化父类VMobject

本文所有物体都继承自此,故很多参数只在本部分阐述。


VMobject继承自Mobject

V的意思是向量化的,vectorized mobject

fill_color=None, fill_opacity=0.0, stroke_color=None,
stroke_opacity=1.0, stroke_width=DEFAULT_STROKE_WIDTH,
background_stroke_color=BLACK, background_stroke_opacity=1.0,
background_stroke_width=0, sheen_factor=0.0, sheen_direction=UL,
close_new_points=False,
pre_function_handle_to_anchor_scale_factor=0.01,
make_smooth_after_applying_functions=False, background_image=None,
shade_in_3d=False, tolerance_for_point_equality=1e-6,
n_points_per_cubic_curve=4, **kwargs

构造参数:

  • fill_color 填充颜色
  • fill_opacity 填充透明度
  • stroke_color 边框颜色
  • stroke_opacity 边框颜色透明度
  • stroke_width 边框宽度

    • 注:DEFAULT_STROKE_WIDTH = 4
  • background_stroke_color
  • background_stroke_opacity
  • background_stroke_width
  • sheen_factor 物体的光泽
  • sheen_direction 物体光泽的中心
  • close_new_points Indicates that it will not be displayed, but that it should count in parent mobject’s path
  • ...

第一节:几何

本节列举大量常用的几何图形。

manim.mobject.geometry


Circle 圆

manim.mobject.geometry.arc.Circle

radius: float | None = None, color: Color | str = RED, **kwargs,

构造参数:

  • radius(float或None)圆的半径,例如 1
  • color(str或Color)圆形的颜色,例如 WHITE
  • **kwargs 附加参数

构造示例:

from manim import *

class CircleExample(Scene):
    def construct(self):
        circle_1 = Circle(radius=1.0)
        circle_2 = Circle(radius=1.5, color=GREEN)
        circle_3 = Circle(radius=1.0, color=BLUE_B, fill_opacity=1)

        circle_group = Group(circle_1, circle_2, circle_3).arrange(buff=1)
        self.add(circle_group)

【Manim CE】常用Mobject与使用

Dot 点

manim.mobject.geometry.arc.Dot

point: list | np.ndarray = ORIGIN,
radius: float = DEFAULT_DOT_RADIUS,
stroke_width: float = 0,
fill_opacity: float = 1.0,
color: Color | str = WHITE,

构造参数:

  • point(数组)屏幕坐标,例如 [0,0,0]
  • radius(float)点的半径,例如 0.05
  • stroke_width (float)点的轮廓宽度,例如 0.01
  • fill_opacity(float)点内部的颜色,例如 YELLOW
  • **kwargs 附加参数

构造示例:

from manim import *

class DotExample(Scene):
    def construct(self):
        dot1 = Dot(point=LEFT, radius=0.08)
        dot2 = Dot(point=ORIGIN)
        dot3 = Dot(point=RIGHT)
        self.add(dot1,dot2,dot3)

【Manim CE】常用Mobject与使用

Ellipse 椭圆

manim.mobject.geometry.arc.Ellipse

width: float = 2,
height: float = 1,
**kwargs

构造参数:

  • width(float)短轴
  • height(float)长轴
  • **kwargs 附加参数

    构造示例:

    from manim import *

    class EllipseExample(Scene):

    def construct(self):
        ellipse_1 = Ellipse(width=2.0, height=4.0, color=BLUE_B)
        ellipse_2 = Ellipse(width=4.0, height=1.0, color=BLUE_D)
        ellipse_group = Group(ellipse_1,ellipse_2).arrange(buff=1)
        self.add(ellipse_group)
    

【Manim CE】常用Mobject与使用

Angle 角

manim.mobject.geometry.line.Angle

line1: Line, line2: Line, radius: float = None, quadrant=(1, 1),
other_angle: bool = False, dot=False, dot_radius=None,
dot_distance=0.55, dot_color=WHITE, elbow=False, **kwargs,

构造参数:

  • line1(Line)起始线

    • 注:Line可以视为两个点坐标的集合,两点连成的线段。
  • line2(Line)终止线

    • 注:line1、line2两条线不能平行
  • radius(float)原点与角度弧线的距离半径
  • quadrant 角度弧线的象限,可传入:(1,1) (1,-1) (-1,1) (-1,-1)
  • other_angle(bool)从正方向画角度弧线
  • dot(bool)弧度中心标记一个点,一般用于表示角度位置
  • dot_radius(float)点的半径
  • dot_distance(float)点离原点的距离
  • elbow(bool)表示直角的角度折现
  • **kwargs 附加参数

构造示例:

from manim import *

class RightArcAngleExample(Scene):
    def construct(self):
        line1 = Line( LEFT, RIGHT )
        line2 = Line( DOWN, UP )
        rightarcangles = [
            Angle(line1, line2, dot=True),
            Angle(line1, line2, radius=0.4, quadrant=(1,-1), dot=True, other_angle=False),
            Angle(line1, line2, radius=0.5, quadrant=(-1,1), stroke_width=8, dot=True, dot_color=YELLOW, dot_radius=0.04, other_angle=True),
            Angle(line1, line2, radius=0.7, quadrant=(-1,-1), color=RED, dot=True, dot_color=GREEN, dot_radius=0.08),
        ]
        plots = VGroup()
        for angle in rightarcangles:
            plot=VGroup(line1.copy(),line2.copy(), angle)
            plots.add(plot)
        plots.arrange(buff=1.5)
        self.add(plots)

【Manim CE】常用Mobject与使用

Line 线

manim.mobject.geometry.line.Line

start=LEFT, end=RIGHT, buff=0, path_arc=None, **kwargs

构造参数:

  • start(list)起始点
  • end(list)终点
  • buff(float)两端点与可见线的距离
  • **kwargs 附加参数

构造示例:

from manim import *

class LineExample(Scene):
    def construct(self):
        ax = Axes()
        line1 = Line(ax.c2p(1,-3),ax.c2p(1,3),buff=0)
        line2 = Line(ax.c2p(2,-3),ax.c2p(2,3),buff=1)
        line3 = Line(ax.c2p(2,-3),ax.c2p(2,3),path_arc=PI)
        self.add(ax,line1,line2,line3)

【Manim CE】常用Mobject与使用

Rectangle 矩形

manim.mobject.geometry.polygram.Rectangle

color: Color = WHITE, height: float = 2.0, width: float = 4.0,
grid_xstep: float | None = None, grid_ystep: float | None = None,
**kwargs,

构造参数:

  • color(Color)颜色
  • height(float)高度
  • width(float)宽度
  • grid_xstep(float或None)分割x
  • gird_ystep(float或None)分割y
  • **kwargs 附加参数

构造示例:

from manim import *

class RectangleExample(Scene):
    def construct(self):
        rect1 = Rectangle(width=4.0, height=2.0, grid_xstep=1.0, grid_ystep=0.5)
        rect2 = Rectangle(width=1.0, height=4.0)

        rects = Group(rect1,rect2).arrange(buff=1)
        self.add(rects)

【Manim CE】常用Mobject与使用

Square 正方形

manim.mobject.geometry.polygram.Square

side_length=2.0,
**kwargs

构造参数:

  • side_length (float)边长
  • **kwargs 构造参数

构造示例:

from manim import *

class SquareExample(Scene):
    def construct(self):
        square = Square(side_length=2.0,
                     color=BLUE,fill_opacity=1,sheen_factor=0.8)
        self.add(square)

【Manim CE】常用Mobject与使用

Star 星

manim.mobject.geometry.polygram.Star

n: int = 5,
outer_radius: float = 1,
inner_radius: float | None = None,
density: int = 2,
start_angle: float | None = TAU / 4,
**kwargs,

构造参数:

  • n(int)节点数量
  • outer_radius(float)外半径
  • inner_radius(float)内半径
  • density()

    构造示例:

    from manim import *

    class StarExample(Scene):

    def construct(self):
        pentagram = RegularPolygram(5, radius=2).to_edge(LEFT)
        star = Star(outer_radius=2, color=RED).next_to(pentagram,RIGHT)
        sum = VGroup(
            pentagram.copy().to_edge(RIGHT),
            star.copy().to_edge(RIGHT))
        self.add(pentagram,star,sum)
    

【Manim CE】常用Mobject与使用

from manim import *

class StarExample(Scene):
    def construct(self):
        pentagram = RegularPolygram(7, radius=2).to_edge(LEFT)
        star = Star(n=7,outer_radius=2, color=RED).next_to(pentagram,RIGHT)
        sum = VGroup(
            pentagram.copy().to_edge(RIGHT),
            star.copy().to_edge(RIGHT))
        self.add(pentagram,star,sum)

【Manim CE】常用Mobject与使用

Triangle 三角形

manim.mobject.geometry.polygram.Triangle

构造参数:

构造示例:

from manim import *

class TriangleExample(Scene):
    def construct(self):
        triangle_1 = Triangle()
        triangle_2 = Triangle().scale(2).rotate(60*DEGREES)
        tri_group = Group(triangle_1, triangle_2).arrange(buff=1)
        self.add(tri_group)

【Manim CE】常用Mobject与使用

SurroundingRectangle 环绕矩形

manim.mobject.geometry.shape\_matchers.SurroundingRectangle

mobject, color=YELLOW, buff=SMALL_BUFF, corner_radius=0.0, **kwargs

构造参数:

  • mobject(Mobject)被环绕的物体
  • buff(float)边框与物体的距离
  • corner_radius(float)边框圆角半径

构造示例:

from manim import *

class SurroundingRectExample(Scene):
    def construct(self):
        title = Title("Fourier Transform")
        quote = Text('''
Generally speaking, the two ways to describe the same problem 
can be called that they are the mutual transform to each other.
''',color=BLUE,
        ).scale(0.75)
        box = SurroundingRectangle(quote, color=YELLOW, buff=MED_LARGE_BUFF)

        t2 = Tex(r"Hello World").scale(1.5)
        box2 = SurroundingRectangle(t2, corner_radius=0.2)
        mobjects = VGroup(VGroup(box, quote), VGroup(t2, box2)).arrange(DOWN)
        self.add(title, mobjects)

【Manim CE】常用Mobject与使用

Underline 下划线

manim.mobject.geometry.shape\_matchers.Underline

mobject, buff=SMALL_BUFF, **kwargs

构造参数:

  • mobject(Mobject)被标记下划线的物体
  • buff(float)间距

构造示例:

from manim import *

class UnderLine(Scene):
    def construct(self):
        man = MathTex("Manim \enspace Remoo")  # Full Word
        ul = Underline(man)  # Underlining the word
        self.add(man, ul)

【Manim CE】常用Mobject与使用

第二节:数学图像

本节主要尽可能详细的列举需要使用的数学图像VMobject。

manim.mobject.graphing


Axes 直角坐标

manim.mobject.graphing.coordinate\_systems.Axes

x_range: Sequence[float] | None = None, y_range: Sequence[float] |
None = None, x_length: float | None = round(config.frame_width) - 2,
y_length: float | None = round(config.frame_height) - 2, axis_config:
dict | None = None, x_axis_config: dict | None = None, y_axis_config:
dict | None = None, tips: bool = True, **kwargs,

构造参数:

  • x_range(Sequence[float])坐标的横坐标范围,序列 [左边界,右边界,步进]
  • y_range(Sequence[float])坐标的纵坐标范围,序列 [下边界,上边界,步进]
  • x_length(float)坐标物体Mobject的宽度
  • y_length(float)坐标物体Mobject的高度
  • axis_config(dict)坐标参数集
  • x_axis_config 同上
  • y_axis_config 同上
  • tips(bool)坐标箭头

构造示例:

from manim import *

class AxesExample(Scene):
    def construct(self):
        ax1 = Axes(
            x_range=[-2*2*PI,2*2*PI,PI],
            y_range=[-1,1,0.5],
            x_length=6,
            tips=True).to_edge(LEFT)
        graph1 = ax1.plot(lambda x : np.sin(x))
        ax2 = Axes(
            x_range=[-1,4,1],
            y_range=[-2,2,1],
            x_length=6,
            tips=False).to_edge(RIGHT)
        graph2 = VGroup(*(Dot(ax2.c2p(x-1,2-x)) for x in range(5)))

        self.add(ax1,graph1,ax2,graph2)

【Manim CE】常用Mobject与使用

from manim import *

class LogScalingExample(Scene):
    def construct(self):
        ax = Axes(
            x_range=[0, 10, 1],
            y_range=[-2, 6, 1],
            tips=False,
            axis_config={"include_numbers": True},
            y_axis_config={"scaling": LogBase(custom_labels=True)},
        )

        graph = ax.plot(lambda x: x ** 2, x_range=[0.001, 10,0.01])
        self.add(ax, graph)

【Manim CE】常用Mobject与使用

NumberPlane 数轴

manim.mobject.graphing.coordinate\_systems.NumberPlane

x_range: Sequence[float] | None = ( -config["frame_x_radius"],
config["frame_x_radius"],1,), y_range: Sequence[float] | None = (
-config["frame_y_radius"], config["frame_y_radius"],1,),
x_length: float | None = None, y_length: float | None = None,
background_line_style: dict | None = None, faded_line_style: dict |
None = None, faded_line_ratio: int = 1,
make_smooth_after_applying_functions: bool = True, **kwargs,

构造参数:

  • x_range(Sequence[float])数轴的x轴参数,默认是屏幕的宽度:[-(7+1/9),(7+1/9),1]
  • y_range(Sequence[float]) 数轴的y轴参数,默认是屏幕的高度:[-4,4,1]
  • x_length(float)数轴的宽度,默认同上x_range
  • y_length(float)数轴的高度,默认同上y_range
  • background_line_style(dict) 例:"stroke_color": RED, "stroke_width": 2, "stroke_opacity": 1,
  • faded_line_style(dict)
  • faded_line_style(int)
  • make_smooth_after_applying_functions(bool)

构造示例:

from manim import *

class CoordsToPointExample(Scene):
    def construct(self):
        plane = NumberPlane()
        dot_scene = Dot((2,2,0), color=RED)

        self.add(plane, dot_scene)

【Manim CE】常用Mobject与使用

from manim import *

class NumberPlaneExample(Scene):
    def construct(self):
        number_plane = NumberPlane(
            background_line_style={
                "stroke_color": TEAL,
                "stroke_width": 4,
                "stroke_opacity": 0.6
            }
        )
        self.add(number_plane)

【Manim CE】常用Mobject与使用

未完待续。。

评论区 0