Manim学习记录 Day1
JEREZW
2021年10月25日 19:05
收录于文集
共1篇

First day to learn Manim...

(To prepare my GRE, decide to record it in English)

Start from scratch!


A quick start!

Make a directory called learn and then make a python file scene.py

Write the code below in scene,py

代码块
Python
自动换行
复制代码
from manim import *


class SquareToCircle(Scene):#define a scene named SquareToCircle
    def construct(self):
        circle = Circle()  # create a circle
        circle.set_fill(PINK, opacity=0.5)  # set color and transparency

        square = Square()  # create a square
        square.rotate(PI / 4)  # rotate a certain amount

        self.play(Create(square))  # animate the creation of the square
        self.play(Transform(square, circle))  # interpolate the square into the circle
        self.play(FadeOut(square))  # fade out animation
复制成功

Then play it with command line(for mac)

代码块
Python
自动换行
复制代码
manim -pql scene.py SquareToCircle
复制成功

-p means play it

-ql means the vedio quality is low, can be substituded by -qm(mediem) -qh(high) -qk(4k)

Then it will generate several files and directories automatically.

Here is the folder structure

代码块
Python
自动换行
复制代码
project/
├─scene.py
└─media
  ├─videos
  | └─scene
  |   ├─480p15
  |   | ├─SquareToCircle.mp4
  |   | └─partial_movie_files
  |   └─1080p60
  |     ├─SquareToCircle.mp4
  |     └─partial_movie_files
  ├─text
  └─Tex
复制成功

The vedio will be save as mp4 file, we can export it as gif file with the flag -i

Have a look at the animation!

squareToCircle.gif(quality medium 720p60)


Three important classes

In manim, we have 3 different concepts that you can orchestrate together to produce mathematical animations: the mathematical object (or mobject for short) the animation, and the scene

Each of these three concepts is implemented in manim as a separate class: the , and  classes.


Mobjects

    How to create

Mobjects is a very basic concept for manim animation.

Each class that derives from  represents an object that can be displayed on the screen. For example, simple shapes such as , and  are all mobjects. More complicated constructs such as , or  are mobjects as well.

We cannot directly play a  on the scene for the  class is an abstract base class of all other mobjects. It doesn't has a pre-determined visual shape.

So, mobjects is just a general concept. When it comes to the real case, we often use its derived class, for example the VMobjects, which V represents vector.

If we want a mobjects to be displayed, skills below can be useful

代码块
Python
自动换行
复制代码
from manim import *

class CreatingMobjects(Scene):
    def construct(self):
        circle = Circle()
        self.add(circle)
        self.wait(1)
        self.remove(circle)
        self.wait(1)
复制成功

We use the methods add() and remove() to display a single mobject Circle.

Here is the output GIF:

CreatingMobjects.gif(quality low 480p15)

    How to place

we have several method for position

For example shift() 

代码块
Python
自动换行
复制代码
from manim import *

class PlaceShapesWithShift(Scene):
    def construct(self):
        circle = Circle()
        square = Square()
        triangle = Triangle()

        circle.shift(LEFT)
        square.shift(UP)
        triangle.shift(RIGHT)

        self.add(circle, square, triangle)
        self.wait(1)
复制成功

output

PlaceShapesWithShift.gif

also we have other methods like next_to( ) move_to( ) align_to( )

Example

代码块
Python
自动换行
复制代码
from manim import *

class MobjectPlacement(Scene):
    def construct(self):
        circle = Circle()
        square = Square()
        triangle = Triangle()

        # place the circle two units left from the origin
        circle.move_to(LEFT * 2)
        # place the square to the left of the circle
        square.next_to(circle, LEFT)
        # align the left border of the triangle to the left border of the circle
        triangle.align_to(circle, LEFT)

        self.add(circle, square, triangle)
        self.wait(1)
复制成功

and the output

MobjectPlacement.gif

    Add Style

We can apply different styles on the mobjects with many methods.

set_stroke( )  set_fill( ) ......

代码块
Python
自动换行
复制代码
from manim import *

class MobjectZOrder(Scene):
    def construct(self):
        circle = Circle().shift(LEFT)
        square = Square().shift(UP)
        triangle = Triangle().shift(RIGHT)

        circle.set_stroke(color=GREEN, width=20)
        square.set_fill(YELLOW, opacity=1.0)
        triangle.set_fill(PINK, opacity=0.5)

        self.add(triangle, square, circle)
        self.wait(1)
复制成功

Notice here the add(triangle, square, circle) has sequence.


Animation

At the heart of manim is animation. Generally, we can add an animation to our scene by calling the  method.

代码块
Python
自动换行
复制代码
from manim import *

class SomeAnimations(Scene):
    def construct(self):
        square = Square()
        self.add(square)

        # some animations display mobjects, ...
        self.play(FadeIn(square))

        # ... some move or rotate mobjects around...
        self.play(Rotate(square, PI/4))

        # some animations remove mobjects from the screen
        self.play(FadeOut(square))

        self.wait(1)
复制成功

Animating methods

Any property of a mobject that can be changed can be animated. In fact, any method that changes a mobject’s property can be used as an animation, through the use of .

代码块
Python
自动换行
复制代码
from manim import *

class AnimateExample(Scene):
    def construct(self):
        square = Square().set_fill(RED, opacity=1.0)
        self.add(square)

        # animate the change of color
        self.play(square.animate.set_fill(WHITE))
        self.wait(1)

        # animate the change of position and the rotation at the same time
        self.play(square.animate.shift(UP).rotate(PI / 3))
        self.wait(1)
复制成功

 is a property of all mobjects that animates the methods that come afterward. For example,  sets the fill color of the square, while  animates this action.


That's all for taday&#​39;s record. Actually I learned more than what I post here, but it's too much and I have to learn something else today Leave it to next share!

By the way, the progress of my learning is a little bit fast. I draftly know how to display euler's angles by Manim. But of course, still too many details not known.