Search code examples
pythonmanim

Multiple animations simultaneously on a single object in Manim


I have a Square, and I want to move and scale it at the same time:

sq = Square()
self.add(sq)
self.play(ApplyMethod(sq.scale, 0.5), ApplyMethod(sq.move_to, UP*3))

However, the first animation is skipped and only the last one works.

Is there a simpler solution than using Transform?


Solution

  • There are 3 ways:

    class MultipleMethods1(Scene):
        def construct(self):
            sq = Square()
            self.add(sq)
            self.play(
                sq.scale, 0.5,
                sq.move_to, UP*3,
                run_time=5
            )
            self.wait()
    
    class MultipleMethods2(Scene):
        def construct(self):
            sq = Square()
            cr = Circle()
            VGroup(sq,cr).arrange(RIGHT)
            self.add(sq)
            def apply_function(mob):
                mob.scale(0.5)
                mob.shift(UP*3)
                return mob
    
            self.play(
                ApplyFunction(apply_function,sq),
                ApplyFunction(apply_function,cr),
                run_time=5
            )
            self.wait()
    
    class MultipleMethods3(Scene):
        def construct(self):
            sq = Square()
            self.add(sq)
            sq.generate_target()
            sq.target.scale(0.5)
            sq.target.move_to(UP*3)
    
            self.play(
                MoveToTarget(sq),
                run_time=5
            )
            self.wait()