VGroup

限定名称:manim.mobject.types.vectorized\_mobject.VGroup

class VGroup(*vmobjects, **kwargs)[源]

基类:VMobject

一组向量化mobject。

这可用于将多个VMobject实例组合在一起,以便一起缩放、移动等。

注意

多次添加同一个mobject时,重复项将被忽略。使用Mobject.copy()创建一个单独的副本,然后将其添加到组中。

示例

要添加VGroup,您可以使用add()方法,或使用++=运算符。同样,您可以通过remove()方法或--=运算符来从VGroup中移除元素。

>>> from manim import Triangle, Square, VGroup
>>> vg = VGroup()
>>> triangle, square = Triangle(), Square()
>>> vg.add(triangle)
VGroup(Triangle)
>>> vg + square  # a new VGroup is constructed
VGroup(Triangle, Square)
>>> vg  # not modified
VGroup(Triangle)
>>> vg += square
>>> vg  # modifies vg
VGroup(Triangle, Square)
>>> vg.remove(triangle)
VGroup(Square)
>>> vg - square  # a new VGroup is constructed
VGroup()
>>> vg  # not modified
VGroup(Square)
>>> vg -= square
>>> vg  # modifies vg
VGroup()

示例:ArcShapeIris

../_images/ArcShapeIris-1.png
from manim import *

class ArcShapeIris(Scene):
    def construct(self):
        colors = [DARK_BROWN, BLUE_E, BLUE_D, BLUE_A, TEAL_B, GREEN_B, YELLOW_E]
        radius = [1 + rad * 0.1 for rad in range(len(colors))]

        circles_group = VGroup()

        # zip(radius, color) makes the iterator [(radius[i], color[i]) for i in range(radius)]
        circles_group.add(*[Circle(radius=rad, stroke_width=10, color=col)
                            for rad, col in zip(radius, colors)])
        self.add(circles_group)
class ArcShapeIris(Scene):
    def construct(self):
        colors = [DARK_BROWN, BLUE_E, BLUE_D, BLUE_A, TEAL_B, GREEN_B, YELLOW_E]
        radius = [1 + rad * 0.1 for rad in range(len(colors))]

        circles_group = VGroup()

        # zip(radius, color) makes the iterator [(radius[i], color[i]) for i in range(radius)]
        circles_group.add(*[Circle(radius=rad, stroke_width=10, color=col)
                            for rad, col in zip(radius, colors)])
        self.add(circles_group)

方法

添加

检查所有传入元素是否为VMobject的实例或可迭代对象,然后将它们添加到子对象中

属性

animate (动画)

用于动画化 self 的任何方法的应用。

animation_overrides (动画覆盖)

颜色

depth (深度)

mobject 的深度。

fill_color (填充颜色)

如果存在多种颜色(用于渐变),则返回第一种颜色

height (高度)

mobject 的高度。

n_points_per_curve (每条曲线的点数)

sheen_factor (光泽因子)

stroke_color (描边颜色)

width (宽度)

mobject 的宽度。

参数:
  • vmobjects (VMobject | VMobject可迭代对象[VMobject])

  • kwargs (任意类型)

_original__init__(*vmobjects, **kwargs)

初始化自身。有关准确签名,请参阅 help(type(self))。

参数:
  • vmobjects (VMobject | VMobject可迭代对象[VMobject])

  • kwargs (任意类型)

返回类型:

add(*vmobjects)[源]

检查所有传入元素是否为VMobject的实例或可迭代对象,然后将它们添加到子对象中

参数:

vmobjects (VMobject | VMobject可迭代对象[VMobject]) – 要添加的VMobject列表或可迭代对象

返回类型:

VGroup

抛出:

TypeError – 如果列表或可迭代对象中的某个元素不是VMobject的实例

示例

以下示例展示了如何通过VGroup构造函数及其.add()方法添加单个或多个VMobject实例。

示例:AddToVGroup

from manim import *

class AddToVGroup(Scene):
    def construct(self):
        circle_red = Circle(color=RED)
        circle_green = Circle(color=GREEN)
        circle_blue = Circle(color=BLUE)
        circle_red.shift(LEFT)
        circle_blue.shift(RIGHT)
        gr = VGroup(circle_red, circle_green)
        gr2 = VGroup(circle_blue) # Constructor uses add directly
        self.add(gr,gr2)
        self.wait()
        gr += gr2 # Add group to another
        self.play(
            gr.animate.shift(DOWN),
        )
        gr -= gr2 # Remove group
        self.play( # Animate groups separately
            gr.animate.shift(LEFT),
            gr2.animate.shift(UP),
        )
        self.play( #Animate groups without modification
            (gr+gr2).animate.shift(RIGHT)
        )
        self.play( # Animate group without component
            (gr-circle_red).animate.shift(RIGHT)
        )
class AddToVGroup(Scene):
    def construct(self):
        circle_red = Circle(color=RED)
        circle_green = Circle(color=GREEN)
        circle_blue = Circle(color=BLUE)
        circle_red.shift(LEFT)
        circle_blue.shift(RIGHT)
        gr = VGroup(circle_red, circle_green)
        gr2 = VGroup(circle_blue) # Constructor uses add directly
        self.add(gr,gr2)
        self.wait()
        gr += gr2 # Add group to another
        self.play(
            gr.animate.shift(DOWN),
        )
        gr -= gr2 # Remove group
        self.play( # Animate groups separately
            gr.animate.shift(LEFT),
            gr2.animate.shift(UP),
        )
        self.play( #Animate groups without modification
            (gr+gr2).animate.shift(RIGHT)
        )
        self.play( # Animate group without component
            (gr-circle_red).animate.shift(RIGHT)
        )

一个VGroup也可以使用可迭代对象创建。请记住,可迭代对象生成的所有值都必须是VMobject的实例。示例如下:

示例:AddIterableToVGroupExample

../_images/AddIterableToVGroupExample-1.png
from manim import *

class AddIterableToVGroupExample(Scene):
    def construct(self):
        v = VGroup(
            Square(),               # Singular VMobject instance
            [Circle(), Triangle()], # List of VMobject instances
            Dot(),
            (Dot() for _ in range(2)), # Iterable that generates VMobjects
        )
        v.arrange()
        self.add(v)
class AddIterableToVGroupExample(Scene):
    def construct(self):
        v = VGroup(
            Square(),               # Singular VMobject instance
            [Circle(), Triangle()], # List of VMobject instances
            Dot(),
            (Dot() for _ in range(2)), # Iterable that generates VMobjects
        )
        v.arrange()
        self.add(v)

为了方便这一点,可迭代对象在将其单独的实例添加到VGroup之前会被解包。因此,当您索引VGroup时,您永远不会得到一个可迭代对象。相反,您将始终收到VMobject实例,包括那些最初添加到VGroup中的可迭代对象的一部分。