examples

76 interactive examples

hover example to preview

1

Snake 3D

audio
2

Creating Yoyo Animation

3

Creating Wooden Planks Animation

4

Creating Windstorm Animation

5

Creating Windmill Animation

6

Creating Whale Animation

7

Creating Waterfall Animation

8

Creating Volcano Animation

9

Creating Vaporwave Animation

10

Creating Tree Animation

11

Creating Tornado Animation

12

Creating Sun Animation

13

Creating Stone Wall Animation

14

Creating Spring Animation

15

Creating Spider Animation

16

Creating Spaceship Animation

17

Creating Solar System Educational Animation

18

Creating Solar System Animation

19

Creating Snowglobe Animation

20

Creating Snake Animation

21

Creating Sailboat Animation

22

Creating Robot Animation

23

Creating River Rapids Animation

24

Creating Rainbow Animation

25

Creating Prism Animation

26

Creating Pinwheel Animation

27

Creating Pine Tree Animation

28

Creating Phoenix Animation

29

Creating Pendulum Animation

30

Creating Octopus Animation

31

Creating Mushroom Animation

32

Creating Metronome Animation

33

Creating Meteor Animation

34

Creating Metal Plates Animation

35

Creating Lissajous Animation

36

Creating Lightning Animation

37

Creating Lighthouse Animation

38

Creating Lava Lamp Animation

39

Creating Lantern Animation

40

Creating Kite Animation

41

Creating Kaleidoscope Animation

42

Creating Jellyfish Animation

43

Creating Inner Planets Orbit Animation

44

Creating Hourglass Animation

45

Creating Hive Animation

46

Creating Grass Animation

47

Creating Geyser Animation

48

Creating Gear Animation

49

Creating Fountain Animation

50

Creating Flower Animation

51

Creating Fish Animation

52

Creating Fireworks Animation

53

Creating Fern Animation

54

Creating Earth Orbit Animation

55

Creating Dragon Animation

56

Creating Disco Ball Animation

57

Creating Demon Animation

58

Creating Crystallization Animation

59

Creating Crystal Animation

60

Creating Cosmic Formation Animation

61

Creating Coral Reef Animation

62

Creating Compass Animation

63

Creating Cloud Animation

64

Creating Clock Animation

65

Creating Castle Animation

66

Creating Carousel Animation

67

Creating Candle Animation

68

Creating Campfire Animation

69

Creating Cactus Animation

70

Creating Butterfly Animation

71

Creating Bush Animation

72

Creating Brick Wall Animation

73

Creating Blackhole Animation

74

Creating Beehive Animation

75

Creating Aurora Animation

76

🎈 Floating Balloons

hover for preview

Creating Snake Animation

published on 8/21/2025
interactive example

Snake - 3D Voxel Animation Learning Example

This guide walks you through how to generate a looping 3D voxel animation of a snake using SpatialStudio. The script creates a slithering snake that moves through a cubic 3D space with realistic body motion, then saves the animation to a .splv file.


What this script does

  • Creates a 3D scene of size 128×128×128
  • Spawns 1 animated snake with:
    • A segmented body that follows a curved path
    • Realistic slithering motion with sine-wave undulation
    • Gradient coloring from head to tail
    • Smooth body segments that follow the head
  • Animates the snake moving in a figure-8 pattern for 8 seconds at 30 FPS
  • Outputs the file snake.splv that you can play in your viewer

How it works (simplified)

  1. Voxel volume Each frame is a 3D grid filled with RGBA values (SIZE × SIZE × SIZE × 4).

  2. Snake body segments The snake consists of multiple spherical segments that follow a calculated path, each slightly smaller than the previous.

  3. Path calculation The snake follows a parametric figure-8 curve in 3D space, with additional vertical undulation for realistic motion.

  4. Body following Each segment follows the position of the segment in front of it with a time delay, creating natural snake-like movement.

  5. Color gradient The snake uses a green-to-yellow gradient from head to tail, making it easy to see the direction of movement.

  6. Animation loop A normalized time variable t cycles from 0 → 2π, ensuring the motion loops smoothly.

  7. Encoding Frames are passed into splv.Encoder, which writes them into the .splv video file.


Try it yourself

Install requirements first:

pip install spatialstudio numpy tqdm

Then copy this script into snake.py and run:

python snake.py

Full Script

import numpy as np
from spatialstudio import splv
from tqdm import tqdm

# Scene setup
SIZE, FPS, SECONDS = 128, 30, 8
FRAMES = FPS * SECONDS
CENTER_X = CENTER_Y = CENTER_Z = SIZE // 2
OUT_PATH = "../outputs/snake.splv"

# Snake settings
SNAKE_LENGTH = 25
SEGMENT_SIZE = 3
SEGMENT_SPACING = 2.5
PATH_RADIUS = 30

def add_voxel(volume, x, y, z, color):
    if 0 <= x < SIZE and 0 <= y < SIZE and 0 <= z < SIZE:
        volume[x, y, z, :3] = color
        volume[x, y, z, 3] = 255

def calculate_snake_position(t, segment_index):
    # Figure-8 path with time delay for each segment
    delayed_t = t - (segment_index * 0.1)
    
    # Figure-8 parametric equations
    x = PATH_RADIUS * np.sin(delayed_t)
    z = PATH_RADIUS * np.sin(delayed_t) * np.cos(delayed_t)
    
    # Vertical undulation for 3D movement
    y = 10 * np.sin(delayed_t * 2.0 + segment_index * 0.3)
    
    return CENTER_X + int(x), CENTER_Y + int(y), CENTER_Z + int(z)

def get_segment_color(segment_index):
    # Gradient from bright green (head) to yellow (tail)
    progress = segment_index / SNAKE_LENGTH
    red = int(255 * progress)
    green = 255
    blue = int(50 * (1 - progress))
    return (red, green, blue)

def generate_snake_segment(volume, cx, cy, cz, color, size):
    for dx in range(-size, size+1):
        for dy in range(-size, size+1):
            for dz in range(-size, size+1):
                distance = np.sqrt(dx*dx + dy*dy + dz*dz)
                if distance <= size:
                    # Add some texture variation
                    brightness = 1.0 - (distance / size) * 0.3
                    final_color = tuple(min(255, int(c * brightness)) for c in color)
                    add_voxel(volume, cx+dx, cy+dy, cz+dz, final_color)

def generate_snake_head(volume, cx, cy, cz, t):
    # Head is slightly larger and has eyes
    head_color = (0, 255, 0)  # Bright green
    head_size = SEGMENT_SIZE + 1
    
    # Generate head body
    generate_snake_segment(volume, cx, cy, cz, head_color, head_size)
    
    # Add eyes
    eye_color = (255, 255, 255)  # White eyes
    eye_offset = 2
    add_voxel(volume, cx-eye_offset, cy+eye_offset, cz+eye_offset, eye_color)
    add_voxel(volume, cx+eye_offset, cy+eye_offset, cz+eye_offset, eye_color)
    
    # Add pupils
    pupil_color = (0, 0, 0)  # Black pupils
    add_voxel(volume, cx-eye_offset, cy+eye_offset, cz+eye_offset+1, pupil_color)
    add_voxel(volume, cx+eye_offset, cy+eye_offset, cz+eye_offset+1, pupil_color)

def generate_snake(volume, t):
    snake_positions = []
    
    # Calculate all segment positions
    for i in range(SNAKE_LENGTH):
        x, y, z = calculate_snake_position(t, i)
        snake_positions.append((x, y, z))
    
    # Draw snake segments from tail to head
    for i in range(SNAKE_LENGTH-1, -1, -1):
        x, y, z = snake_positions[i]
        
        if i == 0:  # Head
            generate_snake_head(volume, x, y, z, t)
        else:  # Body segments
            color = get_segment_color(i)
            # Segments get slightly smaller towards the tail
            size = max(1, SEGMENT_SIZE - int(i * 0.05))
            generate_snake_segment(volume, x, y, z, color, size)

def add_ground_texture(volume):
    # Add some ground voxels for reference
    ground_color = (101, 67, 33)  # Brown ground
    for x in range(0, SIZE, 8):
        for z in range(0, SIZE, 8):
            if np.random.random() > 0.7:  # Sparse ground texture
                add_voxel(volume, x, 5, z, ground_color)

def generate_scene(volume, t):
    add_ground_texture(volume)
    generate_snake(volume, t)

# Initialize encoder
enc = splv.Encoder(SIZE, SIZE, SIZE, framerate=FPS, outputPath=OUT_PATH, motionVectors="off")

# Generate animation frames
for frame in tqdm(range(FRAMES), desc="Generating snake"):
    volume = np.zeros((SIZE, SIZE, SIZE, 4), dtype=np.uint8)
    t = (frame / FRAMES) * 2*np.pi
    generate_scene(volume, t)
    enc.encode(splv.Frame(volume, lrAxis="x", udAxis="y", fbAxis="z"))

enc.finish()
print(f"Created {OUT_PATH}")

Next steps

  • Change SNAKE_LENGTH to make the snake longer or shorter.
  • Modify PATH_RADIUS to make the snake's movement path larger or smaller.
  • Edit the get_segment_color() function for different color schemes.
  • Adjust SEGMENT_SPACING to make the snake more or less compressed.
  • Try different path equations in calculate_snake_position() for unique movement patterns (spiral, sine wave, etc.).
  • Add obstacles or terrain for the snake to navigate around.