Build your first 4D voxel video with spatialstudio. This Python toolkit lets you create, encode, and export stunning volumetric content. You'll learn the fundamentals by creating a simple animated red cube that moves through 3D space over time.
By the end of this tutorial, you'll have generated example.splv
- your first spatial video file that showcases a red cube moving through 3D space. This seemingly simple creation will teach you the core concepts you need to build far more complex voxel animations.
First, let's get spatialstudio installed on your system. We recommend using Python 3.8 or higher:
pip install spatialstudio
Once installed, you'll have access to the entire spatialstudio API for creating volumetric content.
Let's dive right in and create something visual! The following script demonstrates the core spatialstudio workflow. We'll create a spatial that shows a red cube moving through 3D space over time:
from spatialstudio import splv
# Set up the voxel volume dimensions
w = h = d = 128 # Create a 128x128x128 voxel space
# Create an encoder to generate our .splv file
enc = splv.Encoder(w, h, d, framerate=30.0, outputPath="example.splv")
# Generate 100 frames of animation
for i in range(100):
# Create a new frame for this time step
f = splv.Frame(w, h, d)
# Draw a 20x20x20 red cube that moves diagonally
f.fill(i, i, i, i+20, i+20, i+20, voxel=(255, 0, 0))
# Encode this frame into our video
enc.encode(f)
# Finalize and save the .splv file
enc.finish()
Let's break down what this code does step by step:
w = h = d = 128
We define our voxel space as a 128×128×128 cube. This gives us plenty of room for our animation while keeping the file size manageable.
enc = splv.Encoder(w, h, d, framerate=30.0, outputPath="example.splv")
The encoder
is your gateway to creating .splv
files. It handles:
.splv
filefor i in range(100):
f = splv.Frame(w, h, d)
f.fill(i, i, i, i+20, i+20, i+20, voxel=(255, 0, 0))
enc.encode(f)
This loop creates 100 frames of animation. For each frame, we create a fresh frame
object and use the fill()
method to draw a 20×20×20 red cube. As i
increases, the cube moves diagonally through 3D space.
enc.finish()
The finish()
method completes the encoding process and writes the final .splv
file to disk.
Save the code above as example.py
and run it:
python example.py
You should see encoding progress information, and when complete, you'll have example.splv
in your current directory. This file contains your first 4D voxel video!
Want to see your creation in action? Use our free preview tool to view your example.splv
file directly in your browser!
Spatialstudio uses a standard 3D coordinate system:
When we call f.fill(i, i, i, i+20, i+20, i+20, voxel=(255, 0, 0))
, we're defining a rectangular region from point (i, i, i)
to point (i+20, i+20, i+20)
and filling it with red voxels.
Congratulations! You've successfully:
.splv
voxel video filefill()
methodYour example.splv
file is a fully-functional 4D voxel video that can be played back using spatialjs in web browsers or other compatible players.
Now that you've mastered the basics, you're ready to explore more sophisticated techniques! In our next tutorial, "Spatialstudio encoding basics and techniques", you'll learn how to create complex multi-object animations, implement advanced movement patterns using mathematical functions, and optimize your encoding settings for different use cases.
Get ready to take your voxel creation skills to the next level with professional-grade techniques and workflows!