Creating Your First Scene – manim Series: Part 2

This is part of a series of posts on my journey learning how to use manim, a mathematical animation toolbox created for the 3blue1brown video series. Check out the previous entry to install the required packages to get started.

Important Note:  These posts are based on an earlier version of manim which uses Python 2.7.  The latest version of manim is using Python 3.  To follow along with these posts, use Python 2.7 and the May 9, 2018 commit of manim .

To give you an idea how I’m approaching learning manim, I started off by going through the example scenes in example_scenes.py in the top-level manim folder. I also found another Github repository by Adirockzz95, who had worked out several more example files. Once I had a good feel for those examples, I started watching the 3b1b videos and when I came to an animation I wanted to learn I’d go to the original code (located in the old_projects folder of the manim code) and try to figure out how it worked. Note that, due to changes in manim, much of the older code will throw an error. However, trying to fix the code enough to run will give you a better idea of how things work.

2.0 Creating Your First Scene

Copy and paste the code below into a new text file and save it as manim_tutorial_1.py in the top-level manim directory. The .py extension tells your operating system that this is a Python file.

Open up a  command line window and go to the top-level manim directory, and type python extract_scene.py manim_tutorial_1.py Shapes -pl

We are calling the Python interpreter with the python command. The first argument passed to Python, extract_scence.py is the part of the manim code that runs your script and creates a video file. This argument will always need to be called to create videos. The second argument, manim_tutorial_1.py is the name of the file (i.e. the module) where your script is stored. The third argument, Shapes is the name of the class (i.e. the scene name) defined within your file that gives instructions on how to construct a scene. The last arguments, -pl tell the extract_scene script to preview the animation by playing it once it is done and to render the animation in low quality, which speeds up the time to create the animation. See the README.md for directions on what other arguments you can pass to extract_scene.

from big_ol_pile_of_manim_imports import *

class Shapes(Scene):
#A few simple shapes
def construct(self):
circle = Circle()
square = Square()
line=Line(np.array([3,0,0]),np.array([5,0,0]))
triangle=Polygon(np.array([0,0,0]),np.array([1,1,0]),np.array([1,-1,0]))

self.add(line)
self.play(ShowCreation(circle))
self.play(FadeOut(circle))
self.play(GrowFromCenter(square))
self.play(Transform(square,triangle))

If everything works you should see the following messages (or something similar) in your terminal:

The video should look like this:

All of the various manim modules are contained in big_ol_pile_of_manim_imports.py so importing this gives you all of the basic features of manim. This doesn’t include every single module from manim but it contains the core modules. It is worth your time to dive into some of the modules to see how things are put together. I’ve learned a surprising amount of Python by trying to figure out how things work. Incidentally I find using the search box at https://github.com/3b1b/manim very helpful for finding different classes and figuring out what arguments they take and how they work.

2.1 Scenes and Animation

The Scene is the script that tells manim how to place and animate your objects on the screen. I read that each 3blue1brown video is created as individual scenes which are stiched together using video editing software. You must define each scene as a separate class that is a subclass of Scene. This class must have a construct() method, along with any other code required for creating objects, adding them to the screen, and animating them. The construct() method is essentially the main method in the class that gets called when run through extract_scene.py. It is similar to __init__; it is the method that is automatically called when you create an instance of the class. Within this method you should define all of your objects, any code needed to control the objects, and code to place the objects onscreen and animate them.

For this first scene we’ve created a circle, a square, a line, and a triangle. Note that the coordinates are specified using numpy arrays np.array(). You can pass a 3-tuple like (3,0,0), which works sometimes, but some of the transformation methods expect the coordinates to be a numpy array.

One of the more important methods from the Scene() class is the play() method. play() is what processes the various animations you ask manim to perform. My favorite animation is Transform, which does a spectacular job of morphing one mobject into another. This scene shows a square changing into a triangle, but you can use the transform to morph any two objects together. To have objects appear on the screen without any animation you can use add() to place them. The line has been added and shows up in the very first frame, while the other objects are fade in or grow. The naming of the transformations is pretty straight forward so it’s usually obvious what each one does.

Things to try
– Change the order of the add() and play() commands.
– Try using the Transform() method on other shapes.
– Check out the shapes defined in geometry.py which is located in the /manim/mobject/ folder.

Next time: In the next entry in this series I’ll look at other shapes and how to change their properties.

Advertisements
This entry was posted in Just for Fun, Programming and tagged , , , . Bookmark the permalink.

7 Responses to Creating Your First Scene – manim Series: Part 2

  1. Pingback: More Shapes – manim Series: Part 3 | Talking Physics

  2. Pingback: Installing manim and Python – manim Series: Part 1 | Talking Physics

  3. Leo Martínez says:

    Hola,
    ¿cómo se puede añadir una forma geométrica sin ningún tipo de animación durante todo el tiempo que uno desee?
    Si solo ejecuto el método add() no se reproduce nada y con el método play() no encuentro como hacer para que simplemente muestre la forma geométrica sin más.
    Muchas gracias.

    • Leo Martínez says:

      Ya lo he encontrado. Se puede usar el método wait() especificando la duración en segundos que por defecto es 1 segundo tal y como viene en constant.py

      un saludo

  4. Leo Martínez says:

    ¿Podría explicar como funciona el método add() desde cero? No como se utiliza sino sun funcionamiento para poder entender como se hace.
    Muchas gracias

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.