from manim import *
class SquareToCircle(Scene):
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.flip(RIGHT) # flip horizontally
square.rotate(-3 * TAU / 8) # 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
$ manim scene.py SquareToCircle -pql
# scene.py 指定执行哪个文件
# SqeuareToCircle 指定了渲染哪个场景, 因为一个文件中可能有很多个场景
# 参数含义
-p: 当manim渲染完成时, 播放场景
-f: 当渲染完成时, 打开渲染视频的目录
-ql: 渲染低质量视频(low quality), 480p, 15fps
-qm: medium quality
-qk: 4k quality
-qh: 高质量(high quality), 1080p, 60fps
-s: 输出最后一帧
# 三要素
1. mathematical object (mobject)
2. animation
3. scene
class CreatingMobjects(Scene):
def construct(self):
circle = Circle()
self.add(circle)
self.wait(1)
self.remove(circle)
self.wait(1)
通过调用add方法, 将Circle对象展示在屏幕中, 并用remove方法将其删除
class Shapes(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)
manim创建一个对象时, 会赋予一个随机颜色, 并且将其放在场景中央——也即坐标轴中央.
ORIGIN, UP, DOWN, LEFT, RIGHT这些向量方向常量定义在 constants 模块中
除了shift, 还有move_to, next_to, align_to等方法来放置对象
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)
class MobjectStyling(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(circle, square, triangle)
self.wait(1)
add方法添加对象的顺序, 决定了对象展示的顺序
manim的核心是动画, 可以通过调用play方法来添加动画
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)
# mobjects 任何可变得属性都能动画化
class ApplyMethodExample(Scene):
def construct(self):
square = Square().set_fill(RED, opacity=1.0)
self.add(square)
# animate the change of color
self.play(ApplyMethod(square.set_fill, WHITE))
self.wait(1)
# animate the change of position
self.play(ApplyMethod(square.shift, UP))
self.wait(1)
ApplyMethod 接收一个位置参数, 这个参数是一个用来展示动画的方法(例如square.set_fill), 并且还接收任意多个变长参数, 作为实参传给第一个参数
class RunTime(Scene):
def construct(self):
square = Square()
self.add(square)
self.play(ApplyMethod(square.shift, UP), run_time=3)
self.wait(1)
默认情况下, play方法的动画播放疫苗, run_time参数可以改变动画时长