Manim in Jupyter¶
Working with manim in jupyter notebooks has several advantages:
code snippets and rendered outputs are close together
easy to iterate examples
easy to try different varieties of one scene in multiple cells
computation intensive code can be executed separately from the scenes
global Mobjects can be used in multiple scenes.
Simple Example¶
First, we need to import manim
[1]:
from manim import *
Manim Community v0.11.0
Now we build up our scene
[2]:
%%manim -v WARNING --progress_bar None -s -ql --disable_caching MyExample
class MyExample(Scene):
def construct(self):
m= ManimBanner()
self.add(m)
Note, that I use the following parameters:
-v WARNING
means that only warnings are shown in the log--progress_bar None
will not show the animation progress bar-s
will only show the last frame-ql
renders in low quality--disable_caching
will disable the manim caching systemMyExample
gives the scene name
for rendering a video, just remove the -s flag. To lower the resolution, you can use -r 400,200 (pixel values in x and y direction).
[3]:
%%manim -v WARNING --progress_bar None -r 400,200 --disable_caching HelloManim
class HelloManim(Scene):
def construct(self):
self.camera.background_color = "#ece6e2"
banner_large = ManimBanner(dark_theme=False).scale(0.7)
self.play(banner_large.create())
self.play(banner_large.expand())
We can define the parameters as a string params
and call this string by the cell magic with $params
[4]:
params = "-v WARNING -s -ql --disable_caching Example"
paramsSMALL = "-v WARNING -r 400,200 -s --disable_caching Example"
[5]:
%%manim $params
class Example(Scene):
def construct(self):
m= ManimBanner()
self.add(m)
Initializing Mobjects Outside the Class¶
In some cases, it might be convenient to define mobjects outside the Scene
class (e.g. for uncluttering or for speeding up the animation).
[6]:
m = ManimBanner()
[7]:
%%manim $paramsSMALL
class Example(Scene):
def construct(self):
m.scale(0.4 )
m.shift(1.5*UP)
self.add(m)
Because the mobject is manipulated in the class, the next cell might show some unexpected scaling and shifting:
[8]:
%%manim $paramsSMALL
class Example(Scene):
def construct(self):
m.scale(0.4)
m.shift(1.5*UP)
self.add(m)
To aviod this, it is better to add only a copy of these mobjects to scenes, and keep the originals untouched:
[9]:
m_reference = ManimBanner()
[10]:
%%manim $paramsSMALL
class Example(Scene):
def construct(self):
m = m_reference.copy()
m.scale(0.4)
m.shift(2*UP)
self.add(m)
[11]:
%%manim $paramsSMALL
class Example(Scene):
def construct(self):
m = m_reference.copy()
m.scale(0.4)
m.shift(2*UP)
self.add(m)
Defining Global Mobjects¶
When you have to build complex scenes, you might want to use parts of that scene for your next scene. That is possible with global variables, which can be accessed in any other scene.
[12]:
%%manim $paramsSMALL
class Example(Scene):
def construct(self):
stars= VGroup()
for i in range(0,20):
s= Star(color= random_bright_color(), fill_opacity=1).scale(0.8)
stars.add(s)
stars.arrange_in_grid()
self.add(stars)
global favoritstar
favoritstar = stars[9]
[13]:
%%manim $paramsSMALL
class Example(Scene):
def construct(self):
self.add(favoritstar)
Pre-Execute Slow Code¶
%%time
command will print the execution time of the cells.[14]:
%%time
np.random.seed(20)
steps = np.random.choice(a=[-1, 0, 1], size=(100000,1000))
stop = steps.cumsum(0)
end_points= stop[-1]/stop[-1].max()
end_pointsX = end_points[0:499]
end_pointsY = end_points[500:-1]
CPU times: user 2.11 s, sys: 751 ms, total: 2.86 s
Wall time: 2.86 s
[15]:
%%time
%%manim $params
class Example(Scene):
def construct(self):
radius= (end_pointsX*end_pointsX + end_pointsY * end_pointsY)**0.5
dots = VGroup()
for x,y,r in zip(end_pointsX, end_pointsY,radius):
c= interpolate_color(YELLOW, RED, r)
dots.add(Dot(color=c,point=[3*x,3*y,0]).scale(0.7))
self.add(dots)
CPU times: user 407 ms, sys: 3.82 ms, total: 411 ms
Wall time: 411 ms
Installing Plugins¶
plugins can be found at https://plugins.manim.community/
[16]:
!pip install manim-physics
Collecting manim-physics
Downloading manim_physics-0.2.3-py3-none-any.whl (9.9 kB)
Requirement already satisfied: manim>=0.6.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim-physics) (0.11.0)
Collecting pymunk<7.0.0,>=6.0.0
Downloading pymunk-6.2.0-cp38-cp38-manylinux2010_x86_64.whl (984 kB)
|████████████████████████████████| 984 kB 4.6 MB/s
Requirement already satisfied: setuptools in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (58.2.0)
Requirement already satisfied: click>=7.1 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (8.0.3)
Requirement already satisfied: scipy in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (1.7.1)
Requirement already satisfied: pydub in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (0.25.1)
Requirement already satisfied: moderngl<6.0.0,>=5.6.3 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (5.6.4)
Requirement already satisfied: pycairo<2.0,>=1.19 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (1.20.1)
Requirement already satisfied: mapbox-earcut<0.13.0,>=0.12.10 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (0.12.10)
Requirement already satisfied: pygments in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (2.10.0)
Requirement already satisfied: isosurfaces==0.1.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (0.1.0)
Requirement already satisfied: skia-pathops<0.8.0,>=0.7.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (0.7.1)
Requirement already satisfied: Pillow in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (8.3.2)
Requirement already satisfied: manimpango<0.4.0,>=0.3.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (0.3.1)
Requirement already satisfied: tqdm in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (4.62.3)
Requirement already satisfied: numpy<2.0,>=1.9 in /home/docs/.pyenv/versions/3.8.6/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (1.19.2)
Requirement already satisfied: decorator<6.0.0,>=5.0.7 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (5.1.0)
Requirement already satisfied: networkx<3.0,>=2.5 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (2.6.3)
Requirement already satisfied: rich>=6.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (10.12.0)
Requirement already satisfied: requests in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (2.26.0)
Requirement already satisfied: screeninfo<0.7.0,>=0.6.7 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (0.6.7)
Requirement already satisfied: watchdog in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (2.1.6)
Requirement already satisfied: moderngl-window<3.0.0,>=2.3.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (2.4.0)
Requirement already satisfied: cloup<0.8.0,>=0.7.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (0.7.1)
Requirement already satisfied: colour in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (0.1.5)
Requirement already satisfied: click-default-group in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from manim>=0.6.0->manim-physics) (1.2.2)
Collecting cffi>1.14.0
Downloading cffi-1.15.0-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (446 kB)
|████████████████████████████████| 446 kB 60.1 MB/s
Collecting pycparser
Downloading pycparser-2.20-py2.py3-none-any.whl (112 kB)
|████████████████████████████████| 112 kB 77.5 MB/s
Requirement already satisfied: glcontext<3,>=2 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from moderngl<6.0.0,>=5.6.3->manim>=0.6.0->manim-physics) (2.3.4)
Requirement already satisfied: pyrr<1,>=0.10.3 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from moderngl-window<3.0.0,>=2.3.0->manim>=0.6.0->manim-physics) (0.10.3)
Requirement already satisfied: pyglet<2,>=1.5.8 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from moderngl-window<3.0.0,>=2.3.0->manim>=0.6.0->manim-physics) (1.5.21)
Requirement already satisfied: commonmark<0.10.0,>=0.9.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from rich>=6.0->manim>=0.6.0->manim-physics) (0.9.1)
Requirement already satisfied: colorama<0.5.0,>=0.4.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from rich>=6.0->manim>=0.6.0->manim-physics) (0.4.4)
Requirement already satisfied: certifi>=2017.4.17 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from requests->manim>=0.6.0->manim-physics) (2021.10.8)
Requirement already satisfied: idna<4,>=2.5 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from requests->manim>=0.6.0->manim-physics) (3.3)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from requests->manim>=0.6.0->manim-physics) (1.26.7)
Requirement already satisfied: charset-normalizer~=2.0.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from requests->manim>=0.6.0->manim-physics) (2.0.7)
Requirement already satisfied: multipledispatch in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.11.0/lib/python3.8/site-packages (from pyrr<1,>=0.10.3->moderngl-window<3.0.0,>=2.3.0->manim>=0.6.0->manim-physics) (0.6.0)
Requirement already satisfied: six in /home/docs/.pyenv/versions/3.8.6/lib/python3.8/site-packages (from multipledispatch->pyrr<1,>=0.10.3->moderngl-window<3.0.0,>=2.3.0->manim>=0.6.0->manim-physics) (1.15.0)
Installing collected packages: pycparser, cffi, pymunk, manim-physics
Successfully installed cffi-1.15.0 manim-physics-0.2.3 pycparser-2.20 pymunk-6.2.0
[17]:
%%manim -v WARNING --progress_bar None -qm --disable_caching Example
from manim_physics import *
class Example(SpaceScene):
def construct(self):
circle = Dot(radius=1).shift(1.5*LEFT+3*UP)
rect = Square(color=YELLOW, fill_opacity=1)
ground = Line([-4, -3.5, 0], [4, -3.5, 0])
wall1 = Line([-4, -3.5, 0], [-4, 3.5, 0])
wall2 = Line([4, -3.5, 0], [4, 3.5, 0])
walls = VGroup(ground, wall1, wall2)
self.add(walls)
self.add(rect, circle)
self.make_rigid_body(rect, circle)
self.make_static_body(walls)
self.wait(5)