这个示例展示如何在 Jupyter Notebook 中编写并渲染 Manim 动画。下面的代码与输出直接来自同目录下的 demo.ipynb,视频以 Base64 形式内嵌在 Notebook 中,无需额外的视频文件。
用 Manim 在 Jupyter Notebook 中制作动画
这个最简示例使用 media_embed=True,将渲染完成的 MP4 直接以 Base64 写入 .ipynb 输出。博客只需解析 Notebook,无需单独保存或复制视频文件。
import manim
from manim import *
print(f"Manim Community {manim.__version__}")Manim Community 0.20.1
%%manim -qm -r 960,540 -v WARNING BlogDemo
config.media_embed = True
config.media_width = "100%"
config.background_color = "#101827"
class BlogDemo(Scene):
def construct(self):
title = Text("Manim in Jupyter", font_size=48, color=BLUE_B)
subtitle = Text("Create · animate · Transform", font_size=24, color=GRAY_B)
subtitle.next_to(title, DOWN)
circle = Circle(radius=1.0, color=BLUE, fill_opacity=0.35)
circle.shift(LEFT * 3 + DOWN * 0.7)
self.play(Write(title), FadeIn(subtitle, shift=UP), run_time=1.2)
self.play(VGroup(title, subtitle).animate.to_edge(UP).scale(0.75))
self.play(Create(circle))
self.play(
circle.animate.shift(RIGHT * 3).scale(1.3).set_color(YELLOW),
run_time=1.5,
)
square = Square(side_length=2.6, color=PURPLE_B, fill_opacity=0.45)
square.move_to(circle)
self.play(Transform(circle, square), run_time=1.2)
self.play(circle.animate.rotate(PI / 4).set_color(GREEN_B), run_time=1.0)
self.wait(0.6)
self.play(FadeOut(VGroup(title, subtitle, circle)), run_time=0.8)Manim Community v0.20.1


说些什么吧!