3

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?

1 Answers1

4

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.

Benjamin Hackl
  • 1,011
  • 2
  • 8
  • This works very nicely. If you know why my method didn't produce the results that you provided, I would like to know. – SuperUser_Vermeylen Apr 20 '22 at 23:23
  • 1
    @SuperUser_Vermeylen sure: the problem is `get_part_by_tex`, which actually by default returns the first part in which the queried string appears as a substring. If you change your calls to `.get_part_by_tex(tex, substring=False)`, the animation renders also just fine! – Benjamin Hackl Apr 22 '22 at 00:16