Spiega documentation
- Knowledge sharing
- Portfolio
2d animations - manim
2d animations
We can create animations in software like blender and its dedicated workflow: blender animation.
Here we want to use a different approach by using the manim library which allows to create impressive animations.
We have a running script to run those simulations viz_graph.py.
Overview: manim - Mathematical Animation Engine
About manim
manim is mathematical animation engineering — a Python library for creating beautiful mathematical animations for presentations.
Key Features
| Feature | Purpose | |
|---|---|---|
| LaTeX Integration | Mathematical notation | AMSP |
| Scene Architecture | Modular scene organization | AMSP |
| Vector Graphics | Crisp animations for all sizes | AMSP |
| Custom Shaders | Advanced visual effects | AMSP |
| Interactive Mode | Live preview before rendering | AMSP |
| Web Export | Share via URL | AMSP |
| Audio Sync | Music synchronization | AMSP |
Use Cases for Blender Twin
- Presenting IoT architecture
- Visualizing sensor data flow
- Explaining BLE mesh networks
- Demonstrating MQTT pub/sub
- Showing hardware connectivity
Quick Start: Installation
Install manim
We install manim on ubuntu
# Ubuntu/Debian (Jetson required)sudo apt-get updatesudo apt-get install python3-pip build-essential ffmpeg libjpg-dev libjpeg-devpython3 -m pip install manimpython3 -m pip install manim-rl
Test the application .
# Verify installationpython3 -c "from manim import *; print('Manim installed!')"
Manim installed!
Version Requirements
# Check requirementspython3 --version # Python 3.8+manim --version # Manim 0.17.0+# Recommended packagespip install numpy matplotlib pillow coloramapip install imageio[ffmpeg]
Python 3.14.4Manim Community v0.20.1
Basic Animation Structure
Inside manim_animations.py we have the different classes we want to animate.
Scene Class Definition
We launch a simple splash screen
Render Commands
manim -pql manim_animations.py IntroScene# # Export to GIF#manim -pql manim_animations.py -o --write_to_movie=false
Advanced Animation Examples
1. IoT Sensor Network Visualizer
manim -pql manim_animations.py SensorNetworkScene
2. MQTT Pub/Sub Data Flow
manim -pql manim_animations.py MQTTFlowScene
3. Hardware Bridge Architecture
manim -pql manim_animations.py BridgeArchitecture
Complete Example: Full Animation Script
manim -pql manim_animations.py FullAnimation
Animation Patterns and Techniques
1. Object Animate
from manim import Circle, ScaleInPlace, MoveAlongPath, FadeOut, BLUEobject = Circle(color=BLUE)self.play(Create(object))self.play(ScaleInPlace(object, scale=2, run_time=1))self.play(MoveAlongPath(object, object.get_center()))self.play(FadeOut(object))
2. Write Effect
# Text animation effectseffects = [Write, # Basic writeFadeIn, # Fade inCreate, # Create (for Dashed/Arrow)Transform, # Transform between objects]for effect in effects:self.play(effect(text_object))
3. Replacement Transform
from manim import *# Create two objectsold_text = Text("Old", font_size=36)new_text = Text("New", font_size=36)# Animation transformself.play(ReplacementTransform(old_text, new_text),run_time=2)
4. Lag Sequence
# Create staggered animationobjects = VGroup(*[Text(str(i), font_size=24)for i in range(10)])objects.arrange(LEFT, buff=0.5)# Create with lagself.play(*(Create(obj)for obj in objects),lag_ratio=0.1)```
Color and Style Examples
Color Schemes
from manim import *import itertools# Create color schemescolors = {'IoT': COLOR_PALETTE,'Sensor': RED,'Audio': GREEN,'Network': BLUE,}# Generate themetheme = VPC(colors.values())# Use custom colorsred = REDblue = BLUEgreen = GREENyellow = YELLOWpurple = PURPLEwhite = WHITEblack = BLACK# Style texttext = Text("Blender Twin",color=THEME_COLORS,font_size=48)```
Style Parameters
# Font settingstext = Text("Title",font="Arial",font_size=48,font_weight=300, # Thinshadow=False)# Gradient textgradient = Text("Gradient",color_gradient=[RED, BLUE],gradient_width=WIDTH)# Shadow textshadows = Text("Shadow",shadow=False,shadow_color=BLACK,shadow_type=2)# Background effectsbackground = Square(color=WHITE).set_stroke(color=BLACK,width=0.1)background_with_gradient = Square(color=WHITE).set_fill(color=BLUE,opacity=0.5)```
Audio and Music Integration
Add Background Music
from manim import *import osclass MusicScene(Scene):"""Scene with background music."""def construct(self):# Add background musicmusic = Sound.from_file("background.mp3",fade_in=2,fade_out=2)# Create scene contenttitle = Text("Blender Twin IoT",font_size=60)# Add to sceneself.play(Create(title))# Fade outself.play(FadeOut(title))```
Voiceover Integration
# VOX file recordingfrom manim import *class VoiceoverScene(Scene):"""Scene with voiceover narration."""def construct(self):# Create narrationnarration = VOX("Hello, this is the Blender Twin IoT project.","We are demonstrating the architecture.")self.play(VOX.narrate(narration))```
Export and Share
Export Options
# Render with high qualitymanim -pql IntroScene IntroScene# Export formatsmanim -pql -q IntroScene --write_to_movies=falsemanim -pql IntroScene IntroScene -o# Create GIF animationmanim -pql IntroScene IntroScene --write_to_movie=false -g# Export to different formatsmanim -pql IntroScene IntroScene --output_format=mp4manim -pql IntroScene IntroScene --output_format=gif```
Web Sharing
# Upload to YouTube/Vimeomanim -pql IntroScene IntroScene# Upload to YouTubepython3 -c "from manim import *import subprocesssubprocess.run(['ffmpeg'] + ['-i', 'output.mp4','-vf', 'fps=30,scale=1280:720','-y', 'output.mp4'])"```
Advanced Techniques
Custom Shaders
from manim import *import numpy as npclass ShaderScene(Scene):"""Scene with custom shaders."""def construct(self):# Create shader materialshader = ShaderMaterial(shader_code="""#version 330in vec4 vertex;in vec4 color;out vec4 vColor;void main() {vColor = color;}""",vertex_shader="""#version 330in vec4 vertex;out vec4 color;void main() {gl_Position = vertex;color = vec4(1.0);}""",frag_shader="""#version 330in vec4 color;out vec4 fragColor;void main() {fragColor = color;}""",)# Animate shader materialself.play(Create(shader))```
Particles and Effects
from manim import *import randomclass ParticleScene(Scene):"""Scene with particle effects."""def construct(self):# Create particle systemparticles = VGroup(*[Dot(point=np.random.random(3) * 3,radius=random.uniform(0.5, 1),color=BLUE)for _ in range(50)])# Animate particlesself.play(Create(particles))self.play(ReplaceByColor(particles,PARTICLE_EFFECT[1, 0.5, WHITE]),rate_func=ease_in)```
Blender Twin Specific Examples
1. MQTT Pub/Sub Flow
from manim import *class MQTTFlow(Scene):"""Demonstrate MQTT pub/sub for Blender Twin sensors."""def construct(self):# Create MQTT brokerbroker = Text("MQTT Broker", font_size=72, color=YELLOW)broker.to_edge(UR)# Create MQTT topicstopics = VGroup(Text("axoloti/audio", font_size=36),Text("daisy/sensor", font_size=36),Text("xiao/bat", font_size=36),)# Position topicstopics.arrange(DOWN, buff=0.5, aligned_edge=LEFT)topics[0].shift(LEFT * 4)topics[1].shift(LEFT * 4)topics[2].shift(LEFT * 4)# Create arrowsarrows = VGroup(*[Arrow(UP, DOWN, color=GREEN,buff=0.2)for i in range(3)])```
2. Hardware Configuration
from manim import *class HardwareConfig(Scene):"""Showcase all available components."""def construct(self):# Titletitle = Text("Blender Twin Hardware", font_size=60, color=YELLOW)title.to_edge(UP)self.play(Write(title))# Component nodesnodes = VGroup(Text("ESP32-WROOM", font_size=80, color=RED),Text("daisy_seed", font_size=80, color=GREEN),Text("axoloti", font_size=80, color=BLUE),Text("xiao", font_size=80, color=PURPLE),)# Configure positionsnodes[0].to_edge(DOWN)nodes[1].to_edge(DL)nodes[2].to_edge(UR)nodes[3].to_edge(DR)# Animate creationself.play(Create(nodes[0]))self.play(Create(nodes[1]))self.play(Create(nodes[2]))self.play(Create(nodes[3]))# Connect with linesself.play(Create(Arrow(nodes[0], nodes[1], color=YELLOW)))self.play(Create(Arrow(nodes[0], nodes[2], color=YELLOW)))self.play(Create(Arrow(nodes[0], nodes[3], color=YELLOW)))# Show connectionsself.wait(3)self.play(FadeOut(title))```
Tips for Better Animations
Best Practices
# 1. Keep animations simple# Create clear single-message flowsself.play(Create(obj), run_time=0.5)# 2. Use clear timing# Slow down for readingself.wait(1.5)# 3. Add context# Show what's happeningAddText(Tex("Temperature: 22.5°C"))# 4. Test iteratively# Preview before final rendermanim -pql IntroScene IntroScene# 5. Use appropriate fontstext = Text("Font", font="DejaVu Sans")# 6. Use appropriate colorstext = Text("Color", color=THEME_COLORS)# 7. Add captionscaption = Captions(text)self.play(Create(caption))```
Common Issues and Solutions
# Issue: Manim fails to render# Solution: Check dependenciespip install manim-legacy imageio[ffmpeg]# Issue: Low quality rendering# Solution: Increase quality flagsmanim -pql [scene] [scene_name]# Issue: No video output# Solution: Render with write_to_movie flagmanim -pql [scene] --write_to_movies# Issue: Slow rendering# Solution: Use low quality previewmanim -pql [scene]```
Performance Optimization
Render Settings
# High quality rendering (for final video)manim -pql -q IntroScene IntroScene# Low quality (for preview)manim -pql IntroScene IntroScene# Optimize for GPUmanim -v IntroScene IntroScene# Use multiprocessingenv PYTHONPATH MANIM_RENDERER=ffmpeg```
Summary
Key Points
| Concept | Usage | |
|---|---|---|
| Scene | Animation container | AMSP |
| Play | Animation method | AMSP |
| Create | Object creation | AMSP |
| Write | Text animation | AMSP |
| Replace | Object transformation | AMSP |
| Fading | Object fade in/out | AMSP |
| Arrow | Connection lines | AMSP |
| VGroup | Object grouping | AMSP |
Workflow
```
- Write Python animation script
- Test with manim -pql [Scene]
- Preview with manim [Scene]
- Render with manim [Scene] –write_to_movie=false
- Share via web upload
```
Resources
- main GitHub: https://github.com/3b1b/manim
- documentation: https://docs.manim.community/
- examples: https://github.com/3b1b/manim/blob/master/source/examples
Blender Twin Integration
All animations can be used in Blender Twin presentations to:
- Visualize IoT architecture
- Explain sensor connectivity
- Demonstrate mesh networking
- Present hardware configuration
- Share technical concepts
Next Steps
- Experiment with basic animations
- Create specific scene examples
- Add audio and music
- Share via web platforms
- Present to team