【Python】Python視頻制作工具M(jìn)anim入門,基礎(chǔ)形狀詳細(xì)介紹
來源:Python數(shù)據(jù)之道 (ID:PyDataLab)作者:陽哥
01寫在前面
最近幾個(gè)月,我在微信視頻號「價(jià)值前瞻」和「Python數(shù)據(jù)之道」發(fā)布了一些視頻,有不少同學(xué)問到這些視頻是怎么做的,用什么工具制作的。
在文章 用 python 制作高逼格的數(shù)學(xué)動(dòng)畫 中,我跟大家介紹了 Manim 這個(gè)視頻制作工具,以及以及案例演示。
不少同學(xué)覺得這個(gè)工具不錯(cuò),問到有沒有完整的使用教程或者相關(guān)書籍。據(jù)我所知,目前應(yīng)該是沒有專門的書籍來介紹這個(gè)工具的。至于教程,不同版本的Manim有一部分文檔,其中 Manim社區(qū)版的文檔相對而言要完善些。
本次,我將基于 Manim社區(qū)版(Manim Community)給大家分享Manim入門的第一部分,基礎(chǔ)形狀的使用。
本次使用的版本為 Manim Community v0.14.0,文中介紹的基礎(chǔ)形狀如下:

02Manim的安裝與運(yùn)行
安裝
如何安裝社區(qū)版Manim,參見下面的官方鏈接:
https://docs.manim.community/en/stable/installation.html
如何運(yùn)行 Manim
用 Manim 繪制圖形,首先需要引入 Manim 庫,然后將需要繪制的內(nèi)容封裝到一個(gè) 類(class) 里面。
社區(qū)版的導(dǎo)入代碼如下:
from?manim?import?*
對于 編輯好的程序文件( XXXX.py 文件),需要在同一個(gè)文件夾下運(yùn)行命令來運(yùn)行程序,命令格式如下:
manim?-pql?XXXX.py?DemoSquare
上面的命令行中:
manim是社區(qū)版Manim運(yùn)行程序的主要標(biāo)志用語;p表示程序運(yùn)行后會(huì)進(jìn)行預(yù)覽(圖片或視頻);ql表示低質(zhì)量(quality low), 其他的選項(xiàng)有-ql,-qm,-qh,-qk, 分別表示 低質(zhì)量、正常質(zhì)量、高質(zhì)量、4K質(zhì)量;XXXX.py是py代碼文件;DemoSquare 是 py代碼文件中的一個(gè)類;
演示過程錄屏如下:

命令行中,還有其他許多參數(shù)可以設(shè)置,可以通過社區(qū)版的支持文檔來進(jìn)一步了解:
https://docs.manim.community/en/stable/tutorials/configuration.html#command-line-arguments
03Manim 的基礎(chǔ)形狀介紹
通用屬性
Manim 中基礎(chǔ)形狀有些屬性和方法,對于大部分形狀是通用的,因此在介紹具體的形狀之前,在這里通過 正方形 來講解下形狀的基礎(chǔ)用法。
在下面的案例中,實(shí)現(xiàn)了對正方形的邊框顏色設(shè)置、線寬度設(shè)置、填充顏色、旋轉(zhuǎn)、大小變換等。
#?manim?-pql?manimce-intro-01.py?DemoSquare
class?DemoSquare(Scene):
????def?construct(self):
????????WaterMark.construct(self)
????????r?=?1
????????sq1?=?Square(
????????????side_length=2?*?r,
????????????color=BLUE,
????????)
????????sq1.to_corner(UL,buff=2)
????????self.add(sq1)
????????self.wait()
????????sq2?=?Square(
????????????side_length=2?*?r,
????????????color=BLUE,
????????????stroke_width=10,??#?設(shè)置邊框線的粗細(xì)
????????)
????????sq2.next_to(sq1,RIGHT,buff=1)
????????self.add(sq2)
????????self.wait()
????????sq3?=?Square(
????????????side_length=2?*?r,
????????????color=BLUE,
????????????fill_color=ORANGE,??#?設(shè)置填充顏色
????????????fill_opacity=0.5,??#?設(shè)置透明度
????????)
????????sq3.next_to(sq2,RIGHT,buff=1)
????????self.add(sq3)
????????self.wait()
????????#?形狀大小變換
????????sq4?=?sq1.copy()
????????sq4.scale(0.6)?#?縮小到?60%
????????sq4.next_to(sq1,DOWN,buff=0.5)
????????self.add(sq4)
????????self.wait()
????????#?形狀旋轉(zhuǎn)
????????sq5?=?sq2.copy()
????????sq5.rotate(45*DEGREES)??#?旋轉(zhuǎn)45度
????????sq5.next_to(sq2,DOWN,buff=0.5)
????????self.add(sq5)
????????self.wait()
演示效果如下:

點(diǎn)
對于 點(diǎn) , manim 中目前有幾種不一樣的形狀可以來展示,包括:
Dot AnnotationDot LabeledDot
class?DemoDot(Scene):
????def?construct(self):
????????WaterMark.construct(self)
????????g?=?Group(
????????????#?點(diǎn)
????????????Dot(color=PINK),
????????????AnnotationDot(stroke_color=YELLOW,?fill_color=BLUE,fill_opacity=1),
????????????#?帶文字標(biāo)簽的點(diǎn)
????????????LabeledDot(Tex("2022",?color=RED)),
????????????LabeledDot(MathTex("a",?color=GREEN)),
????????????LabeledDot(Text("Python數(shù)據(jù)之道",?color=BLUE)).scale(0.3),
????????????LabeledDot("Lemon"),
????????)
????????g.arrange(RIGHT,buff=0.5).scale(1.5)
????????g[:2].move_to(UP*1.5)
????????g[2:].next_to(g[:2],DOWN,buff=1)
????????for?shape?in?g:
????????????self.add(shape)
????????????self.wait(0.5)
演示效果如下:

線
在這里,線 的形狀,包括 直線、虛線、箭頭、雙箭頭、彎曲的箭頭等,如下:
Line DashedLine Arrow DoubleArrow CurvedArrow
class?DemoLine(Scene):
????def?construct(self):
????????WaterMark.construct(self)
????????g?=?Group(
????????????#?線
????????????Line(0.5*LEFT,0.5*RIGHT,color=YELLOW),
????????????#?虛線
????????????DashedLine(0.5*LEFT,0.5*RIGHT,color=TEAL),
????????????#?箭頭
????????????Arrow(color=BLUE),?
????????????Arrow(color=?BLUE,?tip_shape=ArrowCircleFilledTip),?#??ArrowCircleTip
????????????Arrow(color=?BLUE,?tip_shape=ArrowSquareTip),#?ArrowSquareFilledTip
????????????#?雙箭頭
????????????DoubleArrow(color=BLUE),
????????????#?彎曲的箭頭
????????????CurvedArrow(LEFT,RIGHT,angle=90*DEGREES,color=?BLUE),?
????????)
????????g.arrange(RIGHT,buff=0.5)
????????g[:3].move_to(UP*1.5)
????????g[3:].next_to(g[:3],DOWN,buff=1)
????????for?shape?in?g:
????????????self.add(shape)
????????????self.wait(0.5)
演示效果如下:

圓形
圓形包括 圓、圓環(huán)、扇形、橢圓、弧形等,如下:
Circle Annulus Ellipse Sector Arc ArcBetweenPoints
class?DemoCircle(Scene):
????def?construct(self):
????????WaterMark.construct(self)
????????g?=?Group(
????????????#?圓形
????????????Circle(radius=0.8,color=YELLOW,fill_color=BLUE,fill_opacity=1),
????????????#?圓環(huán)
????????????Annulus(inner_radius=0.7,?outer_radius=1,fill_color=?DARK_BLUE,?stroke_color=YELLOW,?stroke_width=4),?
????????????#?橢圓
????????????Ellipse(color=?BLUE),
????????????#?扇形
????????????Sector(inner_radius=0.7,?outer_radius=1,fill_color=?BLUE,?stroke_color=YELLOW,?stroke_width=4),
????????????#?弧形
????????????Arc(radius=1.3,?start_angle=-PI/8,?angle=PI,color=?BLUE),
????????????ArcBetweenPoints(start=2?*?RIGHT,?end=2*LEFT,?stroke_color=BLUE)?,
????????)
????????g.arrange(RIGHT,buff=0.5)
????????g[:3].move_to(UP*1.5)
????????g[3:].next_to(g[:3],DOWN,buff=1)
????????for?shape?in?g:
????????????self.add(shape)
????????????self.wait(0.5)
演示效果如下:

下面的這個(gè)視頻,就是基于 扇形(Sector)來制作的。
矩形
矩形類的形狀,是咱們經(jīng)常使用到的一類圖形,在 manim 中包括:
Rectangle RoundedRectangle Square
class?DemoRect(Scene):
????def?construct(self):
????????WaterMark.construct(self)
????????g?=?Group(
????????????#?矩形
????????????Rectangle(width=1,height=0.6,color=BLUE,fill_color=ORANGE,fill_opacity=1),
????????????Rectangle(width=1,height=0.6,color=BLUE,grid_xstep=0.5,grid_ystep=0.2),
????????????#?圓角矩形
????????????RoundedRectangle(corner_radius=0.3,width=1,height=0.6,fill_color=PURPLE,fill_opacity=1),
????????????#?正方形
????????????Square(
????????????side_length=1,
????????????color=BLUE,
????????????fill_color=ORANGE,??#?設(shè)置填充顏色
????????????fill_opacity=0.5,??#?設(shè)置透明度
????????????),
????????)
????????g.arrange(RIGHT,buff=0.5).scale(2)
????????for?shape?in?g:
????????????self.add(shape)
????????????self.wait(0.5)
演示效果如下:

多邊形
多邊形性,相對來說要復(fù)雜些,主要是需要設(shè)置邊緣點(diǎn)的位置,在manim中有多種方式來表示多邊形,包括:
Triangle Polygon RegularPolygon Star Polygram RegularPolygram
class?DemoPolygon(Scene):
????def?construct(self):
????????WaterMark.construct(self)
????????g?=?Group(
????????????#?正三角形
????????????Triangle(radius=2,color=BLUE),
????????????#?三角形
????????????Polygon([-5,?1.5,?0],?[-2,?1.5,?0],?[-3.5,?-2,?0]),
????????????#?多邊形
????????????Polygon([-5,?1.5,?0],?[-2,?1.5,?0],?[-2.5,?-2,?0],?[-4.5,?-1.5,?0]),
????????????#正多邊形
????????????RegularPolygon(n=6,color=BLUE),
????????????#?星型
????????????Star(color=BLUE),
????????????#多邊形
????????????Polygram(
????????????????[[0,?2,?0],?[-np.sqrt(3),?-1,?0],?[np.sqrt(3),?-1,?0]],
????????????????[[-np.sqrt(3),?1,?0],?[0,?-2,?0],?[np.sqrt(3),?1,?0]],),
????????????RegularPolygram(num_vertices?=?7),
????????????RegularPolygram(5,?radius=1),
????????)
????????g.arrange(RIGHT,buff=0.5).scale(0.7)
????????g[:4].move_to(UP*1.5)
????????g[4:].next_to(g[:3],DOWN,buff=1)
????????for?shape?in?g:
????????????self.add(shape)
????????????self.wait(0.5)
演示效果如下:

符號
在manim 中,也經(jīng)常會(huì)用到 大括號等形狀,如下:
class?DemoCross(Scene):
????def?construct(self):
????????WaterMark.construct(self)
????????#?十字叉
????????cross?=?Cross(stroke_color?=?BLUE,stroke_width=20).scale(0.8)
????????cross.to_corner(UL,buff=2)
????????self.add(cross)
????????self.wait(0.5)
????????#?大括號
????????br1?=?Brace(Line(LEFT,RIGHT),color=?BLUE)
????????br1.next_to(cross,RIGHT,buff=0.5)
????????self.add(br1)
????????self.wait(0.5)
????????#?帶文字的大括號
????????line=Line(LEFT,RIGHT)?
????????br2=?BraceLabel(line,?text=?"14cm",?color=?YELLOW,?buff=0.1)?
????????br2.submobjects[1].set_color(BLUE)?
????????self.add(VGroup(line,br2).next_to(br1,RIGHT,buff=0.5))
????????self.wait(0.5)
????????#?帶弧度的大括號
????????arc?=?Arc(radius=1,start_angle=0,angle=3*PI/4)?
????????br3?=?ArcBrace(arc).set_color(BLUE)
????????self.add(VGroup(arc,br3).next_to(VGroup(line,br2),RIGHT,buff=0.5))
????????#?self.add(arc,br3)
????????self.wait(0.5)
演示效果如下:

04小結(jié)
相對而言,manim 中的基礎(chǔ)形狀,還是比較齊全的,在這些基礎(chǔ)形狀的基礎(chǔ)上,自己可以進(jìn)一步來組合其他的形狀。
大家讀完順手點(diǎn)下右下角的 ?“在看” ,就是最大的鼓勵(lì)和支持了。
往期精彩回顧
