Manim for Animated Videos
Manim is a Python library used to create high-precision math and computer science animated videos.
Manim can be difficult to install so this tutorial will teach you how to run it more simply using a Docker container.
Setup
The first thing you're going to need is Docker. If you don't already have docker, you can download it here.
Pull the docker image for manim:
docker pull manimcommunity/manim
For more information, check out mainimcommunity/manim
There are several ways to interact with Manim. We're going to work with Jupyter Lab. For different methods check out this link.
First, make sure nothing is running on port 8888, then open a terminal window and run the following command to start up the manim docker container:
docker run -it -p 8888:8888 manimcommunity/manim jupyter lab --ip=0.0.0.0
You should see the following output:
file:///manim/.local/share/jupyter/runtime/jpserver-1-open.html
Or copy and paste one of these URLs:
http://1a7fb2e74b32:8888/lab?token=542f72b2a4f0f8eb58089e3ab795f43c1ad3730b0500af58
or http://127.0.0.1:8888/lab?token=542f72b2a4f0f8eb58089e3ab795f43c1ad3730b0500af58Cut and Paste one of the above links in your browser. You should see the following:
- Congratulations! You're now ready to use manim!
Example
Let's start with a simple example:
Open a new JupyterLab notebook and import all the contents of the manim library in a cell:
from manim import *
# To download image used in this example:
!wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/assets/logos/SN_favicon.pngObjects in manim are generally divided into three categories:
Scene
Mobject
Animation
To create a scene, we create a
Scene
object as follows:class SN(Scene):
def construct(self):
sn_logo = ImageMobject("SN_favicon.png")
text1 = Text("Skills" ).shift(DOWN).shift(DOWN).shift(DOWN)
text2 = Text("Skills Network", t2c={'[0:6]': PURPLE}).shift(DOWN).shift(DOWN).shift(DOWN)
self.add(sn_logo)
self.play(Write(text1))
self.play(ReplacementTransform(text1, text2))
self.wait(2)To run the scene, execute the following in the next code cell:
%manim SN
More generally,
%manim [name of class]
- where[name of class]
is the name of the scene class you defined.You should see a scene resembling the following:
For more information, check out the Manim docs.
Saving your work
If you shut down your Docker Container you will lose your work. So you're going to need to download the video and JupyterLab. The lab is straightforward; let's see how to download the video:
Go to the directory
./media/Jupiter/
Download the video, which is the name of the class followed by an
@
and the date:For example,
SN@2022-05-02@02-43-50.mp4 more generally [name of class]@[date].mp4
as seen in the image below:Finally, when you close your application you must make sure the container is closed, open a terminal window and type:
docker ps
You should see a list of your running containers, for example:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a7fb2e74b32 manimcommunity/manim "jupyter lab --ip=0.…" 3 days ago Up 3 days 0.0.0.0:8888->8888/tcp peaceful_kowalevskistop the container by typing
docker stop [name of container]
for this case it would bedocker stop peaceful_kowalevski