Animation engine for explanatory math videos with programmatic mathematical visualization capabilities
—
The scene framework provides the foundation for creating animations in ManimGL. Scenes manage the animation timeline, coordinate system, object lifecycle, and provide the interface for building complex mathematical animations.
The core Scene class provides the fundamental animation framework with timeline control, object management, and rendering coordination.
class Scene:
def __init__(self, **kwargs):
"""
Initialize a new scene.
Parameters:
- camera_class: Camera class to use (default: Camera)
- skip_animations: bool, skip all animations
- leave_progress_bars: bool, keep progress bars after completion
"""
def construct(self):
"""
Override this method to define the scene's animation sequence.
This is where you add objects and animations.
"""
def setup(self):
"""
Called before construct(). Override for scene initialization.
"""
def play(self, *animations, **kwargs):
"""
Play one or more animations simultaneously.
Parameters:
- animations: Animation objects to play
- run_time: float, duration of animations (default: 1.0)
- rate_func: function, timing function for animations
- lag_ratio: float, stagger start times when multiple animations
Returns:
None
"""
def wait(self, duration=None, **kwargs):
"""
Pause the animation for specified duration.
Parameters:
- duration: float, seconds to wait (default: scene's default_wait_time)
Returns:
None
"""
def add(self, *mobjects):
"""
Add mobjects to the scene without animation.
Parameters:
- mobjects: Mobject instances to add
Returns:
None
"""
def remove(self, *mobjects):
"""
Remove mobjects from the scene without animation.
Parameters:
- mobjects: Mobject instances to remove
Returns:
None
"""
def bring_to_front(self, *mobjects):
"""
Bring mobjects to the front of the scene (highest z-index).
Parameters:
- mobjects: Mobject instances to bring forward
Returns:
None
"""
def bring_to_back(self, *mobjects):
"""
Send mobjects to the back of the scene (lowest z-index).
Parameters:
- mobjects: Mobject instances to send back
Returns:
None
"""Enhanced scene class for real-time development and interaction with mouse and keyboard controls.
class InteractiveScene(Scene):
def __init__(self, **kwargs):
"""
Initialize interactive scene with real-time controls.
Additional features:
- Mouse interaction
- Keyboard shortcuts
- Real-time object manipulation
- Live scene editing
"""
def on_key_press(self, symbol, modifiers):
"""
Handle keyboard input during scene playback.
Parameters:
- symbol: Key symbol pressed
- modifiers: Modifier keys (Ctrl, Shift, etc.)
Returns:
None
"""
def on_mouse_press(self, point, button, mods):
"""
Handle mouse clicks during scene playback.
Parameters:
- point: np.array, click position in scene coordinates
- button: Mouse button pressed
- mods: Modifier keys
Returns:
None
"""
def on_mouse_drag(self, point, d_point, buttons, modifiers):
"""
Handle mouse dragging.
Parameters:
- point: np.array, current mouse position
- d_point: np.array, change in position
- buttons: Mouse buttons held
- modifiers: Modifier keys
Returns:
None
"""Specialized scene class optimized for three-dimensional animations with enhanced camera controls and depth management.
class ThreeDScene(Scene):
def __init__(self, **kwargs):
"""
Initialize 3D scene with specialized camera.
Parameters:
- camera_class: Defaults to ThreeDCamera
"""
def set_camera_orientation(self, phi=None, theta=None, gamma=None, **kwargs):
"""
Set the 3D camera orientation.
Parameters:
- phi: float, angle around x-axis (latitude)
- theta: float, angle around z-axis (longitude)
- gamma: float, rotation around viewing axis
- **kwargs: Additional camera parameters
Returns:
None
"""
def begin_ambient_camera_rotation(self, rate=0.02):
"""
Start continuous camera rotation.
Parameters:
- rate: float, rotation speed in radians per frame
Returns:
None
"""
def stop_ambient_camera_rotation(self):
"""
Stop continuous camera rotation.
Returns:
None
"""
def move_camera(self, phi=None, theta=None, gamma=None, **kwargs):
"""
Animate camera movement to new orientation.
Parameters:
- phi: float, target latitude angle
- theta: float, target longitude angle
- gamma: float, target rotation angle
- **kwargs: Animation parameters (run_time, rate_func)
Returns:
Animation
"""Camera classes for managing viewport, coordinate transformations, and rendering.
class Camera:
def __init__(self, **kwargs):
"""
Initialize 2D camera.
Parameters:
- background_color: Color for scene background
- frame_height: float, height of viewable area
- frame_width: float, width of viewable area
"""
def capture(self, *mobjects, **kwargs):
"""
Render mobjects to image.
Parameters:
- mobjects: Objects to render
Returns:
PIL.Image
"""
class ThreeDCamera(Camera):
def __init__(self, **kwargs):
"""
Initialize 3D camera with depth and perspective.
Parameters:
- phi: float, initial latitude angle
- theta: float, initial longitude angle
- distance: float, camera distance from origin
"""
def set_euler_angles(self, phi, theta, gamma):
"""
Set camera orientation using Euler angles.
Parameters:
- phi: float, rotation around x-axis
- theta: float, rotation around z-axis
- gamma: float, rotation around viewing axis
Returns:
None
"""Window class for display management and user interaction.
class Window:
def __init__(self, scene, **kwargs):
"""
Initialize display window.
Parameters:
- scene: Scene instance to display
- size: tuple, window dimensions (width, height)
- title: str, window title
- resizable: bool, whether window can be resized
"""
def show(self):
"""
Display the window and start the main loop.
Returns:
None
"""
def close(self):
"""
Close the window.
Returns:
None
"""from manimgl import *
class BasicScene(Scene):
def construct(self):
# Create objects
circle = Circle(color=BLUE)
text = Text("Hello World")
# Add to scene
self.add(circle, text)
self.wait()
# Animate
self.play(FadeOut(circle))
self.play(Transform(text, Text("Goodbye!")))
self.wait()class InteractiveExample(InteractiveScene):
def construct(self):
circle = Circle(color=BLUE)
self.add(circle)
def on_key_press(self, symbol, modifiers):
if symbol == ord('r'):
# Red circle on 'r' key
self.mobjects[0].set_color(RED)class ThreeDExample(ThreeDScene):
def construct(self):
axes = ThreeDAxes()
surface = ParametricSurface(
lambda u, v: [u, v, u**2 + v**2]
)
self.set_camera_orientation(phi=75*DEGREES)
self.add(axes, surface)
self.begin_ambient_camera_rotation()
self.wait(5)Install with Tessl CLI
npx tessl i tessl/pypi-manimgldocs