2. Creating the first Scene

In manim’s root directory, create a new python file, called first_scene.py and write the following code in it:

The first line basically imports everything form (aptly named) big_ol_pile_of_manim_imports, this is done so that, as stated in docs inside the same file, “While creating animations for videos, it can be very nice to simply have all of the Mobjects, Animations, Scenes, etc. of manim available without having to worry about what namespace they come from.”

In the third line, FirstScene class inherits the scene class; I’ll get into the details of scene class later, but for all our practical purposes, all classes that consist of animation objects must inherit from scene class (is-a-relationship |i.e FirstScene is a scene ).
If you get the error: That scene is not in the script that probably means that you forgot to inherit scene class.
Similar to inheriting scene class, the construct method (line 4) is (sort of) mandatory. Think of it as __init__(self).

We created the scene inheriting the Scene class, we will push the animation to the scene from within the def construct method.

At line 9 we created an object circle of the Circle class (which will create a circle on the scene – didn’t see that coming, did you?) note that the circle is just created, it has not been pushed to the scene yet, that’ll be done later – in the next line.

Finally, in line 10 we pushed the circle to the scene.
self.wait(2) is self-explanatory. Just after the last frame to the rendered video.

Now, before running it, make sure your virtual environment is activated (if you’re using one). To render the video type

python3 -m manim first_scene.py FirstScene -p

You’ll be greeted with this sweet little video

If you’re getting ModuleNotFoundError: No module named 'cairo' error, it means that the venv is not active

Now we’ll look at the command used for rendering the video
The first part, python3 -m manim is calling the manim module, what’s interesting is the latter part, first_scene.py is the name of the python file in which you’re working, and FirstScene is the name of the class which is the subclass of Scene. In other words, FirstScene is the name of the class consisting of the construct method and animation object to be rendered.
-p is for preview. It’ll automatically play the rendered video in your default video player. Later on we’ll be using -pl which basically means to render the video in lower quality and subsequently open. Try removing this option and see what happens. (Hint: You’ll have to navigate to media directory)

The video that we’re gonna replicate in this series will consist of many classes, like FirstScene, that is, each scene will have its own separate class (which in turn will inherit Scene). All the scenes for a video will then be stitched together to make a video with some sort of video editing software.
Think of it as a theatre play — after each scene, the curtains are drawn Similarly, in this case, after each Scene the current class’ objective will end and a new class will begin.

Let’s create one more class in the same file so that there’s no room for doubt on what’s happening. Write the following code in first_scene.py.

And render the video by

python3 -m manim first_scene.py SecondScene -p

If you didn’t understand why we used SecondScene in the above command, re-read this post. If you did understand it, then you’re all set for the next post.

PS: If you’re wondering about line 17, don’t worry, we’ll explore that in the next post.

2 thoughts on “2. Creating the first Scene

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.