| name | manim-fundamentals |
| description | Core Manim concepts including Scene lifecycle, Mobject hierarchy, coordinate systems, animation lifecycle, and rate functions. |
Manim Fundamentals
Core concepts for Manim Community Edition v0.20.1.
Scene Lifecycle
Scene Structure
class MyScene(Scene):
def setup(self):
"""Called once before construct()
- Initialize instance variables
- Create shared objects
- Set up tracking systems
"""
self.tracked_objects = []
def construct(self):
"""Main scene logic
- Create mobjects
- Define animations
- Control timing
"""
circle = Circle()
self.play(Create(circle))
def tear_down(self):
"""Called after construct()
- Cleanup (rarely needed)
"""
pass
Scene Types
| Scene Type | Use Case | Camera |
|---|
Scene | Standard 2D animations | Static |
MovingCameraScene | 2D with zoom/pan | Movable 2D |
ThreeDScene | 3D animations | 3D rotation |
ZoomedScene | Picture-in-picture zoom | Zoom inset |
Mobject Hierarchy
Base Classes
Mobject (base)
├── VMobject (vector)
│ ├── VGroup
│ ├── Text, Tex, MathTex
│ ├── Circle, Square, Rectangle
│ ├── Line, Arrow, DashedLine
│ ├── Axes, NumberPlane
│ └── Graph, BarChart
├── ImageMobject
├── Group
└── ValueTracker
VGroup - Container for VMobjects
group = VGroup(circle, square, triangle)
group[0]
group[-1]
group.add(new_element)
group.remove(element)
group.arrange(DOWN, buff=0.5)
group.arrange_in_grid(rows=2, cols=3)
Common Mobject Methods
obj.move_to(point)
obj.next_to(other, RIGHT)
obj.to_edge(UP, buff=0.5)
obj.to_corner(UL)
obj.shift(UP * 2)
obj.align_to(other, LEFT)
obj.scale(0.5)
obj.stretch(2, dim=0)
obj.rotate(PI/4)
obj.flip()
obj.set_color(RED)
obj.set_fill(BLUE, opacity=0.5)
obj.set_stroke(WHITE, width=2)
obj.get_center()
obj.get_width()
obj.get_height()
obj.get_corner(UR)
Coordinate Systems
Default Frame
Frame dimensions: 14.22 x 8 units
Origin: center of screen
+X: right, +Y: up, +Z: out of screen
config.frame_width # ~14.22
config.frame_height # 8
Direction Constants
UP = [0, 1, 0]
DOWN = [0, -1, 0]
LEFT = [-1, 0, 0]
RIGHT = [1, 0, 0]
UL = UP + LEFT
UR = UP + RIGHT
DL = DOWN + LEFT
DR = DOWN + RIGHT
ORIGIN = [0, 0, 0]
Axes Coordinate System
axes = Axes(
x_range=[-3, 3, 1],
y_range=[-2, 2, 0.5],
x_length=8,
y_length=5,
)
point = axes.c2p(x, y)
x, y = axes.p2c(point)
graph = axes.plot(lambda x: x**2, color=BLUE)
point = graph.get_point_from_function(x_value)
Animation Lifecycle
Basic Animation
self.play(animation, run_time=1)
self.wait(duration)
Animation Parameters
self.play(
Create(circle),
run_time=2,
rate_func=smooth,
lag_ratio=0.5,
)
Animation Sequencing
self.play(anim1, anim2)
self.play(Succession(anim1, anim2, anim3))
self.play(LaggedStart(anim1, anim2, anim3, lag_ratio=0.3))
self.play(anim1)
self.wait(0.5)
self.play(anim2)
Rate Functions
Rate functions control animation timing curves.
Built-in Rate Functions
rate_func=linear
rate_func=smooth
rate_func=rush_into
rate_func=rush_from
rate_func=there_and_back
rate_func=there_and_back_with_pause
rate_func=exponential_decay
rate_func=lingering
rate_func=running_start
rate_func=double_smooth
Custom Rate Function
def my_rate_func(t):
"""
t: progress from 0 to 1
return: modified progress 0 to 1
"""
return t ** 2
self.play(Create(circle), rate_func=my_rate_func)
Configuration
Config Object
from manim import config
config.frame_width
config.frame_height
config.pixel_width
config.pixel_height
config.quality
config.frame_rate
config.background_color
Quality Settings
| Quality | Resolution | FPS |
|---|
| low | 480p | 15 |
| medium | 720p | 30 |
| high | 1080p | 60 |
| fourk | 2160p | 60 |
Key Imports
from manim import *
from manim import (
Scene,
Circle, Square, Rectangle,
Text, Tex, MathTex,
VGroup,
Create, Write, FadeIn, FadeOut,
Transform, ReplacementTransform,
Axes, NumberPlane,
ValueTracker,
always_redraw,
config,
UP, DOWN, LEFT, RIGHT, ORIGIN,
BLUE, RED, GREEN, YELLOW, WHITE,
)
Common Patterns
Add Then Animate
circle = Circle()
self.add(circle)
self.play(circle.animate.shift(RIGHT * 2))
self.play(circle.animate.scale(0.5).set_color(RED))
Animate Method
self.play(
obj.animate.shift(UP * 2),
obj.animate.scale(1.5),
obj.animate.set_color(RED),
obj.animate.rotate(PI/4),
)
self.play(
obj.animate.shift(UP).scale(0.5).set_color(BLUE)
)
Copy Pattern
copy = original.copy()
self.play(Transform(original, target))
self.play(ReplacementTransform(original, target))