MANIM的基本構成要素(Building blocks)


MANIM的基本構成要素(Building blocks)

簡介

本質上,manim就是把三個你可以支配並且精心放置的“概念”組合在一起然后生成數學動畫。這三個“概念”是:數學物體(mathematical object用 mobject的縮寫形式)、動畫(animation)、場景(scene)。在接下來的介紹中可以看到,這三個概念中的每一個都作為一個單獨的“類”被獨立的執行。這三個類是:Mobject、Animation和Scene。

Mobject

Mobject是manim動畫中基本的構造塊(building block)。每一個從Mobject派生出的類代表着一個可以被放置在屏幕上的物體。

For example, simple shapes such as Circle, Arrow, and Rectangle are all mobjects. More complicated constructs such as Axes, FunctionGraph, or BarChart are mobjects as well.

如果你嘗試放置一個Mobject中的實例,你只會看見一個空的框架。原因是:Mobject類是其他所有mobject的抽象基類,也就是說,它是沒有提前決定好的視覺形象能被展示再屏幕上。因此,你將很少使用平凡的沒有經過修飾的Mobject實例,相反,你更有可能創建它的派生類別中的實例。其中的一個就是VMobject。V代表着向量化(vectorized)。本質上,vmobject是一個使用矢量圖來顯示的mobject。大多數時間,你將處理vmobjects,盡管我們還是繼續使用“mobject”去指代可以將圖形放置在屏幕上的這個類別(Mobject),因為mobject含義更加廣泛。

Note

Any object that can be displayed on the screen is a mobject, even if it is not necessarily mathematical in nature.任何能在屏幕上顯示的物體都是mobject,即便事實上它不一定有數學含義。

Tip

To see examples of classes derived from Mobject, see the geometry module. Most of these are in fact derived from VMobject as well.

接下來我將把Mobject的一些要點整合進幾個代碼中:

from manim import *

class MobjectPlacement(Scene):
   def construct(self):
   #圖像創建
  #shift從原點以某方向移動
       circle01 = Circle().shift(RIGHT) #也可以分開寫
    #分開寫法示例
       # square = Square()
# square.shift(LEFT)
       circle02 = Circle()
       square = Square()
       triangle = Triangle()
#圖像放置
       #move_to 相對於坐標原點的移動
       circle02.move_to(LEFT * 2)
       #next_to 相對於第一個放置的mobject的移動
       square.next_to(circle02, UP)
       #align_to 下面的一行代碼表示讓三角形的左邊界與圓的左         邊界對齊 注意是邊界而不是最左邊的點
       triangle.align_to(circle02, LEFT)
  #圖像顯示
#add 只是單純的將圖像顯示不會有動畫過程
       self.add(circle01,circle02, square, triangle)
       self.wait(1)
       #remove 可以移除圖像
       self.remove(circle01)
from manim import *

class MobjectStyling(Scene):
   def construct(self):
       circle = Circle().shift(LEFT)
       square = Square().shift(UP)
       triangle = Triangle().shift(RIGHT)
#圖形的樣式
  #set_stroke 設置邊框的顏色、寬度
       circle.set_stroke(color=GREEN, width=20)
       #set_fill   設置圖形內部的顏色、透明度(0到1)
       #由於默認圖形內部是透明的,必須設置透明度才可以顯示圖 形顏色。
       square.set_fill(YELLOW, opacity=1.0)
       triangle.set_fill(PINK, opacity=0.5)
  #圖形的add順序
#放置在后面的mobject會覆蓋在之前放置的上面
  #下一行代碼顯示三角形在最上面
       self.add(circle, square, triangle)
       self.wait(1)

NOTE:

Only instances of VMobject implement set_stroke() and set_fill(). Instances of Mobject implement set_color() instead. The vast majority of pre-defined classes are derived from VMobject so it is usually safe to assume that you have access to set_stroke() and set_fill().

Animations

動畫是manim的核心部分。以下仍提供幾個整合的代碼示例:

from manim import *

class SomeAnimations(Scene):
   def construct(self):
       square = Square().set_fill(BLUE, opacity=1.0)
       circle = Circle().set_fill(RED,  opacity=1.0)
   #play 用run_time設置演示時間
       #FadeIn 漸進顯示(通過插值不斷變化透明度實現 透明度從0到1)
       self.play(FadeIn(square),run_time=3)
       #Rotate 旋轉
       self.play(Rotate(square, PI/4))
       #ApplyMethod 任何mobject的屬性可以用這個方法改變,並且以動畫的形式在屏幕上顯示出來
       self.play(ApplyMethod(square.set_fill, WHITE))
       self.play(ApplyMethod(square.shift, UP))
       self.play(ApplyMethod(square.rotate, PI/4))
       #mobject間的轉換   Transform
       self.play(Transform(square,circle))
       #FadeOut 漸進淡出(透明度從1到0)
       self.play(FadeOut(square),run_time=2)
       #值得注意的是轉換后的circle是沒有被放置上去的
       #因此要實現淡出效果,內部的變量仍然是square而不是轉變后的circle

Scenes

Scene好比manim的結締組織。每個 mobject 都必須added到一個場景才能顯示,或者removed從它停止顯示。每個動畫都必須在一個場景中才可以顯示出來,並且每個沒有動畫發生的時間間隔都通過調用wait( )確定。視頻的所有代碼都必須包含在construct()派生自Scene. 最后,Scene如果要同時渲染多個場景,則單個文件可能包含多個子類。

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM