Basic Python

The following things (beside classes and inheritance) should be the things you already know.

Variables

You can learn them in W3Schools.

1
2
3
4
5
6
7
a = 1        # Comments are written after a hash sign
b = 114.514
c = '1919'
d = a + b

print("Hello, World")
print(a)

Condition and Loop

if-elif-else statement. You can also learn them in W3Schools.

1
2
3
4
5
6
if a == 1:
print('a == 1')
elif a == 2:
print('a == 2')
else:
print('a != 1 and a != 2')

while loop. You can also learn them in W3Schools.

1
2
3
4
while a < 10:
a += 1
b += 1
d += a + b

for loop. You can also learn them in W3Schools.

1
2
3
arr = [1, 3, 6, 4, 2, 1]
for number in arr:
print(number)

Functions

Define and invoke a function. You can also learn them in W3Schools.

1
2
3
4
def f(x):
return x + 1

print(f(1))

Lambda expression. You can also learn them in W3Schools.

1
2
g = lambda x: x + 1
print(g(1))

Numpy and math functions. For more information of numpy, see here.

1
2
3
import numpy as np
def solve(a, b, c):
return (-b + np.sqrt(b * b - 4 * a * c)) / (2 * a)

Classes and Objects

Manim

You can see the official documentation to learn more about manim.

Make sure that you have already installed manim on your computer. You don’t need LaTeX for the following examples, however, you will need to install LaTeX in the future if you want to add math equations to your animation.

Here is an example code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from manim import *

class Test(Scene): # Every Manim class inherits from 'Scene'
# You can name it whatever you like
def construct(self):
# Your main code goes under the 'construct' function
circle = Circle() # This creates a 'Circle' object
# But the circle hasn't been added to the scene
self.add(circle) # This directly adds the circle to the scene
# You won't see any animation, though
square = Square() # This creates a 'Square' object
self.play(Create(square))
# This animates the creation of the square
self.wait(1) # This pauses the video for 1 second

Create a new file called manim_test.py under any folder you like, and run this command in the commandline:

1
manim manim_test.py Test -ql

After the program finishes execution, you should see a new folder called media under your project folder. The folder structure now should look like this:

1
2
3
4
5
6
7
8
9
10
your project
├─── media
│ ├─── videos
│ │ └─── manim_test
│ │ └─── 480p15
│ │ ├─── partial_movie_files
│ │ │ └─── ...
│ │ └─── Test.mp4
│ └─── images
└─── manim_test.py

The “Test.mp4” file under “480p15” folder is the video generated by manim from the code you just write. You can open it and see the animation you just created.

For those who are not familiar with command lines:

manim manim_test.py Test -ql is a typical command line command. A command is usually one line containing several words separated by spaces. The words are called “arguments”.

The first word, manim in this case, indicates the executable program this command is running. In this case, python already installed manim on your computer, so you can run it directly by typing the command.

Other words, manim_test.py and Test in this case, are arguments of the command. Depending on the specific format of the command, the format of arguments may differ. In the case of manim, the two arguments are the file name and class name of your code. Using manim_test.py and Test tells the program to find a class called Test in the manim_test.py file.

Finally, the last argument, -ql, starts with a dash (- symbol). These type of arguments are called “flags”. Flags usually indicates additional information provided to the executable file. In this case, -ql tells manim to generate the video in low quality.

Different executables have different rules for parsing the arguments. In most cases, if you want to check the details of one specific command, you can type the command with a --help argument and a help message where you can learn about the command will be displayed.

Geometric shapes

Here are some basic geometric shapes. The following code is written inside the construct function in whatever class you named it that inherits from manim’s Scene:

1
2
3
4
5
6
7
8
c = Circle(color = YELLOW)   # Create a yellow circle
s = Square(color = GREEN) # Create a green square
l = Line(start = LEFT, end = RIGHT) # Create a line from left to right
d = Dot(color = RED) # Create a red dot
self.add(c)
self.add(s)
self.add(l)
self.add(d)

This code will generate the following image:

img-1

Note that until you run the self.add(something) command, the shape is not on the screen although it is created and held by a variable. “Whether the shape is created” and “whether manim knows it is on the screen” is different. You can test this out by removing one or multiple self.add(something) lines to see the effect.

Every shape has their own constructor. You can also check out the manim reference for the details of these objects.

For example, you can construct a circle with given radius, color, and position:

1
2
3
4
5
circle = Circle() # This is the default circle
self.add(circle)
circle2 = Circle(2, color=YELLOW).shift(LEFT * 2 + UP)
# This is a circle with radius 2 and centered at (-2, 1)
self.add(circle2)

img-2

Circle, Square, Line, Dot, and many other shapes listed in the manim reference are inherited from a single class called Mobject. Mobject stands for “mathmatic objects”, which has some basic properties such as position, color, etc. that every object has in common.

Exercise

Can you create this image with manim? There are 3 requirements:

  1. The circle is white and hollow
  2. The square is green and filled with 0.8 opacity
  3. The circle passes through the square’s center, and the square’s corner lies on the circle’s center

img-3

Animations

Animations represents anything that “changes with time” on the screen. Animations are represented by objects inherited from manim’s Animation class.

Note that add and remove are not animations, since they won’t actually take up any time in the generated video - they are performed simultaneously.

self.play(animation) plays an animation in manim.

There are several animations that may be useful to you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## Create(mobject): Performs the creation of an object
c = Circle()
self.play(Create(c))

## FadeIn(mobject): Another way to create an object. This shows the process of the object fading in.
s = Square()
self.play(Create(s))

## Transform(mobject_1, mobject_2): Performs the transformation from `mobject_1` to `mobject_2`.
## After the execution of this animation, `mobject_1` is still on the screen while it holds the value of the previous `mobject_2`.
s2 = Square(color=YELLOW).shift(UP + RIGHT)
self.play(Transform(c, s2))
print(c in self.mobjects) # `self.mobjects` holds all objects currently on the screen
print(s2 in self.mobjects)

## ReplacementTransform(mobject_1, mobject_2): exactly the same as `Transform`, except that `mobject_1` is replaced by `mobject_2`.
## In other words, After transformation, `mobject_1` will no longer be on the screen.
c2 = Circle(color = GREEN).shift(DOWN + LEFT)
self.play(ReplacementTransform(s, c2))
print(s in self.mobjects)
print(c2 in self.mobjects)

## Uncreate(mobject): this is exactly the opposite of `Create`. It shows the reverse animation of the object being created.
## After this animation, `mobject` will no longer be on the screen.
self.play(Uncreate(c2))

## FadeOut(mobject): opposite of `FadeIn`. This animates the gradual fading out of the object.
self.play(FadeOut(c))

## self.wait(number): a special animation provided by manim. This animation does nothing but letting the screen stays the same for a given period of time. the `number` in `self.wait` is the number of seconds to wait.
self.wait(1) # waits for 1 second

Here is the video produced by the previous code:

For more information, you can check out the manim reference.

Now you are able to do a lot of animations. Try to experiment with manim and create the best animation!