Manim文档及源码笔记-CE教程BE02英文笔记速览版

Manim文档及源码笔记-CE教程BE02英文笔记速览版

Positioning and Configuration _ Mathematical Animations WITH EASE

前言

这次筛选到了Behackl博士的教程,逐步拆解,更为细腻~
参考视频在此【或请自行搜索上面的英文标题】;
本页中文版传送门【建设中】;

更新【new】:

  • 8月23日
    • 文首标注了“前言”与“正文”;
    • 本系列暂告段落,升级了系列目录,放到了文尾;
  • 后续更新备忘:文中“【建设中】”的内容;欢迎朋友们催更~

首先,国际通则:Just GET STARTED~ JUST DO IT~
此图来自相关网络,侵删

然后,让我们行动起来~
注:
1、代码实践过程根据运行环境及笔记需要略有改动;
2、经过实践理解,加入了一些自己的注释;
3、常见问题及大概率解决方案:

  • Python相关:注意缩进、冒号,中英文字符、大小写;
  • Manim相关:安装与运行环境;
  • Coding相关:检查拼写;

正文

Part 1 Positioning and Alignment of Mobjects

Coordinates

  • Manim uses 3D coordinate system( internally: points are NumPy arrays)
  • Default( 2D) settings: Scene frame is
    • 8 Munits[ Manim Units] in height
    • 14 + 2 / 9 = 14.2 2 ˙ 14+2/9=14.2\dot{2} 14+2/9=14.22˙ Munits in width
  • Origin (0, 0, 0) is at center of frame; it measures
    [ − 7.1 1 ˙ , 7.1 1 ˙ ] × [ − 4 , 4 ] [-7.1\dot{1}, 7.1\dot{1}] \times [-4, 4] [7.11˙,7.11˙]×[4,4] Munits
  • Pixel measurements of output depend on quality settings
from manim import *
config.media_width="60%"
%%manim -v WARNING -qm Positioning

class Positioning(Scene):
    def construct(self):
        plane=NumberPlane()
        self.add(plane)

Positioning`%%manim -v WARNING -qm Positioning

class Positioning(Scene):
def construct(self):
plane=NumberPlane()
self.add(plane)

    # next_to from episode 1
    red_dot=Dot(color=RED)
    green_dot=Dot(color=GREEN)
    green_dot.next_to(red_dot,RIGHT) # RIGHT==[1,0,0]
    self.add(red_dot,green_dot)`

Positioning1
%%manim -v WARNING -qm Positioning2

class Positioning2(Scene):
def construct(self):
plane=NumberPlane()
self.add(plane)

    # next_to from episode 1
    red_dot=Dot(color=RED)
    green_dot=Dot(color=GREEN)
    #green_dot.next_to(red_dot,RIGHT) # RIGHT==[1,0,0]
    green_dot.next_to(red_dot,RIGHT+UP) 
    self.add(red_dot,green_dot)

Positioning2

%%manim -v WARNING -qm Positioning3

class Positioning3(Scene):
    def construct(self):
        plane=NumberPlane()
        self.add(plane)

        # next_to from episode 1
        red_dot=Dot(color=RED)
        green_dot=Dot(color=GREEN)
        #green_dot.next_to(red_dot,RIGHT) # RIGHT==[1,0,0]
        green_dot.next_to(red_dot,RIGHT+UP) 
        self.add(red_dot,green_dot)

        # shift
        s=Square(color=ORANGE)
        s.shift(2*UP+4*RIGHT)
        self.add(s)

Positioning3

%%manim -v WARNING -qm Positioning4

class Positioning4(Scene):
    def construct(self):
        plane=NumberPlane()
        self.add(plane)

        # next_to from episode 1
        red_dot=Dot(color=RED)
        green_dot=Dot(color=GREEN)
        #green_dot.next_to(red_dot,RIGHT) # RIGHT==[1,0,0]
        green_dot.next_to(red_dot,RIGHT+UP) 
        self.add(red_dot,green_dot)

        # shift
        s=Square(color=ORANGE)
        s.shift(2*UP+4*RIGHT)
        self.add(s)

        # move_to
        c=Circle(color=PURPLE)
        c.move_to([-3,-2,0])
        self.add(c)

        # align_to
        c2=Circle(radius=0.5, color=RED, fill_opacity=0.5)
        c3=c2.copy().set_color(YELLOW)
        c4=c2.copy().set_color(ORANGE)
        c2.align_to(s,UP)
        self.add(c2)
        

Positioning4

%%manim -v WARNING -qm Positioning5

class Positioning5(Scene):
    def construct(self):
        plane=NumberPlane()
        self.add(plane)

        # next_to from episode 1
        red_dot=Dot(color=RED)
        green_dot=Dot(color=GREEN)
        #green_dot.next_to(red_dot,RIGHT) # RIGHT==[1,0,0]
        green_dot.next_to(red_dot,RIGHT+UP) 
        self.add(red_dot,green_dot)

        # shift
        s=Square(color=ORANGE)
        s.shift(2*UP+4*RIGHT)
        self.add(s)

        # move_to
        c=Circle(color=PURPLE)
        c.move_to([-3,-2,0])
        self.add(c)

        # align_to
        c2=Circle(radius=0.5, color=RED, fill_opacity=0.5)
        c3=c2.copy().set_color(YELLOW)
        c4=c2.copy().set_color(ORANGE)
        c2.align_to(s,UP)
        c3.align_to(s,RIGHT)
        c4.align_to(s,UP+RIGHT)
        self.add(c2,c3,c4)

Positioning5
开始用上循环,有了超能力。之前在Blender中用Python也有相同感受~

%%manim -v WARNING -qm CriticalPoints

class CriticalPoints(Scene):
    def construct(self):
        c=Circle(color=GREEN,fill_opacity=0.5)
        self.add(c)

        for d in [(0,0,0),UP,UR,RIGHT,DR,DOWN,DL,LEFT,UL]:
            self.add(Cross(scale_factor=0.2).move_to(c.get_critical_point(d)))

CriticalPoints

%%manim -v WARNING -qm CriticalPoints1

class CriticalPoints1(Scene):
    def construct(self):
        c=Circle(color=GREEN,fill_opacity=0.5)
        self.add(c)

        for d in [(0,0,0),UP,UR,RIGHT,DR,DOWN,DL,LEFT,UL]:
            self.add(Cross(scale_factor=0.2).move_to(c.get_critical_point(d)))

        s=Square(color=RED,fill_opacity=0.5)
        s.move_to([1,0,0],aligned_edge=LEFT)
        self.add(s)

CriticalPoints1

%%manim -v WARNING -qm UsefulUnits

from manim.utils.unit import Percent, Pixels
class UsefulUnits(Scene):
    def construct(self):
        for perc in range(5,51,5):
            self.add(Circle(radius=perc*Percent(X_AXIS)))

UsefulUnits

%%manim -v WARNING -qm UsefulUnits1

from manim.utils.unit import Percent, Pixels
class UsefulUnits1(Scene):
    def construct(self):
        for perc in range(5,51,5):
            self.add(Circle(radius=perc*Percent(X_AXIS)))
            self.add(Square(side_length=2*perc*Percent(Y_AXIS),color=YELLOW))

UsefulUnits1

%%manim -v WARNING -qm UsefulUnits2

from manim.utils.unit import Percent, Pixels
class UsefulUnits2(Scene):
    def construct(self):
        for perc in range(5,51,5):
            self.add(Circle(radius=perc*Percent(X_AXIS)))
            self.add(Square(side_length=2*perc*Percent(Y_AXIS),color=YELLOW))

        d=Dot()
        d.shift(100*Pixels*RIGHT)
        self.add(d)

在这里插入图片描述

%%manim -v WARNING -qh UsefulUnits2

在这里插入图片描述

%%manim -v WARNING -qm Grouping

class Grouping(Scene):
    def construct(self):
        red_dot=Dot(color=RED)
        green_dot=Dot(color=GREEN).next_to(red_dot, RIGHT)
        blue_dot=Dot(color=BLUE).next_to(red_dot, UP)
        dot_group=VGroup(red_dot,green_dot,blue_dot)
        dot_group.to_edge(RIGHT)
        self.add(dot_group)

Grouping

%%manim -v WARNING -qm Grouping1

class Grouping1(Scene):
    def construct(self):
        red_dot=Dot(color=RED)
        green_dot=Dot(color=GREEN).next_to(red_dot, RIGHT)
        blue_dot=Dot(color=BLUE).next_to(red_dot, UP)
        dot_group=VGroup(red_dot,green_dot,blue_dot)
        dot_group.to_edge(RIGHT)
        self.add(dot_group)

        circles=VGroup(*[Circle(radius=0.2) for _ in range(10)])
        circles.arrange(UP)
        self.add(circles)

Grouping1

%%manim -v WARNING -qm Grouping2

class Grouping2(Scene):
    def construct(self):
        red_dot=Dot(color=RED)
        green_dot=Dot(color=GREEN).next_to(red_dot, RIGHT)
        blue_dot=Dot(color=BLUE).next_to(red_dot, UP)
        dot_group=VGroup(red_dot,green_dot,blue_dot)
        dot_group.to_edge(RIGHT)
        self.add(dot_group)

        circles=VGroup(*[Circle(radius=0.2) for _ in range(10)])
        circles.arrange(UP,buff=0.5)
        self.add(circles)

Grouping2

%%manim -v WARNING -qm Grouping3

class Grouping3(Scene):
    def construct(self):
        red_dot=Dot(color=RED)
        green_dot=Dot(color=GREEN).next_to(red_dot, RIGHT)
        blue_dot=Dot(color=BLUE).next_to(red_dot, UP)
        dot_group=VGroup(red_dot,green_dot,blue_dot)
        dot_group.to_edge(RIGHT)
        self.add(dot_group)

        circles=VGroup(*[Circle(radius=0.2) for _ in range(10)])
        circles.arrange(UP,buff=0.5)
        self.add(circles)

        stars=VGroup(*[Star(color=YELLOW,fill_opacity=1).scale(0.5) for _ in range(20)])
        stars.arrange_in_grid(4,5,buff=0.2)
        self.add(stars)

Grouping3

Part 2 Manim’s configuration system

config

  • Idea: Single Source of Truth for “global” settings
  • Documentation and Tutorial
  • Settings can be changed in .py file, but also in separate manim.cfg file
  • Local config generation: manim cfg write -l cwd
[CLI]
notify_outdated_version=True
background_color=WHITE
frame_width=16
frame_height=9
frame_rate=60
pixel_height=1080
pixel_width=1920
%%manim -v WARNING -qm SimpleScene

config.background_color=WHITE
class SimpleScene(Scene):
    def construct(self):
        t=Triangle(color=PURPLE, fill_opacity=0.5)
        self.add(t)

SimpleScene
%%manim -v WARNING -qm SimpleScene1

config.background_color=BLACK
config.frame_width=16
config.frame_height=9

class SimpleScene1(Scene):
def construct(self):
plane=NumberPlane()
t=Triangle(color=PURPLE, fill_opacity=0.5)
self.add(plane,t)

SimpleScene1

%%manim -v WARNING -qm SimpleScene2

config.background_color=BLACK
config.frame_width=32
config.frame_height=18

class SimpleScene2(Scene):
    def construct(self):
        plane=NumberPlane()
        t=Triangle(color=PURPLE, fill_opacity=0.5)
        self.add(plane,t)

SimpleScene2
这次在Jupyter模式(.ipynb格式)下,长宽比例没有变化:

%%manim -v WARNING -qm SimpleScene3

config.background_color=BLACK
config.frame_width=10
config.frame_height=10
config.pixel_width=500
config.pixel_height=500

class SimpleScene3(Scene):
    def construct(self):
        plane=NumberPlane()
        t=Triangle(color=PURPLE, fill_opacity=0.5)
        self.add(plane,t)

SimpleScene3
根据前面文档提示信息,需要复制成为常规(.py格式)文件:

from manim import *
#config.media_width="100%"
from manim.utils.unit import Percent, Pixels

config.background_color=BLACK
config.frame_width=10
config.frame_height=10

config.pixel_width=500
config.pixel_height=500

class SimpleScene3(Scene):
    def construct(self):
        plane=NumberPlane(x_range=(-8,8),y_range=(-5,5))
        t=Triangle(color=PURPLE, fill_opacity=0.5)
        self.add(plane,t)

点击Run,VSCode会自动判断路径,然后在TERMINAL窗口内输入下面这行命令即可:

manim -qm -p 02.py SimpleScene3 

SimpleScene3



# 2022-08-10 21:14:27

from manim import *
#config.media_width="100%"
from manim.utils.unit import Percent, Pixels

config.background_color=BLACK
# config.frame_width=10
# config.frame_height=10

# config.pixel_width=500
# config.pixel_height=500
config.frame_width=9
config.frame_height=16

config.pixel_width=1080
config.pixel_height=1920
class SimpleScene4(Scene):
    def construct(self):
        #plane=NumberPlane(x_range=(-8,8),y_range=(-5,5))
        plane=NumberPlane(x_range=(-4.5,4.5),y_range=(-8,8))
        t=Triangle(color=PURPLE, fill_opacity=0.5)
        self.add(plane,t)

SimpleScene4

%%manim -v WARNING -qm ChangedDefaults

class ChangedDefaults(Scene):
    def construct(self):
        t=Text("Hello World!")
        self.add(t)

ChangedDefaults

%%manim -v WARNING -qm ChangedDefaults1
config.background_color=WHITE
class ChangedDefaults1(Scene):
    def construct(self):
        t=Text("Hello World!",color=BLACK)
        self.add(t)

ChangedDefaults1

%%manim -v WARNING -qm ChangedDefaults2
config.background_color=WHITE
class ChangedDefaults2(Scene):
    def construct(self):
        Text.set_default(color=GREEN)
        t=Text("Hello World!")
        self.add(t)

ChangedDefaults2

%%manim -v WARNING -qm ChangedDefaults2a
config.background_color=WHITE
class ChangedDefaults2a(Scene):
    def construct(self):
        Text.set_default(color=GREEN)
        t=Text("Hello World!",font_size=100)
        self.add(t)

ChangedDefaults2a

%%manim -v WARNING -qm ChangedDefaults2b
config.background_color=WHITE
class ChangedDefaults2b(Scene):
    def construct(self):
        Text.set_default(color=GREEN,font_size=100)
        t=Text("Hello World!")
        self.add(t)

ChangedDefaults2b

%%manim -v WARNING -qm ChangedDefaults3
config.background_color=WHITE
class ChangedDefaults3(Scene):
    def construct(self):
        Text.set_default(color=GREEN,font_size=100)
        t=Text("Hello World!",color=RED)
        self.add(t)

ChangedDefaults3

%%manim -v WARNING -qm ChangedDefaults4
config.background_color=BLACK
class ChangedDefaults4(Scene):
    def construct(self):
        Text.set_default(color=GREEN,font_size=100)
        t=Text("Hello World!")
        
        Text.set_default()
        t2=Text("Goodbye!").next_to(t,DOWN)

        self.add(t,t2)

ChangedDefaults4

本系列目录【new】

Manim文档及源码笔记-CE教程BE01英文笔记速览版
Manim文档及源码笔记-CE教程BE02英文笔记速览版【本文】
Manim文档及源码笔记-CE教程BE03英文笔记速览版
Manim文档及源码笔记-CE教程BE04英文笔记速览版
Manim文档及源码笔记-CE教程BE05英文笔记速览版

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值