Search code examples
pythonmanim

Manim: Is the a nice way to move and scale a bunch of mobjects?


I'm using the manim module in Python to display some decision trees. Fist I want to show Tree_1 as it is in the code below. Then I want it to scale it down and shift it to the left. Next I want Tree_2 to appear where Tree_1 is and them move to the upper right quadrant of the screen. Also the PURE_RED lines should move from tilted (as in Tree_1) to straight (as in Tree_2 in the code below). The same should then happen with Tree_3 just in the bottom right quadrant.

Now I could do it by figuring out all the points and then hardcode it. But I wanted to ask if there is a nicer way. Maybe one where I could define points in a local coordinate system and then I can just scale and move the whole tree.

Also I'm sorry if its is considered common knowledge, but I'm super new to manim.

from manim import *

class Tree_1(Scene):
    def construct(self):
        line_1 = Line([0,3,0], [-6,0,0])
        line_2 = Line([0,3,0], [0,0,0])
        line_3 = Line([0,3,0], [6,0,0])
                
        self.play(
            Create(line_1),
            Create(line_2),
            Create(line_3),
            )
        
        line_1l = Line([-6, 0, 0], [-7,-3, 0]).set_color(PURE_GREEN)
        line_1r = Line([-6, 0, 0], [-5,-3, 0]).set_color(PURE_RED)
        line_2l = Line([ 0, 0, 0], [-1,-3, 0]).set_color(PURE_GREEN)
        line_2r = Line([ 0, 0, 0], [ 1,-3, 0]).set_color(PURE_RED)
        line_3l = Line([ 6, 0, 0], [ 5,-3, 0]).set_color(PURE_GREEN)
        line_3r = Line([ 6, 0, 0], [ 7,-3, 0]).set_color(PURE_RED)

        self.play(
            Create(line_1l),
            Create(line_1r),
            Create(line_2l),
            Create(line_2r),
            Create(line_3l),
            Create(line_3r),
            )

class Tree_2(Scene):
    def construct(self):
        line_1 = Line([0,3,0], [-6,0,0])
        line_2 = Line([0,3,0], [0,0,0])
        line_3 = Line([0,3,0], [6,0,0])
                
        self.play(
            Create(line_1),
            Create(line_2),
            Create(line_3),
            )
        
        line_4 = Line([-6, 0, 0], [-6,-3, 0]).set_color(PURE_RED)
        line_5 = Line([ 0, 0, 0], [-0,-3, 0]).set_color(PURE_RED)
        line_6 = Line([ 6, 0, 0], [ 6,-3, 0]).set_color(PURE_RED)

        self.play(
            Create(line_4),
            Create(line_5),
            Create(line_6),
            )

class Tree_3(Scene):
    def construct(self):
        line_1 = Line([0,3,0], [-6,0,0])
        line_2 = Line([0,3,0], [0,0,0])
        line_3 = Line([0,3,0], [6,0,0])
                
        self.play(
            Create(line_1),
            Create(line_2),
            Create(line_3),
            )
        
        line_4 = Line([-6, 0, 0], [-6,-3, 0]).set_color(PURE_GREEN)
        line_5 = Line([ 0, 0, 0], [-0,-3, 0]).set_color(PURE_GREEN)
        line_6 = Line([ 6, 0, 0], [ 6,-3, 0]).set_color(PURE_GREEN)

        self.play(
            Create(line_4),
            Create(line_5),
            Create(line_6),
            )

Solution

  • Yes there is, add them to a VGroup, here is the docs.