Manim 初探

Manim(Mathematical Animation Engine)是一个用于制作数学动画的 Python 包,由 3Blue1Brown 作者 Grant Sanderson 开发。

安装

pip install manim

使用

  1. 创建新项目:

    manim init project my-project --default
    
  2. 进入项目目录,里面已经有了一个示例程序 main.py,内容如下:

    from manim import *
    class DefaultTemplate(Scene):
        def construct(self):
            circle = Circle()                     # 创建一个圆形
            circle.set_fill(PINK, opacity=0.5)    # 设置颜色和不透明度
    
            square = Square()                     # 创建一个正方形
            square.flip(RIGHT)                    # 水平翻转
            square.rotate(-3 * TAU / 8)           # 旋转一定角度
    
            self.play(Create(square))             # 动画展示正方形的创建过程
            self.play(Transform(square, circle))  # 将正方形变为圆形
            self.play(FadeOut(square))            # 淡出动画
    
  3. 生成动画:

    manim -pql main.py DefaultTemplate
    
    • -p:play。生成动画后自动播放
    • -q:质量选项。l 低质量,m 中质量,h 高质量

参考:Quickstart | Manim Community

Troubleshooting

使用 pip 安装 manim 出错:

$ pip install -i https://pypi.org/simple manim
...
Collecting manimpango<1.0.0,>=0.5.0 (from manim)
  Using cached manimpango-0.6.0.tar.gz (4.1 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [33 lines of output]
      Package pangocairo was not found in the pkg-config search path.
      Perhaps you should add the directory containing `pangocairo.pc'
      to the PKG_CONFIG_PATH environment variable
      No package 'pangocairo' found
      Traceback (most recent call last):
        File "<string>", line 137, in check_min_version
        File "/home/ubuntu/.local/opt/miniforge3/envs/misc/lib/python3.13/subprocess.py", line 419, in check_call
          raise CalledProcessError(retcode, cmd)
      subprocess.CalledProcessError: Command '['pkg-config', '--print-errors', '--atleast-version', '1.30.0', 'pangocairo']' returned non-zero exit status 1.
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "/home/ubuntu/.local/opt/miniforge3/envs/misc/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 389, in <module>
          main()
          ~~~~^^
        File "/home/ubuntu/.local/opt/miniforge3/envs/misc/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 373, in main
          json_out["return_val"] = hook(**hook_input["kwargs"])
                                   ~~~~^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/ubuntu/.local/opt/miniforge3/envs/misc/lib/python3.13/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 143, in get_requires_for_build_wheel
          return hook(config_settings)
        File "/tmp/pip-build-env-kga8s7hs/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 331, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=[])
                 ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-kga8s7hs/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 301, in _get_build_requires
          self.run_setup()
          ~~~~~~~~~~~~~~^^
        File "/tmp/pip-build-env-kga8s7hs/overlay/lib/python3.13/site-packages/setuptools/build_meta.py", line 317, in run_setup
          exec(code, locals())
          ~~~~^^^^^^^^^^^^^^^^
        File "<string>", line 204, in <module>
        File "<string>", line 140, in check_min_version
      RequiredDependencyException: pangocairo >= 1.30.0 is required
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

解决方法:安装 pangocairo

# Ubuntu
sudo apt install libpango1.0-dev
# Homebrew
brew install pango

参见:How I animate 3Blue1Brown - A Manim demo with Ben Sparks | YouTube

3Blue1Brown 频道所有代码:3b1b/videos

posted @ 2025-04-28 00:32  Undefined443  阅读(107)  评论(0)    收藏  举报