Search code examples
transformationmanim

Manim Equation Transformation


Manim Community v0.15.1

class Equation_Transformation_Bug(Scene):
    def construct(self):
        equation_1 = MathTex("w", "\\times","v", "=", "1")
        equation_1.shift(UP*2).scale(2)
        equation_2 = MathTex("v", "=", "w^{-1}")
        equation_2.scale(2)
        equation_3 = MathTex("w", "\\times","w^{-1}", "=", "1")
        equation_3.shift(UP*2).scale(2)

        self.play(Write(equation_1), Write(equation_2))
        self.wait(2)
        self.play(FadeOut(equation_1[2]))

        self.play(*[
            Transform(
                equation_2.get_part_by_tex("w^{-1}"),
                equation_3.get_part_by_tex("w^{-1}")                
            )
        ] + [
            Transform(
                equation_1.get_part_by_tex(tex),
                equation_3.get_part_by_tex(tex)
            )
            for tex in ("w", "\\times","=", "1")
        ])
        self.wait(1)

I'm trying to get the w^{-1} from equation_2 to fly into the spot formerly occupied by v of equation_1 and transform into equation_3. The "1" from equation_1, instead, transforms into the w^{-1} from equation_3. I'm not trying to do a replacement transform.

How do I transform equation_1 into equation_3 and move the w^{-1} in the spot occupied by "v" of equation_1?


Solution

  • An approach using TransformMatchingShapes works reasonably well in this particular case:

    class Eq(Scene):
        def construct(self):
            equation_1 = MathTex("w", "\\times","v", "=", "1")
            equation_1.shift(UP*2).scale(2)
            equation_2 = MathTex("v", "=", "w^{-1}")
            equation_2.scale(2)
            equation_3 = MathTex("w", "\\times","w^{-1}", "=", "1")
            equation_3.shift(UP*2).scale(2)
    
            self.play(Write(equation_1), Write(equation_2))
            self.wait(2)
            self.play(FadeOut(equation_1[2]))
    
            self.play(
                TransformMatchingShapes(
                    VGroup(equation_1[0:2], equation_1[3:], equation_2[2].copy()),
                    equation_3,
                )
            )
    

    If you have shapes that would not match uniquely, take a look at the implementation of TransformMatchingShapes, there is a way to tweak what exactly gets transformed into what.