0
# Rendering System
1
2
Backend rendering engines and output management supporting multiple formats, quality settings, and both real-time preview and high-quality export. The rendering system converts mathematical descriptions into visual output through specialized engines optimized for different use cases.
3
4
## Capabilities
5
6
### Configuration System
7
8
Global configuration management controlling rendering parameters, output settings, and quality preferences.
9
10
```python { .api }
11
# Global configuration access
12
from manim import config
13
14
# Core rendering settings
15
config.pixel_height: int # Vertical resolution in pixels
16
config.pixel_width: int # Horizontal resolution in pixels
17
config.frame_rate: float # Animation frame rate (fps)
18
config.frame_height: float # Scene height in Manim units
19
config.frame_width: float # Scene width in Manim units
20
21
# Quality presets
22
config.quality: str # Quality preset name
23
config.low_quality: bool # 480p15 (854x480, 15fps)
24
config.medium_quality: bool # 720p30 (1280x720, 30fps)
25
config.high_quality: bool # 1080p60 (1920x1080, 60fps)
26
config.production_quality: bool # 1440p60 (2560x1440, 60fps)
27
config.fourk_quality: bool # 4K60 (3840x2160, 60fps)
28
29
# Output settings
30
config.output_file: str # Output filename
31
config.scenes_names: list[str] # Scenes to render
32
config.output_dir: str # Output directory path
33
config.images_dir: str # Directory for image outputs
34
config.video_dir: str # Directory for video outputs
35
config.partial_movie_dir: str # Directory for partial renders
36
37
# Rendering behavior
38
config.preview: bool # Whether to preview after rendering
39
config.show_in_file_browser: bool # Open output folder when done
40
config.write_to_movie: bool # Generate video output
41
config.write_all: bool # Render all scenes in file
42
config.save_last_frame: bool # Save final frame as image
43
config.save_pngs: bool # Save individual frame images
44
config.save_as_gif: bool # Generate GIF output
45
config.transparent: bool # Use transparent background
46
47
# Performance settings
48
config.disable_caching: bool # Disable animation caching
49
config.flush_cache: bool # Clear existing cache
50
config.tex_template: str # LaTeX template for math rendering
51
config.verbosity: str # Logging level ("DEBUG", "INFO", "WARNING", "ERROR")
52
53
# Renderer selection
54
config.renderer: str # "cairo" or "opengl"
55
config.use_opengl_renderer: bool # Whether to use OpenGL
56
57
# Background and styling
58
config.background_color: str # Scene background color
59
config.background_opacity: float # Background opacity
60
```
61
62
### Cairo Renderer
63
64
High-quality 2D renderer using Cairo graphics library for precise vector graphics and publication-quality output.
65
66
```python { .api }
67
class CairoRenderer:
68
"""
69
2D renderer using Cairo graphics library for high-quality vector output.
70
71
Optimized for precise mathematical graphics, text rendering, and
72
publication-quality static images and animations.
73
"""
74
75
def __init__(
76
file_writer_class: type = SceneFileWriter,
77
camera_class: type = Camera,
78
skip_animations: bool = False,
79
**kwargs
80
) -> None:
81
"""
82
Parameters:
83
- file_writer_class: Class for handling file output
84
- camera_class: Camera class for viewport management
85
- skip_animations: Whether to skip animation rendering
86
"""
87
88
def init_scene(scene: Scene) -> None:
89
"""Initialize renderer for a specific scene."""
90
91
def play(
92
scene: Scene,
93
*animations: Animation,
94
**kwargs
95
) -> None:
96
"""
97
Render a play() call with given animations.
98
99
Handles animation caching, frame generation, and file output
100
for the specified animations.
101
102
Parameters:
103
- scene: Scene being rendered
104
- *animations: Animations to render
105
"""
106
107
def scene_finished(scene: Scene) -> None:
108
"""Clean up after scene rendering is complete."""
109
110
def render(
111
scene: Scene,
112
time: float,
113
moving_mobjects: list[Mobject]
114
) -> None:
115
"""
116
Render a single frame at specified time.
117
118
Parameters:
119
- scene: Scene to render
120
- time: Animation time for the frame
121
- moving_mobjects: Mobjects being animated
122
"""
123
124
def update_skipping_status(self) -> None:
125
"""Update animation skipping based on configuration."""
126
127
def get_frame(self) -> np.ndarray:
128
"""
129
Get current rendered frame as pixel array.
130
131
Returns:
132
- np.ndarray: Frame pixels in RGBA format
133
"""
134
135
def save_static_frame_data(
136
scene: Scene,
137
static_image: np.ndarray
138
) -> None:
139
"""Save frame data for static image output."""
140
141
@property
142
def camera() -> Camera:
143
"""Camera used by this renderer."""
144
145
@property
146
def file_writer() -> SceneFileWriter:
147
"""File writer for output generation."""
148
149
@property
150
def time() -> float:
151
"""Current animation time."""
152
153
@property
154
def num_plays() -> int:
155
"""Number of play() calls rendered so far."""
156
```
157
158
### OpenGL Renderer
159
160
Real-time 3D renderer using OpenGL for interactive preview and 3D scene rendering with hardware acceleration.
161
162
```python { .api }
163
class OpenGLRenderer:
164
"""
165
Hardware-accelerated 3D renderer using OpenGL for real-time rendering.
166
167
Optimized for 3D scenes, real-time preview, and interactive development
168
with support for lighting, shading, and complex 3D transformations.
169
"""
170
171
def __init__(
172
file_writer_class: type = SceneFileWriter,
173
skip_animations: bool = False,
174
**kwargs
175
) -> None:
176
"""
177
Parameters:
178
- file_writer_class: Class for handling file output
179
- skip_animations: Whether to skip animation rendering
180
"""
181
182
def init_scene(scene: Scene) -> None:
183
"""Initialize OpenGL context and scene resources."""
184
185
def play(
186
scene: Scene,
187
*animations: Animation,
188
**kwargs
189
) -> None:
190
"""Render animations using OpenGL pipeline."""
191
192
def scene_finished(scene: Scene) -> None:
193
"""Clean up OpenGL resources after scene completion."""
194
195
def render(
196
scene: Scene,
197
time: float,
198
moving_mobjects: list[Mobject]
199
) -> None:
200
"""
201
Render frame using OpenGL shaders and buffers.
202
203
Parameters:
204
- scene: Scene to render
205
- time: Animation time
206
- moving_mobjects: Objects being animated
207
"""
208
209
def refresh_static_mobjects(self) -> None:
210
"""Refresh static mobjects in GPU buffers."""
211
212
def add_mobjects_to_buffer(
213
*mobjects: Mobject
214
) -> None:
215
"""Add mobjects to OpenGL rendering buffers."""
216
217
def clear_buffer(self) -> None:
218
"""Clear OpenGL rendering buffers."""
219
220
@property
221
def camera() -> Camera:
222
"""OpenGL camera for 3D rendering."""
223
224
@property
225
def window() -> Window:
226
"""OpenGL window for rendering context."""
227
228
class Window:
229
"""
230
OpenGL window management for interactive rendering and preview.
231
232
Handles window creation, event processing, and real-time display
233
for development and interactive exploration.
234
"""
235
236
def __init__(
237
scene: Scene,
238
**kwargs
239
) -> None:
240
"""
241
Parameters:
242
- scene: Scene to display in window
243
"""
244
245
def setup(self) -> None:
246
"""Initialize OpenGL window and context."""
247
248
def tear_down(self) -> None:
249
"""Clean up window and OpenGL resources."""
250
251
def capture_frame(self) -> np.ndarray:
252
"""
253
Capture current window contents as image.
254
255
Returns:
256
- np.ndarray: Window pixels in RGBA format
257
"""
258
259
def should_close(self) -> bool:
260
"""Check if window should be closed."""
261
262
def swap_buffers(self) -> None:
263
"""Swap front and back buffers for display."""
264
265
def poll_events(self) -> None:
266
"""Process window events and user input."""
267
```
268
269
### File Output System
270
271
File writing and output management for various formats including video, images, and data export.
272
273
```python { .api }
274
class SceneFileWriter:
275
"""
276
File output management for rendered scenes supporting multiple formats.
277
278
Handles video encoding, image export, caching, and file organization
279
with support for various output formats and quality settings.
280
"""
281
282
def __init__(
283
renderer: CairoRenderer | OpenGLRenderer,
284
scene_name: str,
285
**kwargs
286
) -> None:
287
"""
288
Parameters:
289
- renderer: Renderer producing the output
290
- scene_name: Name of scene being rendered
291
"""
292
293
def write_opengl_frame(
294
frame: np.ndarray,
295
num_frames: int = 1
296
) -> None:
297
"""
298
Write OpenGL-rendered frame to output.
299
300
Parameters:
301
- frame: Frame pixel data
302
- num_frames: Number of times to repeat frame
303
"""
304
305
def save_final_image(
306
image: np.ndarray
307
) -> None:
308
"""
309
Save final frame as static image.
310
311
Parameters:
312
- image: Final frame to save
313
"""
314
315
def finish(self) -> None:
316
"""Finalize output file and clean up resources."""
317
318
def add_partial_movie_file(
319
hash_animation: str
320
) -> None:
321
"""
322
Add cached animation segment to final output.
323
324
Parameters:
325
- hash_animation: Hash identifying cached animation
326
"""
327
328
def is_already_cached(
329
hash_animation: str
330
) -> bool:
331
"""
332
Check if animation is already cached.
333
334
Parameters:
335
- hash_animation: Animation hash to check
336
337
Returns:
338
- bool: True if animation is cached
339
"""
340
341
def combine_partial_movie_files(self) -> None:
342
"""Combine partial movie files into final output."""
343
344
def clean_cache(self, **kwargs) -> None:
345
"""Clean animation cache files."""
346
347
# Output format methods
348
def save_as_png(
349
frame: np.ndarray,
350
filename: str
351
) -> None:
352
"""Save frame as PNG image."""
353
354
def save_as_gif(
355
frames: list[np.ndarray],
356
filename: str,
357
fps: float = 15
358
) -> None:
359
"""Save frames as animated GIF."""
360
361
def init_audio(self) -> None:
362
"""Initialize audio processing for video output."""
363
364
def add_audio_segment(
365
audio_segment: AudioSegment
366
) -> None:
367
"""Add audio segment to video output."""
368
369
@property
370
def output_name() -> str:
371
"""Final output filename."""
372
373
@property
374
def partial_movie_files() -> list[str]:
375
"""List of partial movie files for combining."""
376
```
377
378
### Quality and Format Control
379
380
Comprehensive quality presets and format options for different output requirements and use cases.
381
382
```python { .api }
383
# Quality preset configurations
384
QUALITIES = {
385
"fourk_quality": {
386
"pixel_height": 2160,
387
"pixel_width": 3840,
388
"frame_rate": 60,
389
},
390
"production_quality": {
391
"pixel_height": 1440,
392
"pixel_width": 2560,
393
"frame_rate": 60,
394
},
395
"high_quality": {
396
"pixel_height": 1080,
397
"pixel_width": 1920,
398
"frame_rate": 60,
399
},
400
"medium_quality": {
401
"pixel_height": 720,
402
"pixel_width": 1280,
403
"frame_rate": 30,
404
},
405
"low_quality": {
406
"pixel_height": 480,
407
"pixel_width": 854,
408
"frame_rate": 15,
409
},
410
"example_quality": {
411
"pixel_height": 480,
412
"pixel_width": 854,
413
"frame_rate": 15,
414
}
415
}
416
417
# Configuration helper functions
418
def set_quality(quality_name: str) -> None:
419
"""
420
Set rendering quality using preset.
421
422
Parameters:
423
- quality_name: Name of quality preset
424
"""
425
426
def set_custom_quality(
427
pixel_width: int,
428
pixel_height: int,
429
frame_rate: float = 30
430
) -> None:
431
"""
432
Set custom quality parameters.
433
434
Parameters:
435
- pixel_width: Horizontal resolution
436
- pixel_height: Vertical resolution
437
- frame_rate: Animation frame rate
438
"""
439
440
def set_output_format(
441
format_type: str,
442
**kwargs
443
) -> None:
444
"""
445
Configure output format and settings.
446
447
Parameters:
448
- format_type: "mp4", "gif", "png", "mov", "webm"
449
"""
450
451
# Renderer selection
452
def use_cairo_renderer() -> None:
453
"""Switch to Cairo renderer for high-quality 2D output."""
454
455
def use_opengl_renderer() -> None:
456
"""Switch to OpenGL renderer for 3D and real-time rendering."""
457
458
# Background and styling
459
def set_background_color(color: str) -> None:
460
"""Set scene background color."""
461
462
def set_transparent_background(transparent: bool = True) -> None:
463
"""Enable/disable transparent background."""
464
```
465
466
### Caching and Performance
467
468
Animation caching system and performance optimization tools for faster rendering and development iteration.
469
470
```python { .api }
471
# Cache management
472
def enable_caching() -> None:
473
"""Enable animation caching for faster re-rendering."""
474
475
def disable_caching() -> None:
476
"""Disable animation caching for fresh renders."""
477
478
def flush_cache() -> None:
479
"""Clear all cached animation data."""
480
481
def get_cache_dir() -> str:
482
"""Get path to animation cache directory."""
483
484
# Performance monitoring
485
class PerformanceMonitor:
486
"""
487
Performance monitoring and profiling for rendering optimization.
488
489
Tracks rendering times, memory usage, and bottlenecks
490
to help optimize scene performance.
491
"""
492
493
def start_profiling(self) -> None:
494
"""Begin performance profiling."""
495
496
def stop_profiling(self) -> dict:
497
"""
498
End profiling and get results.
499
500
Returns:
501
- dict: Performance statistics
502
"""
503
504
def get_render_stats(self) -> dict:
505
"""
506
Get rendering performance statistics.
507
508
Returns:
509
- dict: Timing and resource usage data
510
"""
511
512
# Memory management
513
def optimize_memory_usage() -> None:
514
"""Apply memory optimization settings."""
515
516
def get_memory_usage() -> dict:
517
"""
518
Get current memory usage statistics.
519
520
Returns:
521
- dict: Memory usage information
522
"""
523
```
524
525
## Usage Examples
526
527
### Basic Rendering Configuration
528
529
```python
530
from manim import *
531
532
# Set quality and output preferences
533
config.quality = "high_quality" # 1080p60
534
config.output_file = "my_animation.mp4"
535
config.preview = True
536
537
class MyScene(Scene):
538
def construct(self):
539
circle = Circle()
540
self.play(Create(circle))
541
```
542
543
### Custom Quality Settings
544
545
```python
546
# Custom resolution and frame rate
547
config.pixel_width = 1600
548
config.pixel_height = 900
549
config.frame_rate = 24
550
551
# Background settings
552
config.background_color = BLACK
553
config.background_opacity = 1.0
554
555
# Output format
556
config.save_as_gif = True
557
config.transparent = False
558
```
559
560
### Multiple Output Formats
561
562
```python
563
class MultiFormatExample(Scene):
564
def construct(self):
565
# Configure for multiple outputs
566
config.write_to_movie = True # Generate MP4
567
config.save_last_frame = True # Save final PNG
568
config.save_as_gif = True # Generate GIF
569
config.save_pngs = True # Save all frames
570
571
text = Text("Multi-format output")
572
self.play(Write(text))
573
self.wait(2)
574
```
575
576
### Renderer Selection
577
578
```python
579
# Use Cairo for high-quality 2D
580
config.renderer = "cairo"
581
config.quality = "production_quality"
582
583
class HighQuality2D(Scene):
584
def construct(self):
585
equation = MathTex(r"\int_0^1 x^2 dx = \frac{1}{3}")
586
self.play(Write(equation))
587
588
# Use OpenGL for 3D and real-time
589
config.renderer = "opengl"
590
config.preview = True
591
592
class Interactive3D(ThreeDScene):
593
def construct(self):
594
cube = Cube()
595
self.add(cube)
596
self.interactive_embed() # Interactive mode
597
```
598
599
### Performance Optimization
600
601
```python
602
# Optimize for faster rendering
603
config.low_quality = True # Lower resolution
604
config.disable_caching = False # Enable caching
605
config.skip_animations = False # Don't skip animations
606
607
# Memory optimization
608
config.max_files_cached = 100 # Limit cache size
609
config.flush_cache = False # Keep cache between runs
610
611
class OptimizedScene(Scene):
612
def construct(self):
613
# Use simpler objects for better performance
614
dots = VGroup(*[Dot() for _ in range(100)])
615
dots.arrange_in_grid(10, 10)
616
617
# Batch animations for efficiency
618
self.play(
619
LaggedStart(
620
*[FadeIn(dot) for dot in dots],
621
lag_ratio=0.01
622
)
623
)
624
```
625
626
### Advanced Output Control
627
628
```python
629
class AdvancedOutputExample(Scene):
630
def setup(self):
631
# Set up custom file writer
632
config.output_dir = "custom_output"
633
config.video_dir = "videos"
634
config.images_dir = "frames"
635
636
# Configure video encoding
637
config.video_codec = "libx264"
638
config.audio_codec = "aac"
639
640
def construct(self):
641
# Scene with custom output settings
642
title = Title("Advanced Output")
643
644
# Add background music (if available)
645
# self.add_sound("background.wav")
646
647
self.play(Write(title))
648
649
# Save specific frames
650
self.wait(1)
651
# Custom frame saving would be done through file_writer
652
653
self.play(FadeOut(title))
654
```
655
656
### Development and Debugging
657
658
```python
659
# Development settings for faster iteration
660
config.preview = True # Auto-preview
661
config.low_quality = True # Faster rendering
662
config.save_last_frame = True # Quick feedback
663
config.verbosity = "INFO" # Detailed logging
664
665
# Debug rendering issues
666
config.disable_caching = True # Fresh renders
667
config.write_all = False # Single scene only
668
config.dry_run = True # Check without rendering
669
670
class DebugScene(Scene):
671
def construct(self):
672
# Add debugging information
673
config.show_in_file_browser = True
674
675
text = Text("Debug Scene")
676
self.add(text)
677
678
# Performance monitoring
679
import time
680
start_time = time.time()
681
682
self.play(Indicate(text))
683
684
render_time = time.time() - start_time
685
logger.info(f"Render time: {render_time:.2f}s")
686
```
687
688
### Batch Rendering
689
690
```python
691
# Configuration for batch processing
692
config.write_all = True # Render all scenes
693
config.output_dir = "batch_output"
694
695
# Render multiple scenes with different settings
696
scenes_config = [
697
{"scene": "Scene1", "quality": "low_quality"},
698
{"scene": "Scene2", "quality": "high_quality"},
699
{"scene": "Scene3", "quality": "production_quality"}
700
]
701
702
for scene_cfg in scenes_config:
703
with tempconfig({"quality": scene_cfg["quality"]}):
704
# Render scene with specific quality
705
pass
706
```