Learning by Example¶
You create videos in manim by writing Scene instances.
example_scenes.py contains a few simple ones that we can use to learn about
manim. For instance, take SquareToCircle.
1 2 3 4 5 6 7 8 9 10 11 | class SquareToCircle(Scene):
def construct(self):
circle = Circle()
square = Square()
square.flip(RIGHT)
square.rotate(-3 * TAU / 8)
circle.set_fill(PINK, opacity=0.5)
self.play(ShowCreation(square))
self.play(Transform(square, circle))
self.play(FadeOut(square))
|
construct() specifies what is displayed on the screen
when the Scene is rendered to video. You can render a
Scene by running extract_scene.py. Run python
extract_scene.py -h to see how it’s used.
> python extract_scene.py -h
usage: extract_scene.py [-h] [-p] [-w] [-s] [-l] [-m] [-g] [-f] [-t] [-q] [-a]
[-o OUTPUT_NAME] [-n START_AT_ANIMATION_NUMBER]
[-r RESOLUTION] [-c COLOR] [-d OUTPUT_DIRECTORY]
file [scene_name]
positional arguments:
file path to file holding the python code for the scene
scene_name Name of the Scene class you want to see
optional arguments:
-h, --help show this help message and exit
-p, --preview
-w, --write_to_movie
-s, --show_last_frame
-l, --low_quality
-m, --medium_quality
-g, --save_pngs
-f, --show_file_in_finder
-t, --transparent
-q, --quiet
-a, --write_all
-o OUTPUT_NAME, --output_name OUTPUT_NAME
-n START_AT_ANIMATION_NUMBER, --start_at_animation_number START_AT_ANIMATION_NUMBER
-r RESOLUTION, --resolution RESOLUTION
-c COLOR, --color COLOR
-d OUTPUT_DIRECTORY, --output_directory OUTPUT_DIRECTORY
The most common flags are -p, to automatically play the generated video,
-l, to render in lower quality in favor of speed, and -s, to show the
last frame of the Scene for faster development. Run
python extract_scene.py example_scenes.py SquareToCircle -pl to produce a
file called SquareToCircle.mp4 in the media directory that you have configured,
and automatically play it.
Let’s step through each line of the Scene. Lines 3 and 4
instantiate a Circle and
Square, respectively. Both of these subclass
Mobject, the base class for objects in manim. Note
that instantiating a Mobject does not add it to the
Scene, so you wouldn’t see anything if you were to render
the Scene at this point.
3 4 | circle = Circle()
square = Square()
|
Lines 5, 6, and 7 apply various modifications to the mobjects before animating
them. The call to flip() on line 5 flips the
Square across the RIGHT vector. This is equivalent
to a refection across the x-axis. Then the call to
rotate() on line 6 rotates the
Square 3/8ths of a full rotation counterclockwise.
Finally, the call to set_fill() on line 7 sets
the fill color for the Circle to pink, and its
opacity to 0.5.
5 6 7 | square.flip(RIGHT)
square.rotate(-3 * TAU / 8)
circle.set_fill(PINK, opacity=0.5)
|
Line 9 is the first to generate video.
ShowCreation,
Transform, and
FadeOut are
Animation instances. Each
Animation takes one or more
Mobject instances as arguments, which it animates
when passed to play(). This is how video is typically
created in manim. Mobject instances are automatically
added to the Scene when they are animated. You can add a
Mobject to the Scene manually
by passing it as an argument to add().
9 10 11 | self.play(ShowCreation(square))
self.play(Transform(square, circle))
self.play(FadeOut(square))
|
ShowCreation draws a
Mobject to the screen,
Transform morphs one
Mobject into another, and
FadeOut fades a
Mobject out of the Scene. Note
that only the first argument to Transform is
modified, and the second is not added to the Scene. After
line 10 is executed square is a Square instance
with the shape of a Circle.