Manim基本操作。

1. 添加动画内容

首先,需要创建一个画布(由类Scene定义),随后创建的对象可以在画布上展示。通常这样做,自己创建一个类,继承Scene,并在Scene.construct()函数中添加动画内容,举例如下:

class MyScene(Scene):
    def construct(self):
        # Add content to the Scene.
        self.play(Write(Text("Hello World!")))

1.1 创建对象

Mobject指数学对象(Mathematical Object),是所有能显示在屏幕上的基类。

class Mobject(color='#FFFFFF', name=None, dim=3, target=None, z_index=0)

Manim定义了很多常用对象,比如文本、点、线、三角形,Mobjects#mobjects给出了一张类的继承关系,还带超链接,浏览起来很方便。

Mobjects-class.png

创建对象举例:

text = Text('ABCDEABCDE')    # create a text
circle = Circle()              # create a circle

1.2 添加对像

在画布中添加对象,调用函数add(*mobjects)

# Mobjects will be displayed, from background to foreground in the order with which they are added.
add(*mobjects)

Write模拟手写或手画一个对象,直接在画布上动态显示该对象。

# Simulate hand-writing a Text or hand-drawing a VMobject
class Write(mobject=None, *args, use_override=True, **kwargs)

# Example
self.play(Write(Text("Hello World!")))

1.3 移除对象

(1)移除单个对象

remove(*mobjects)

# Removes mobjects in the passed list of mobjects from the scene and the foreground, by removing them from "mobjects" and "foreground_mobjects"
remove(*mobjects)

(2)移除所有对象

clear()

# Removes all mobjects present in self.mobjects and self.foreground_mobjects from the scene.
clear()

1.4 设置动画

有了对象,就可以设置对象的动画,如移动、翻转。类Animation是动画的基类,Manim提供了多种动画,如淡入、淡出、翻转。Mobjects#animations给出了一张类的继承关系,还带超链接,浏览起来很方便。

Mobjects#animations

1.5 播放动画

调用场景的play函数,播放上述设置的动画。

# Plays an animation in this scene.
play(*args, subcaption=None, subcaption_duration=None, subcaption_offset=0, **kwargs)

# Example
self.play(Write(text_solar_term.next_to(text_notes, DOWN)))

1.6 其他

(1)保存对象为图片

使用Mobject.save_image

# Saves an image of only this Mobject at its position to a png file.
save_image(name=None)

# Axample
class Test(Scene):
    def construct(self):
        text = Text('Hello World!')
        text.save_image('test.png')

提示错误 KeyError: 'video_dir {media_dir}/videos/{module_name}/{quality} requires the following keyword arguments: module_name'

2. 产生视频

在场景Scene中construct()函数完成动画内容添加后,在命令行运行如下命令,产生视频。

manim [OPTIONS] FILE [SCENES]

A list of all CLI flags给出了所有的选项,常用的有:

  • -p (“preview”)
  • -q (--quality),表示产生视频的分辨率,值可以是[l|m|h|p|k],分别表示854x480 15FPS, 1280x720 30FPS, 1920x1080 60FPS, 2560x144060FPS, 3840x2160 60FPS。
本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2022-02-26 22:19

results matching ""

    No results matching ""