Search code examples
pythonanimationmanim

How to animate a simultaneous change in angle and rotation for a sector in manim


Is it possible to animate a rotation and a size change on the same Sector at the same time?

I know it can be cheated with Transform to turn it into a different Sector. But that doesn't have the animation I want. Transform shrinks and then expands. I want the Sector to rotate while changing size.

I can get the two effects I want but not at the same time.

I've tried but failed to find an API for manim. I've seen the readthedocs but that isn't an API showing all the methods and variables for each Class.

class Test(Scene):
    def construct(self):
        rotateGroup=VGroup(*[SmallDot()],
                           Sector(angle=45*DEGREES,
                                  start_angle=25*DEGREES,
                                  stroke_width=0,
                                  fill_color=RED),
                           Sector(angle=135*DEGREES,
                                  start_angle=185*DEGREES,
                                  stroke_width=0,
                                  fill_color=GREEN))\
                                  .shift(RIGHT,DOWN)
        # Start
        self.add(rotateGroup[1])
        self.wait()
        self.play(FadeOut(rotateGroup[1]),
                  # Ideal finishing position
                  FadeIn(rotateGroup[2]))
        self.wait()
        self.remove(rotateGroup[2])
        copy1=rotateGroup[1].copy()
        copy2=rotateGroup[2].copy()
        copy3=rotateGroup[1].copy()
        self.add(copy1)
        self.wait()

        # This has the desired rotation but lacks the size change
        self.play(Rotating(copy1,
                           axis=OUT,
                           run_time=2,
                           about_point=rotateGroup[0].get_center(),
                           radians=170*DEGREES,
                           rate_func=smooth))
        self.wait()
        self.remove(copy1)

        # This has the size change but lacks the correct rotation
        self.add(copy3)
        self.play(Transform(copy3,copy2))

Thanks in advance.


Solution

  • class Test(Scene):
        def construct(self):
            rotateGroup=VGroup(
                Sector(angle=45*DEGREES,
                        start_angle=25*DEGREES,
                        stroke_width=0,
                        fill_color=RED
                ),
                Sector(angle=135*DEGREES,
                        start_angle=185*DEGREES,
                        stroke_width=0,
                        fill_color=GREEN
                        )
            )
            red_sector, green_sector = rotateGroup
            # -----------------------------------------------
            red_sector.save_state()
            def update_sector(mob,alpha):
                mob.restore()
                angle = interpolate(45*DEGREES,135*DEGREES,alpha)
                start_angle = interpolate(25*DEGREES,185*DEGREES,alpha)
                mob.become(
                    Sector(
                        angle=angle,
                        start_angle=start_angle,
                        stroke_width=0,
                        fill_color=interpolate_color(RED,GREEN,alpha)
                    )
                )
            self.play(
                UpdateFromAlphaFunc(red_sector,update_sector)
            )
            self.wait()
    

    As a task, try to do a custon animation, tutorial here