Manim 学习笔记(一)--常用的几个函数和操作

常用的一些操作

【注:随版本升级,有些操作和指令会有改动】

from manim import * 从 manim 中导入了需要用到的模块

class Hello_World(Scene) 创建一个继承自Scene的一个类;

def construct(self) construct方法对于manim很特殊,manim调用这个方法来创建动画。所以基本上这个方法是在运行manim时必不可少的;

x=Text("内容",color=颜色) 创建一个对象,内容为“内容”,颜色为“颜色”

self.play(Write(name) 动画输出 name 对象的内容

self.wait(x) 等待 x 秒

x=Rectangle(color=颜色) 创建一个矩形,颜色为“颜色”

x.surround(y) 对象 x 包裹对象 y

x=VGroup(a,b) 合并 a,b 到一个群组中

x.scale() 设置缩放大小

x.set_color 设置颜色

x.set_height 设置尺寸

self.play(FadeIn(x)) 让 x 对象渐入显示

self.play(FadeOut(x)) 让 x 对象渐出

self.play(Transform(x,y)) 从 x 渐变到 y

self.ApplyMethod(x.属性,number)) 把 x 的某个属性放大 number 倍,x 可以是一个群组

x=Circle() /Annulus()/Ellipse()/Square()/Triangle()/Polygon()/Line() 画圆、圆环、椭圆、方形、三角形、多边形、直线

属性:color fill_color fill_opacity height width 颜色 内部填充颜色 不透明度 高度 宽度

或用 x.set_xxxx

self.add(x) 对象 x 直接出现

self.play(GrowFromCenter(x)) 让 x 对象从中间一步步生成

self.play(FadeInFromDown(x)) 让 x 对象从下面渐入

self.play(FadeInFrom(x,vector)) 让 x 对象从 vector 方向渐入

x=Line([x1,y1,z1],[x2,y2,z2]) 一条起点 (x1,y1,z1) ,终点 (x2,y2,z2)

x=Annulus(inner_radius,outer_radius) 内半径,外半径

x.shift(vector) 接受一个向量vector,图形初始位置的平移,其中vector可以是 DOWN,LEFT,RIGHT,UP 常量,并可以*n

LEFT = np.array([-1,0,0])

RIGHT = np.array([1,0,0])

UP = np.array([0,1,0])

DOWN = np.array([0,-1,0])

IN = np.array([0,0,-1])

OUT = np.array([0,0,1])

单位长度 一个默认的圆(Circle)其半径为一个单位,默认的动画场景高度为8个单位

B.next_to(A,vecor) 物体 B 在 vector(如:np.array([1,2,0]),UP) 定义方向上挨着 A

B.rotate(degree) 让B旋转 degree(弧度制)

self.play(ApplyMethod(A.shift, vector)) 直接使用shift等方式改变的位置是没有动画的,如果要动画的话可以使用这个来实现A以向量vector进行移动的动画

self.play(A.animate.shift(vector)) 功能同上,并且可以多种动画同时连接,如:self.play(A.animate.shift(vector).scale(2).move_to(vector)) 此时多个动画是同时执行的

x.to_edge(UP/RIGHT/DOWN/LEFT) 到边缘

x.get_corner(vector) x 的 vector 角落

x.match_color(y) 让 x 和 y 颜色一样

x.bg=BackGroundRectangle(x,属性) x 背景的属性设置

x.rotate(TAU*(n/360)) 将 x 旋转 n 度 ,其中, TAU=2*np.pi

x.set_color_by_gradient(color1,color2,...) x 颜色按设定渐变

书写 LaTeX 的时候,用 x=Tex("") 等价于 x=Text("$$"),其中,‘\’ 的意义成为空格,两个'' 才表示后跟公式

【1】生成480p Gif动画[命令行]:manim test08.py -pql -i
【2】生成480p视频动画[命令行]:manim test08.py -pql
【3】生成1080p视频动画[命令行]:manim test08.py -p

Tips

1. Constants

ORIGIN = np.array((0., 0., 0.))
UP = np.array((0., 1., 0.))
DOWN = np.array((0., -1., 0.))
RIGHT = np.array((1., 0., 0.))
LEFT = np.array((-1., 0., 0.))
IN = np.array((0., 0., -1.))
OUT = np.array((0., 0., 1.))
X_AXIS = np.array((1., 0., 0.))
Y_AXIS = np.array((0., 1., 0.))
Z_AXIS = np.array((0., 0., 1.))
# Useful abbreviations for diagonals
UL = UP + LEFT
UR = UP + RIGHT
DL = DOWN + LEFT
DR = DOWN + RIGHT
TOP = FRAME_Y_RADIUS * UP
BOTTOM = FRAME_Y_RADIUS * DOWN
LEFT_SIDE = FRAME_X_RADIUS * LEFT
RIGHT_SIDE = FRAME_X_RADIUS * RIGHT

2. Table

table = r"""
\begin{table}[]
\begin{tabular}{cc}\hline
\textbf{Grade} & \textbf{Proportion}
\\ \hline
A & ? \\
B & ? \\
C & ? \\
D & ?
\\ \hline
\end{tabular}
\end{table}
"""
table = TexText(table)

3. 位置

所有可以用方向常量作为参数的方法都可以用具体的坐标np.array([x, y, z])来代替
3.1 到某个角落
mob.to_corner(UL)
3.2 到某个边
mob.to_edge(UP)
3.3 get系列
用get系列方法来完成位置的定位,具体有get_left, get_right, get_bottom, get_top

arrow = Arrow(
mob1.get_bottom(),
mob2.get_top(),
)

3.4 move_to

# specific object
mob1.move_to(mob2)
# specific coordinate
mob1.move_to(np.array([x, y, z]))

3.5 align_to
会将 mob1 垂直移动,顶部与 mob2 的上边缘对齐

mob1.align_to(mob2, UP)

3.6 next_to
mob1在mob2的右侧

mob1.next_to(mob2, RIGHT)

3.7 shift

mob1.shift(2 * RIGHT + LEFT)

4. 改变大小

4.1 scale
全方位改变大小

mob1.scale(0.5)

4.2 stretch
沿某一方向拉伸

mob1.stretch(0.5, 0)

5. 动画

5.1 animate
任何方法均可用animate动起来

self.play(
mob1.animate.set_fill(BLUE, opacity=1.)
)

5.2 替代动画

self.play(
# a
FadeTransform(mob1, mob2),
# b
ReplacementTransform(mob1, mob2),
# c
TransformForMatchingTex(mob1.get_part_by_tex("x").copy(), mob2)
)

5.3 显示渲染过程

self.play(
# common objects
ShowCreation(mob1.set_color(RED))
# maths equations
Write(mob2)
)

5.4 其他常用

self.play(
FadeIn(...),
FadeOut(...),
Rotate(mob1, 180 * DEGREES)
)

6. 基础Mobject

6.1 矩形

rect = Rectangle(width, height)

6.2 环绕矩形

rect = SurroundingRectangle(mob1, color=RED)

6.3 圆

circle = Circle(
radius = ...,
stroke_color = ...,
)

6.4 圆柱

cylinder = Cylinder(
radius=0.2,
height=0.9,
u_range=...,
v_range=...,
opacity=1.0
)

6.5 2D坐标

axes = Axes(
x_range=(-5, 5, 1),
y_range=(0, 1.5, 1),
width=8,
height=4,
axis_config={
"include_tip": True,
},
).set_color(WHITE)

坐标label

labels = axes.get_axis_labels(x_label_tex='x', y_label_tex='y')

坐标画图

cal_formula = lambda x: 2 / math.sqrt(math.pi) * math.e ** (-x ** 2 / 4)
dis = axes.get_graph(cal_formula, color=BLUE)

Riemann 矩形

dxs = [1, 0.2, 0.02]
rects = [axes.get_riemann_rectangles(dis, x_range=[-5, 5, dx]) for dx in dxs]
self.play(
FadeIn(rects[0]),
)

获取某个点坐标

# 点移到到原点处
dot.move_to(axes.c2p(0, 0))

6.6 3D坐标

#3D基础坐标实现
axes = ThreeDAxes(
x_range=[-4, 4, 1],
y_range=[-6, 6, 1],
z_range=[-6, 6, 1],
x_length=8,
y_length=6,
z_length=6,
)

固定在画面中,因为3D画面中会进行旋转等操作

mob1.fix_in_frame()

绘画平面

graph = axes.get_parametric_curve(
lambda t: np.array([t, 0, math.e ** (-t ** 2)]),
t_range = [-4, 4],
color=BLUE
)

6.7 Arrow

arrow = Arrow(
[x1, y1, z1],
[x2, y2, z2]
)

6.8 Line

line = Line(
[-0.2, 0, math.e ** (- 0.3 ** 2)],
[0, 0, 0.915]
).set_color(YELLOW)

6.9 Brace

radius_brace = Brace(radius_line, buff = SMALL_BUFF).set_color(BLUE)

6.10 Dot

dot = Dot(radius=0.5,color=RED)

7. 设置颜色

7.1 填充

# do not forget the opacity
mob1.set_fill(RED, opacity=0.5)

7.2 一般物品

mob1.set_color(GREEN)

7.3 公式特殊部分配色

equ = Tex("a^2 = b^2 + c^2", isolate=["a^2", "b^2"])
equ.get_color_by_tex("a^2", BLUE)

7.4 渐变色

color_gradient
colors = color_gradient(colors, len(group))
for (mob, color) in zip(group, colors):
mob.set_color(color)
set_color_by_gradient
group.set_color_by_gradient(RED, BLUE)

8. 基础画面设置

8.1 清空
self.clear()
8.2 保留画面
self.save_state()
8.3 恢复保留的画面
self.restore()
8.4 Notebook开发
self.embed()
8.5 touch

touch()
# 基础命令
# 按住z来进行缩放
# 按住d更换相机视角
# 按住r重置视角
# 按住q来退出
# 按住s进行移动

8.6 直接加入画面中
self.add(mob1, mob2)
8.7 画面短暂停止

# 停留2s
self.wait(2)

9. 相机

phi角增大即为z轴越往里面去,theta增大即xoy平面往里面转

9.1 设置初始化角度

frame = self.camera.frame
frame.set_euler_angles(
theta=120 * DEGREES,
phi=70 * DEGREES,
)

9.2 相机改变动画

self.play(
# 在过渡期间移动相机帧
frame.increment_phi, 5 * DEGREES,
frame.increment_theta, 10 * DEGREES,
run_time=3
)

9.3 Zoom in

# Zoom in
self.play(
# 越小放的越大
frame.set_width, graph_total.get_width() * 2.0,
# Move the camera to the object
frame.move_to, graph_total
)

10. Mobject方法

10.1 save_state
frame.save_state()
10.2 restore
frame.restore()
10.3 VGroup

group = VGroup(mob1, mob2)
# or
group = VGroup(*[mob1, mob2])

10.4 always_redraw

# 接受一个函数,以及它的参数
brace = always_redraw(Brace, square, UP)

几个常用的教程:

系列教程manim使用(一) manim基本组件
系列教程做出高逼格的数学动画——一起来学manim·入门篇(一)
manim边学边做--MathTex
利用Python中的Manim进行数学绘画和创作

posted @   近我者赤  阅读(780)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· 10亿数据,如何做迁移?
· 推荐几款开源且免费的 .NET MAUI 组件库
· c# 半导体/led行业 晶圆片WaferMap实现 map图实现入门篇
点击右上角即可分享
微信分享提示