[ PROMPT_NODE_27758 ]
scenes
[ SKILL_DOCUMENTATION ]
# Manim 中的场景 (Scenes)
场景是动画的画布。所有的 Manim 代码都组织在 Scene 类中,每个场景代表一个完整的动画序列。
## 基本场景结构
python
from manim import *
class MyScene(Scene):
def construct(self):
# 所有动画代码写在这里
circle = Circle()
self.play(Create(circle))
self.wait(1)
## 场景类型
Manim 为不同目的提供了不同的场景类型:
### 标准场景
python
class BasicScene(Scene):
def construct(self):
text = Text("Hello, Manim!")
self.play(Write(text))
self.wait(2)
### 移动摄像机场景
用于需要摄像机移动的场景:
python
class CameraScene(MovingCameraScene):
def construct(self):
circle = Circle()
self.play(Create(circle))
# 缩放以聚焦圆形
self.play(self.camera.frame.animate.scale(0.5))
self.wait(1)
### 3D 场景
用于三维动画:
python
class ThreeDExample(ThreeDScene):
def construct(self):
axes = ThreeDAxes()
sphere = Sphere()
self.set_camera_orientation(phi=75 * DEGREES, theta=30 * DEGREES)
self.play(Create(axes), Create(sphere))
self.begin_ambient_camera_rotation(rate=0.1)
self.wait(5)
## 添加与移除 Mobjects
python
class AddRemoveScene(Scene):
def construct(self):
circle = Circle()
square = Square()
# 直接添加,无动画
self.add(circle)
self.wait(1)
# 带动画添加
self.play(Create(square))
self.wait(1)
# 直接移除,无动画
self.remove(circle)
self.wait(1)
# 带动画移除
self.play(FadeOut(square))
## 场景方法
常用的场景方法:
python
class SceneMethods(Scene):
def construct(self):
circle = Circle()
# 将 mobject 添加到场景
self.add(circle)
# 播放动画
self.play(circle.animate.shift(RIGHT * 2))
# 等待(暂停)
self.wait(2)
# 移除 mobject
self.remove(circle)
# 清除所有 mobjects
self.clear()
## 单个文件中的多个场景
python
from manim import *
class Scene1(Scene):
def construct(self):
circle = Circle()
self.play(Create(circle))
class Scene2(Scene):
def construct(self):
square = Square()
self.play(Create(square))
#