Transform

Transform

class manimlib.animation.transform.Transform(mobject, target_mobject=None, **kwargs)
TransformExample
class TransformExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").scale(3)
        B = TextMobject("Text-B").scale(3)
        C = TextMobject("C-Text").scale(3)

        self.add(A)
        self.wait()
        self.play(Transform(A, B))
        self.wait()
        self.play(Transform(A, C)) # notice here
        self.wait()

ReplacementTransform

class manimlib.animation.transform.ReplacementTransform(mobject, target_mobject=None, **kwargs)
ReplacementTransformExample
class ReplacementTransformExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").scale(3)
        B = TextMobject("Text-B").scale(3)
        C = TextMobject("C-Text").scale(3)

        self.add(A)
        self.wait()
        self.play(ReplacementTransform(A, B))
        self.wait()
        self.play(ReplacementTransform(B, C)) # notice here
        self.wait()

Transform和ReplacementTransform的区别

  1. Transform(A, B) 在画面上 A 变成了 B 的样子,但是画面上的物体名字还叫 A

  2. ReplacementTransform(A, B) 在画面上 A 变成了 B 的样子,并且画面上的物体名字叫 B

所以以下两个效果相同

self.play(Transform(A, B))
self.play(Transform(A, C))
self.play(ReplacementTransform(A, B))
self.play(ReplacementTransform(B, C))

TransformFromCopy

class manimlib.animation.transform.TransformFromCopy(mobject, target_mobject, **kwargs)

Performs a reversed Transform

TransformFromCopyExample
class TransformFromCopyExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").scale(3)
        B = TextMobject("Text-B").scale(3).shift(UP*2)

        self.add(A)
        self.wait()
        self.play(TransformFromCopy(A, B))
        self.wait()

ClockwiseTransform

class manimlib.animation.transform.ClockwiseTransform(mobject, target_mobject=None, **kwargs)
ClockwiseTransformExample
class ClockwiseTransformExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").scale(3)
        B = TextMobject("Text-B").scale(3).shift(UP*2)

        self.add(A)
        self.wait()
        self.play(ClockwiseTransform(A, B))
        self.wait()

CounterclockwiseTransform

class manimlib.animation.transform.CounterclockwiseTransform(mobject, target_mobject=None, **kwargs)
CounterclockwiseTransformExample
class CounterclockwiseTransformExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").scale(3)
        B = TextMobject("Text-B").scale(3).shift(UP*2)

        self.add(A)
        self.wait()
        self.play(CounterclockwiseTransform(A, B))
        self.wait()

MoveToTarget

class manimlib.animation.transform.MoveToTarget(mobject, **kwargs)
MoveToTargetExample
class MoveToTargetExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").to_edge(LEFT)
        A.generate_target()  # copyA自身形成A的target属性
        A.target.scale(3).shift(RIGHT*7+UP*2) # 操作A的target

        self.add(A)
        self.wait()
        self.play(MoveToTarget(A))
        self.wait()

另外,直接使用 self.play(mob.method, ...) 相当于给mob创建target,然后根据method 操作target。并且这样能对同一个mobject多次操作(而 ApplyMethod 不可以,见下)

SelfPlayExample
class SelfPlayExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").to_edge(LEFT)

        self.add(A)
        self.wait()
        self.play(
            A.scale, 3,
            A.shift, RIGHT*7+UP*2,
        )
        self.wait()

ApplyMethod

class manimlib.animation.transform.ApplyMethod(method, *args, **kwargs)
ApplyMethodExample
class ApplyMethodExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").to_edge(LEFT)

        self.add(A)
        self.wait()
        self.play(
            ApplyMethod(A.scale, 3), # 这个不会执行
            ApplyMethod(A.shift, RIGHT*7+UP*2),
        )
        self.wait()

因为 ApplyMethod 是Transform的子类,所以每次只能对同一个物体执行一次操作(最后一次)

ApplyPointwiseFunction

class manimlib.animation.transform.ApplyPointwiseFunction(function, mobject, **kwargs)

等价于 ApplyMethod(mobject.apply_function, function, **kwargs)

ApplyPointwiseFunctionToCenter

class manimlib.animation.transform.ApplyPointwiseFunctionToCenter(function, mobject, **kwargs)

FadeToColor

class manimlib.animation.transform.FadeToColor(mobject, color, **kwargs)

等价于 ApplyMethod(mobject.set_color, color, **kwargs)

ScaleInPlace

class manimlib.animation.transform.ScaleInPlace(mobject, scale_factor, **kwargs)

等价于 ApplyMethod(mobject.scale, scale_factor, **kwargs)

ShrinkToCenter

class manimlib.animation.transform.ShrinkToCenter(mobject, **kwargs)
ShrinkToCenterExample
class ShrinkToCenterExample(Scene):
    def construct(self):
        mobjects = VGroup(
                Square(),
                RegularPolygon(fill_opacity=1),
                TextMobject("Text").scale(2)
            )
        mobjects.scale(1.5)
        mobjects.arrange_submobjects(RIGHT,buff=2)

        self.play(
            *[ShrinkToCenter(mob) for mob in mobjects]
        )

        self.wait()

Restore

class manimlib.animation.transform.Restore(mobject, **kwargs)

等价于 ApplyMethod(mobject.restore, **kwargs)

RestoreExample
class RestoreExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").to_edge(LEFT)
        A.save_state()  # 记录下现在状态,restore会回到此时
        A.scale(3).shift(RIGHT*7+UP*2)

        self.add(A)
        self.wait()
        self.play(Restore(A))
        self.wait()

ApplyFunction

class manimlib.animation.transform.ApplyFunction(function, mobject, **kwargs)
ApplyFunctionExample
class ApplyFunctionExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").to_edge(LEFT)
        def function(mob):
            return mob.scale(3).shift(RIGHT*7+UP*2)
            # 需要return一个mobject

        self.add(A)
        self.wait()
        self.play(ApplyFunction(function, A))
        self.wait()

ApplyMatrix

class manimlib.animation.transform.ApplyMatrix(matrix, mobject, **kwargs)
ApplyMatrixExample
class ApplyMatrixExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").scale(2)
        mat = np.array([
            [1, 3, 1],
            [0.5, 1, 1],
            [1, 1, 1]
        ])

        self.add(A)
        self.wait()
        self.play(ApplyMatrix(mat, A))
        self.wait()

ApplyComplexFunction

class manimlib.animation.transform.ApplyComplexFunction(function, mobject, **kwargs)

等价于 ApplyMethod(mobject.apply_complex_function, function, **kwargs)

CyclicReplace

class manimlib.animation.transform.CyclicReplace(*mobjects, **kwargs)
CyclicReplaceExample
class CyclicReplaceExample(Scene):
    def construct(self):
        A = TextMobject("Text-A").scale(3)
        B = TextMobject("Text-B").scale(3)
        VGroup(A, B).arrange(RIGHT)

        self.add(A, B)
        self.wait()
        self.play(CyclicReplace(A, B)) # 或Swap(A, B)
        self.wait()

Swap

class manimlib.animation.transform.Swap(*mobjects, **kwargs)

CyclicReplace

TransformAnimations

class manimlib.animation.transform.TransformAnimations(start_anim, end_anim, **kwargs)

这个不常用,甚至Grant都怀疑它存在的意义