头文件引入
导入manim命名空间
from manim import *
manim基本结构
这是一个最基本的manim结构,格式:
from manim import *
class 类的名字(Scene):
def construction(self):
类名叫做BaseFrame,传入一个场景Scene,并且包含一个construct()方法,传入self对象。self.wait()就是播放一段静默的动画(默认1秒)
class BaseFrame(Scene): def construct(self): self.wait()
例1-创建一个Circle对象并显示
类名:CreateCircle
涉及的新内容:Circle()
set_fill()
Create()
首先创建一个Circle()对象:circle
然后设置circle对象的颜色与透明度,
然后调用self.play()方法,传入animate类型的参数,即可渲染出一段动画。
class CreateCircle(Scene): def construct(self): circle = Circle() # 创建了一个Circle对象:circle circle.set_fill(PINK, opacity=0.5) # set the color and transparency self.play(Create(circle)) # show the circle on screen
编译
终端(控制台/PowerShell等)cd到.py文件所在目录,
命令格式manim -pql XXX.py ClassName
XXX.py 填入你编写的文件名
ClassName 填入类名
例如,你的文件名是sc1.py,你想渲染里面的CreateCircle类,
即:manim -pql sc1.py CreateCircle
例2-正方形变圆形
类名:SquareToCircle
涉及的新内容:Square()
rotate()
Transform()
FadeOut()
创建一个圆、一个正方形,将正方形旋转四分之PI的角度。
此时circle和square都没有被创建出来。
调用self.play()将正方形展示出来,通过Transform方法将square对象的形状(包括位置)过渡到circle。
需要注意的是,此时的square对象仍是他自己,主体对象没有改变。circle对象相当于提供了一个模版,此时circle没有被生成出来!
最后通过FadeOut()动画方法淡出square对象(而不是circle,因为circle根本就没有在场景中,只是square变成了circle的样子罢了)
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.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
例3-同时生成两个图案
类名:SquareAndCircle
涉及的新内容:next_to()
前几行代码准备了一个圆形circle和一个正方形square,尚未放到场景中。
调用square.next_to(circle, RIGHT, buff=0.5)
,将square对象挪到circle对象的RIGHT side,间隔0.5个单位。
另外注意,manim的坐标是以屏幕为中心的笛卡尔直角坐标。
我们可以在self.play()方法中置入多组动画方法参数,同时执行动画。
本案例中的:self.play(Create(circle), Create(square))
class SquareAndCircle(Scene): def construct(self): circle = Circle() # create a circle circle.set_fill(PINK, opacity=0.5) # set the color and transparency square = Square() # create a square square.set_fill(BLUE, opacity=0.5) # set the color and transparency square.next_to(circle, RIGHT, buff=0.5) # set the position self.play(Create(circle), Create(square)) # show the shapes on screen
例4-正方形变圆形播放动画
类名:AnimatedSquareToCircle
涉及的新内容:.animate.
ReplacementTransform()
class AnimatedSquareToCircle(Scene): def construct(self): circle = Circle() # create a circle square = Square() # create a square #Play_Scripts_Start self.play(Create(square)) # show the square on screen self.play(square.animate.rotate(PI / 4)) # 旋转图形,参数是弧度制 self.play( ReplacementTransform(square, circle) ) # transform the square into a circle self.play( circle.animate.set_fill(PINK, opacity=0.5) ) # color the circle on screen #Play_Scripts_End
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· c# 半导体/led行业 晶圆片WaferMap实现 map图实现入门篇