1. Manim in Jupyter¶
Latest update : 18.6.2021
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.
more can bee seen in chapter 4
Simple Example¶
First, we need to import manim
[1]:
from manim import *
Manim Community v0.9.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.18 s, sys: 978 ms, total: 3.16 s
Wall time: 3.16 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 410 ms, sys: 2.17 ms, total: 412 ms
Wall time: 413 ms
Installing Plugins¶
plugins can be found at https://plugins.manim.community/
[16]:
!pip install manim-rubikscube
Collecting manim-rubikscube
Downloading manim_rubikscube-0.1.0-py2.py3-none-any.whl (7.5 kB)
Requirement already satisfied: manim in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim-rubikscube) (0.9.0)
Collecting kociemba-manim-rubikscube
Downloading kociemba_manim_rubikscube-0.0.1-py3-none-any.whl (33.3 MB)
|████████████████████████████████| 33.3 MB 4.7 MB/s
Requirement already satisfied: pydub in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (0.25.1)
Requirement already satisfied: moderngl<6.0.0,>=5.6.3 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (5.6.4)
Requirement already satisfied: click>=7.1 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (8.0.1)
Requirement already satisfied: colour in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (0.1.5)
Requirement already satisfied: Pillow in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (5.4.1)
Requirement already satisfied: requests in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (2.26.0)
Requirement already satisfied: click-default-group in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (1.2.2)
Requirement already satisfied: setuptools in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (57.4.0)
Requirement already satisfied: networkx<3.0,>=2.5 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (2.6.2)
Requirement already satisfied: manimpango<0.4.0,>=0.3.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (0.3.0)
Requirement already satisfied: moderngl-window<3.0.0,>=2.3.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (2.4.0)
Requirement already satisfied: screeninfo<0.7.0,>=0.6.7 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (0.6.7)
Requirement already satisfied: pygments in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (2.9.0)
Requirement already satisfied: decorator<6.0.0,>=5.0.7 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (5.0.9)
Requirement already satisfied: watchdog in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (2.1.3)
Requirement already satisfied: pycairo<2.0,>=1.19 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (1.20.1)
Requirement already satisfied: tqdm in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (4.62.0)
Requirement already satisfied: cloup<0.8.0,>=0.7.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (0.7.1)
Requirement already satisfied: mapbox-earcut<0.13.0,>=0.12.10 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (0.12.10)
Requirement already satisfied: numpy<2.0,>=1.9 in /home/docs/.pyenv/versions/3.8.6/lib/python3.8/site-packages (from manim->manim-rubikscube) (1.19.2)
Requirement already satisfied: scipy in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (1.7.1)
Requirement already satisfied: rich>=6.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from manim->manim-rubikscube) (10.6.0)
Requirement already satisfied: glcontext<3,>=2 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from moderngl<6.0.0,>=5.6.3->manim->manim-rubikscube) (2.3.4)
Requirement already satisfied: pyglet<2,>=1.5.8 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from moderngl-window<3.0.0,>=2.3.0->manim->manim-rubikscube) (1.5.18)
Requirement already satisfied: pyrr<1,>=0.10.3 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from moderngl-window<3.0.0,>=2.3.0->manim->manim-rubikscube) (0.10.3)
Requirement already satisfied: multipledispatch in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from pyrr<1,>=0.10.3->moderngl-window<3.0.0,>=2.3.0->manim->manim-rubikscube) (0.6.0)
Requirement already satisfied: colorama<0.5.0,>=0.4.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from rich>=6.0->manim->manim-rubikscube) (0.4.4)
Requirement already satisfied: commonmark<0.10.0,>=0.9.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from rich>=6.0->manim->manim-rubikscube) (0.9.1)
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->manim-rubikscube) (1.15.0)
Requirement already satisfied: charset-normalizer~=2.0.0 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from requests->manim->manim-rubikscube) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from requests->manim->manim-rubikscube) (2021.5.30)
Requirement already satisfied: idna<4,>=2.5 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from requests->manim->manim-rubikscube) (3.2)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/docs/checkouts/readthedocs.org/user_builds/flyingframes/envs/v0.9.0/lib/python3.8/site-packages (from requests->manim->manim-rubikscube) (1.26.6)
Installing collected packages: kociemba-manim-rubikscube, manim-rubikscube
Successfully installed kociemba-manim-rubikscube-0.0.1 manim-rubikscube-0.1.0
[17]:
%%manim -v WARNING --progress_bar None -ql --disable_caching Example
from manim_rubikscube import *
class Example(ThreeDScene):
def construct(self):
cube = RubiksCube().scale(0.6)
self.add(cube)
self.set_camera_orientation(phi=50*DEGREES, theta=160*DEGREES)
self.renderer.camera.frame_center = cube.get_center()
self.play(CubeMove(cube, "F"))
self.play(CubeMove(cube, "U2"), run_time=2)
self.play(CubeMove(cube, "R'"))
self.wait(0.5)