Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply multiple animations for one geometry at the same time

Tags:

manim

There is a square location in the ORIGIN, I want to move it to UP*3 and scale to 0.5 at the same with below code snippet:

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

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

I know creating another small square and using transform can do, but that will bring more code, is there simple solution for this? thanks!

like image 744
James Hao Avatar asked Jan 26 '23 00:01

James Hao


1 Answers

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()
like image 114
TheoremOfBeethoven Avatar answered Jun 14 '23 02:06

TheoremOfBeethoven