【】
安装
为了方便,使用了docker简化安装。3b1b github上介绍了其他的安装方法。
docker pull eulertour/manim:latest
3.创建manim的local repo(下载zip也可),将docker与本地的repo相关联
docker run -itv /本地/repo/绝对/路径:/root/manim eulertour/manim
4.测试动画
cd manim
python3 -m manim example_scenes.py SquareToCircle -l
使用
基本操作
1.编写python脚本test_scene.py
class Shapes(Scene):
一些简单的形状
def construct(self): #construct()<a href="/tag/fangfa/" target="_blank" class="keywords">方法</a>一般用于创建形状
circle = Circle()
square = Square()
line=Line(np.array([3,0]),np.array([5,0]))
triangle=Polygon(np.array([0,np.array([1,1,-1,0]))
self.add(line) #在画面中<a href="/tag/tianjia/" target="_blank" class="keywords">添加</a>静止形状
self.play(ShowCreation(circle)) #动画
self.play(FadeOut(circle))
self.play(GrowFromCenter(square))
self.play(Transform(square,triangle))</code></pre>
2.存储至本地manim根目录下
3.python3 -m manim test_scene.py SquareToCircle -p
big_ol_pile_of_manim_imports 存储了manim核心的模块
关于每个模块的具体使用可以参考,同时,在github界面使用搜索框也很ok。
各种形状
test_scene.py
class MoreShapes(Scene):
def construct(self):
circle = Circle(color=PURPLE_A)
square = Square(fill_color=GOLD_B,fill_opacity=1,color=GOLD_A)
square.move_to(UP+LEFT) #一个UP/LEFT单位默认为视频高度的1/8
circle.surround(square)
rectangle = Rectangle(height=2,width=3)
ellipse=Ellipse(width=3,height=1,color=RED)
ellipse.shift(2*DOWN+2*RIGHT)
pointer = CurvedArrow(2*RIGHT,5*RIGHT,color=MAROON_C)
arrow = Arrow(LEFT,UP)
arrow.next_to(circle,DOWN+LEFT)
rectangle.next_to(arrow,DOWN+LEFT)
ring=Annulus(inner_radius=.5,outer_radius=1,color=BLUE)
ring.next_to(ellipse,RIGHT)
self.add(pointer)
self.play(FadeIn(square))
self.play(Rotating(square),FadeIn(circle)) #同时<a href="/tag/xianshi/" target="_blank" class="keywords">显示</a>多个动画
self.play(GrowArrow(arrow))
self.play(GrowFromCenter(rectangle),GrowFromCenter(ellipse),GrowFromCenter(ring))</code></pre>
添加文字
test_scene.py
class AddingText(Scene):
#添加文字
def construct(self):
my_first_text=TextMobject("Writing with manim is fun")
second_line=TextMobject("and easy to do!")
second_line.next_to(my_first_text,DOWN)
third_line=TextMobject("for me and you!")
third_line.next_to(my_first_text,DOWN)
self.add(my_first_text,second_line)
self.wait(2) #wait()默认为停留1s
self.play(Transform(second_line,third_line))
self.wait(2)
second_line.shift(3*DOWN) #无过渡的改变位置
self.play(ApplyMethod(my_first_text.shift,3*UP)) #为位置移动的<a href="/tag/fangfa/" target="_blank" class="keywords">方法</a><a href="/tag/tianjia/" target="_blank" class="keywords">添加</a>动画<a href="/tag/xiaoguo/" target="_blank" class="keywords">效果</a>需要使用ApplyMethod()<a href="/tag/fangfa/" target="_blank" class="keywords">方法</a></code></pre>
改变文字
test_scene.py
class AddingMoreText(Scene):
#更多的文字效果
def construct(self):
quote = TextMobject("Imagination is more important than knowledge")
quote.set_color(RED)
quote.to_edge(UP)
quote2 = TextMobject("A person who never made a mistake never tried anything new")
quote2.set_color(YELLOW)
author=TextMobject("-Albert Einstein")
author.scale(0.75)
author.next_to(quote.get_corner(DOWN+RIGHT),DOWN)
self.add(quote)
self.add(author)
self.wait(2)
self.play(Transform(quote,quote2),ApplyMethod(author.move_to,quote2.get_corner(DOWN+RIGHT)+DOWN+2*LEFT))
self.play(ApplyMethod(author.scale,1.5)) #缩放动效
author.match_color(quote2)
self.play(FadeOut(quote))</code></pre>