3. Rotations and Transformations

Before we dive deeper into manim, it’ll be a good idea to organize our workflow. Keeping the python files in the root directory can get messy and confusing after a while. Create a folder, manim-intro, and from this point onwards, we’ll put all our files inside this folder. (Remeber how we exported the path at the end of the first tutorial, this was the reason). Tl;dr: Our working directory now is manim/manim-intro/

All the geometric shapes, including the one that we used in the previous program (Circle, and Square) are defined in manim/manimlib/mobject/geometry.py, you’ll come across mobject often when working with manim; it stands for Mathematical Object. Most of the shapes that we’ll deal with are actually VMobject or Vectorized Mathematical Object. VMobject in itself is a mathematical object (We’ll encounter another sub-classification of mobject – TextMobject in the next tutorial).

Mobject has some attributes associated with it. Most trivial of all, that we’ll be using most of the time is move_to. Check out the following example:

python3 -m manim move_to_example.py MoveToEx -p

PS: From now on, I’ll not be explicitly stating the command to render animation. And from next post, we’ll create one file for each tutorial with multiple classes in it.

You’d observe that this time the circle is not created at the centre, but slightly towards the right. That is credited to the move_to attribute invoked at line 6. You’d notice that we used a rather bizarre way giving the coordinate. UP, DOWN, RIGHT, LEFT are defined in manim/manimlib/constants.py and employs simple vector addition. That is if you want to move a mobject to, say (-4, -3), you’d do <mobject>.move_to(4*LEFT+3*DOWN).

We know that self.add(<mobject>) adds the respective mobject to the Scene. Whereas with, you’d see the mobject being created on the Scene. A similar method is self.play(GlowFromCenter(<mobject>))(Try it out and see what it does). Most of the methods in manim are named appropriately, so as to convey what they do; this naming convention plays out nicely when you come across the function you don’t know.

Now let’s take a look at ApplyMethod.

You see the animation of the circle, being created at centre and then moving to bottom left-ish corner. As apparent from the name, ApplyMethod applies the method move_to to the mobject : square. The parameter for move_to is given as a second argument in ApplyMethod, generally, it’s, self.play(ApplyMethod( mobject.method_name,method_args ))

Rotation

Rotating a mobject is the same as moving it. That is, you have two options, either rotate it before putting it to the scene (you’ll see the rotated object) or actually see the rotation on the screen. Either way, you’ll have to use the rotate function: <mobject>.rotate(<radians>). Try replacing the square.move_to(2*RIGHT+3*UP) to square.rotate(3), the angular measurements are taken in radians.

Transformations

Transformations take place from one mobject to another, i.e a Circle can be transformed into a Square. Therefore, this time we will have to create two mobjects, deviating from the naming convention to avoid monotonicity, I’ll rename Square mobject to s1 and Circle mobject to c1.

To transform square to circle, we first have to add a square to the scene. Since the transformation is just for the visual appeal, it makes sense that it happens on the scene. Unsurprisingly, it takes two arguments. The syntax (line 8) can be generalized as: self.play(Transform( <mobject_from> , <mobject_to>))

There’s a lot more to transformations and even more to shapes (colour, solid, opacity and so on). We’ll explore them as they come – but if you’re curious, snoop around geometry.py and with some trial and error, you’d find something. For now, play around with these functions. In the next tutorial, we’ll start with TextMobject (Text Mathematical Object), therefore, make sure you have latex installed and after that tutorial, we’ll start recreating this video.

You can use the argument “about_point” to rotate about any point, even outside the boundary of the shape.

In this case, you can do:
self.play(Rotate(line, angle, about_point = line.get_end()) #to animate the rotation
or
line.rotate(angle, about_point = line.get_end())

Alternatively, according to your requirements, you can also use “line.get_start()”.
Also, the argument “about_edge” can be used to rotate about the edge of a shape like a triangle or a square (I haven’t tried this out yet).