Manim介绍及安装

1. Manim介绍

(1)Manim简介及版本

Manim原先是由Grant Sanderson开发的一个动画引擎,用在他的Youtube频道3Blue1Brown - YouTube,旨在用动画直观解释数学的一些问题。随着他的YouTube频道变得流行,其他开发者也想用Manim来开发,于是有了社区版。目前Manim有如下版本(详情见Differences between Manim Versions):

  • ManimCE: The community edition of manim, named manim on pip.
  • ManimGL: The current version of manim that is used by 3blue1brown. It supports OpenGL rendering and interactivity, and is named manimgl on pip. You can find documentation for it here.
  • ManimCairo: The old version of manim originally used by 3blue1brown. It is not available on pip.

(2)选用哪个版本

总体上,两者文档都很易读。ManimGL渲染更快,Manim社区版,提供Jupyter Notebooks ,声称更稳定。Manim社区版说2021年4月会全部支持OpenGL。我最开始用的是ManimGL,忘了碰到一个什么问题,后来转向社区版。

2. 安装

使用Manim,需要安装:

  • Python,
  • LaTeX(可选),数学公式
  • FFmpeg,用来将图片拼成视频
  • OpenGL,用于动画渲染,pip install manimgl会连带装上PyOpenGL
  • Cairo,用来绘图,python -m pip install manim会连带装上pycairo
  • SoX(可选),用来负责和音频有关的部分(还没用上,我没装)

安装FFmpeg

直接在Builds - CODEX FFMPEG @ gyan.dev下载ffmpeg-release-essentials.zipProgram Files目录下,解压。将其bin目录(如我的D:\Program Files\ffmpeg-4.3.1-2021-01-01-essentials_build\bin)添加到用户变量或者系统变量的Path中。

2.1 manimgl

安装manimgl

pip install manimgl

Successfully installed PyOpenGL-3.1.5 argparse-1.4.0 colour-0.1.5 commonmark-0.9.1 glcontext-2.3.4 manimgl-1.3.0 manimpango-0.3.1 mapbox-earcut-1.0.0 mod
erngl-5.6.4 moderngl-window-2.4.1 pydub-0.25.1 pyglet-1.5.21 pyrr-0.10.3 rich-10.16.1 screeninfo-0.8 skia-pathops-0.7.2 validators-0.18.2

一个简单例子start.py

from manimlib import *

class SquareToCircle(Scene):
    def construct(self):
        circle = Circle()
        circle.set_fill(BLUE, opacity=0.5)
        circle.set_stroke(BLUE_E, width=4)
        square = Square()

        self.play(ShowCreation(square))
        self.wait()
        self.play(ReplacementTransform(square, circle))
        self.wait()

运行,-o,output,表示输出文件。

manimgl start.py SquareToCircle -o

卸载manimgl

后面转向社区版,就把manimgl卸载了。

pip uninstall manimgl

Proceed (y/n)? y
  Successfully uninstalled manimgl-1.3.0

2.2 Manim Community

最开始用的是manimgl版本,出现一些问题,后来转向Manim社区版,安装方法如下:

python -m pip install manim

Successfully installed click-default-group-1.2.2 cloup-0.7.1 decorator-5.1.1 isosurfaces-0.1.0 manim-0.14.0 manimpango-0.4.0.post0 mapbox-earcut-0.12.11
pycairo-1.20.1 screeninfo-0.6.7 srt-3.5.0

详情见文档Windows - Manim Community

一个简单例子:

# scene.py

from manim import *

class CreateCircle(Scene):
    def construct(self):
        circle = Circle()  # create a circle
        circle.set_fill(PINK, opacity=0.5)  # set the color and transparency
        self.play(Create(circle))  # show the circle on screen

在命令行运行:

# manim [OPTIONS] FILE [SCENES]
manim -p scene.py CreateCircle

将会创建一个scene.mp4视频,并且播放(-p表示preview)。

3. 学习资料

ManimGL

Manim Community

在查资料的时候,了解到有一位用Manim作动画,并且把源代码挂到GitHub,当时没记录,现在找不到了:-(

4. 其他

在看博文学习 Manim | 兴趣使然的博客,发现可以将Windows的字符编码设为UTF-8,操作示意图如下:windows-language-encdoing

本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2022-02-26 20:53

results matching ""

    No results matching ""