How to Write a Blender Script to Animate Shapes
Disclaimer: Much of the content of this post was not written by me. In the interest of time, I have entrusted the code writing, and blog post writing abilities of ChatGPT. Midjourney was utilized to provide the cover image of this post. You wouldn’t think of scolding a carpenter for using a hammer instead of his fist to drive a nail. ChatGPT is my hammer.
Creating and Animating Geometric Shapes in Blender with Python
In this tutorial, we’ll explore how to use Python scripting in Blender to create an animation featuring simple geometric shapes—such as a cube, sphere, and cone—spinning around different axes. We’ll also configure the camera to ensure all objects are visible and set up the scene for rendering.
Blender offers powerful tools for both manual and automated creation of 3D scenes, and with Python, you can take it a step further by scripting repetitive tasks, generating complex animations, and more.
Prerequisites
- Blender 4.2.1 (or later) installed
- Basic knowledge of Python and Blender’s interface
- A macOS system with an M1 chip (though the script works on other systems as well)
What We’re Creating
We’ll build a simple scene with three geometric shapes: a cube, a sphere, and a cone. These objects will spin around different axes, and we’ll set up a camera to ensure everything is visible in the final animation.
Step-by-Step Breakdown
1. Setting Up the Scene
First, we clear any existing mesh objects in the scene to start fresh. We also set up the render engine to use Eevee, as it’s less resource-intensive compared to Cycles and works well for real-time animations.
import bpy
import math
# Delete all existing mesh objects
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()
# Set up render settings (switch to Eevee to reduce load)
bpy.context.scene.render.engine = 'BLENDER_EEVEE_NEXT'
bpy.context.scene.render.resolution_x = 1280 # Lower resolution to reduce load
bpy.context.scene.render.resolution_y = 720
bpy.context.scene.render.fps = 5 # Set to 5 frames per second
bpy.context.scene.frame_end = 50 # Set end frame to 50
- We are deleting any existing mesh objects to start with an empty scene.
- The render engine is set to Eevee using
BLENDER_EEVEE_NEXT
. - The frame rate is set to 5 frames per second, and we set the animation to end at frame 50, making this a 10-second animation.
2. Adding Geometric Shapes
Next, we add a cube, a sphere, and a cone to the scene. Each shape is assigned a unique material and color, so we can easily distinguish them in the animation.
# Create a cube
bpy.ops.mesh.primitive_cube_add(size=1, location=(-2, 0, 0))
cube = bpy.context.object
cube.name = "SpinningCube"
cube.scale = (1, 1, 1)
# Assign material to the cube
cube_material = bpy.data.materials.new(name="CubeMaterial")
cube_material.use_nodes = True
bsdf = cube_material.node_tree.nodes.get('Principled BSDF')
bsdf.inputs['Base Color'].default_value = (1, 0, 0, 1) # Red color
cube.data.materials.append(cube_material)
# Create a sphere
bpy.ops.mesh.primitive_uv_sphere_add(radius=1, location=(2, 0, 0))
sphere = bpy.context.object
sphere.name = "SpinningSphere"
# Assign material to the sphere
sphere_material = bpy.data.materials.new(name="SphereMaterial")
sphere_material.use_nodes = True
bsdf = sphere_material.node_tree.nodes.get('Principled BSDF')
bsdf.inputs['Base Color'].default_value = (0, 1, 0, 1) # Green color
sphere.data.materials.append(sphere_material)
# Create a cone
bpy.ops.mesh.primitive_cone_add(radius1=1, depth=2, location=(0, 0, 2))
cone = bpy.context.object
cone.name = "SpinningCone"
# Assign material to the cone
cone_material = bpy.data.materials.new(name="ConeMaterial")
cone_material.use_nodes = True
bsdf = cone_material.node_tree.nodes.get('Principled BSDF')
bsdf.inputs['Base Color'].default_value = (0, 0, 1, 1) # Blue color
cone.data.materials.append(cone_material
We use:
bpy.ops.mesh.primitive_cube_add
to add the cube at location (-2, 0, 0).bpy.ops.mesh.primitive_uv_sphere_add
for the sphere at (2, 0, 0).bpy.ops.mesh.primitive_cone_add
for the cone, positioned at (0, 0, 2).
For each object, a material is assigned using Blender’s Shader Nodes (we use the default Principled BSDF shader). The objects are assigned colors:
- Red for the cube
- Green for the sphere
- Blue for the cone
3. Camera Setup and Animation
Next, we add a camera and adjust its rotation to tilt down to include both the cube and the sphere in the frame. We then animate each shape, making them rotate around different axes.
# Add a camera and tilt it down to include more of the objects
bpy.ops.object.camera_add(location=(0, -10, 5), rotation=(math.radians(60), 0, 0)) # Tilt camera down (60 degrees)
camera = bpy.context.object
bpy.context.scene.camera = camera
# Add a light
bpy.ops.object.light_add(type='SUN', location=(0, -10, 10))
light = bpy.context.object
# Animation setup: make objects rotate
def create_rotation_animation(obj, axis, frame_start, frame_end, rotations=1):
obj.rotation_euler = (0, 0, 0)
obj.keyframe_insert(data_path="rotation_euler", frame=frame_start)
rotation_value = rotations * 2 * math.pi
if axis == 'X':
obj.rotation_euler[0] = rotation_value
elif axis == 'Y':
obj.rotation_euler[1] = rotation_value
elif axis == 'Z':
obj.rotation_euler[2] = rotation_value
obj.keyframe_insert(data_path="rotation_euler", frame=frame_end)
# Rotate the cube, sphere, and cone around different axes
create_rotation_animation(cube, 'X', frame_start=1, frame_end=50)
create_rotation_animation(sphere, 'Y', frame_start=1, frame_end=50)
create_rotation_animation(cone, 'Z', frame_start=1, frame_end=50)
Here, we:
- Add a camera and position it at (0, -10, 5), with a 60-degree tilt downward to capture both the cube and the sphere in the frame.
- Add a sunlight to illuminate the scene.
We also create a function, create_rotation_animation
, that rotates the objects around different axes:
- The cube rotates around the X-axis.
- The sphere rotates around the Y-axis.
- The cone rotates around the Z-axis.
4. Rendering the Animation
Finally, we set the output file format to MPEG-4 and render the animation. The animation will be saved as an MP4 file on your desktop.
# Set up output path for rendering the animation
bpy.context.scene.render.filepath = '/Users/<YourUsername>/Desktop/spinning_shapes.mp4'
bpy.context.scene.render.image_settings.file_format = 'FFMPEG'
bpy.context.scene.render.ffmpeg.format = 'MPEG4'
bpy.context.scene.render.ffmpeg.codec = 'H264'
# Render animation
bpy.ops.render.render(animation=True)
Replace <YourUsername>
with your actual username on macOS.
Conclusion
With this Python script, you can easily generate an animated sequence in Blender where basic geometric shapes rotate around different axes. By scripting this process, you can automate animation tasks and quickly iterate on complex scenes.
You can further customize this script by adding more shapes, adjusting the lighting, or modifying the camera path to create even more interesting results. Blender’s Python API provides powerful functionality that allows for endless creativity.
Happy animating!
Here is the result of the script: