Create a Shape Key Motion Graphic with the Blender Python API
- Introduction
- Import Dependencies
- Define Helper Functions
- Set up Scene
- Create and Position Camera
- Create Material With Emission Shader
- Create a Plane With the Material
- Cut Out Center From Plane
- Add Shape Keys
- Add Keyframes
- Conclusion
Introduction
I decided to recreate this short tutorial from YouTube to practice using the Blender Python API. This post goes through the code I came up with to replicate the tutorial plus some small additions.
Import Dependencies
The only dependencies strictly required for this tutorial are bpy
and bmesh
. The bpy
package is the base API for Blender and the bmesh
module provides access to Blender’s internal mesh editing API. I also used the math
module from the Python Standard Library for one of my helper functions.
# The Blender Python API
import bpy
# Gives access to Blender's internal mesh editing API
import bmesh
# Provides access to mathematical functions
import math
Define Helper Functions
I made some wrapper functions for the standard location, rotation, and scale transformations as well as getting the name of the active object.
You can get the name of the active object with bpy.context.active_object.name
.
The three standard transformations can be accessed for individual objects with the following:
bpy.data.objects["object_name"].location
bpy.data.objects["object_name"].rotation_euler
bpy.data.objects["object_name"].scale
I also made a function to empty the default collection so that nothing gets duplicated. Collections can be accessed with bpy.data.collections["collection_name"]
or bpy.data.collections[index]
.
Lastly, I made a function to easily add sequences of keyframes to a given object. The function uses the built-in setattr()
method to set the desired value for the target object and uses the object.keyframe_insert()
method to add the keyframe.
def get_name():
"""Get the name for the currently active object"""
return bpy.context.active_object.name
def degToRadian(angle):
"""Convert angle from degrees to radians"""
return angle*(math.pi/180)
def move_obj(name, coords):
"""Set object location to the specified coordinates"""
= coords
bpy.data.objects[name].location
def rotate_obj(name, angles):
"""Set object rotation to the specified angles"""
= [degToRadian(angle) for angle in angles]
rotation = rotation
bpy.data.objects[name].rotation_euler
def scale_obj(name, scale):
"""Set object scale"""
= scale
bpy.data.objects[name].scale
def clear_collection(collection):
"""Remove everything from the specified collection"""
for obj in collection.objects:
bpy.data.objects.remove(obj)
def add_keyframe_sequence(obj, attribute, values, frames):
"""Add a sequence of keyframes for an object"""
for v, f in zip(values, frames):
setattr(obj, attribute, v)
=attribute, frame=f) obj.keyframe_insert(data_path
Set up Scene
The first thing I do is set the Color Management property, View Transform, from the default value of Filmic
to Standard
. This setting can be accessed at bpy.data.scenes["Scene"].view_settings.view_transform
.
Next, I set the background to the desired color. In my case, it’s pure black. The background color is stored in bpy.data.worlds['World'].node_tree.nodes["Background"].inputs[0].default_value
.
The last setup step is to clear any objects added from the last time the script was run with the clear_collection()
function.
"""Set up the scene"""
# Set View Transform to Standard
"Scene"].view_settings.view_transform = "Standard"
bpy.data.scenes[# Set the Background color to pure black
'World'].node_tree.nodes["Background"].inputs[0].default_value = (0, 0, 0, 1)
bpy.data.worlds[# Clear Collection
0]) clear_collection(bpy.data.collections[
Create and Position Camera
Cameras can be added using the bpy.ops.object.camera_add()
method. I then positioned the camera using the wrapper functions I defined earlier.
"""Create and position a new camera"""
# Create a new camera
object.camera_add()
bpy.ops.# Get the name of the current object, the camera
= get_name()
name # Move the camera
0, -8, 0])
move_obj(name, [# Rotate the camera
90, 0, 0])
rotate_obj(name, [# Set camera to orthographic
type = "ORTHO" bpy.context.active_object.data.
Create Material With Emission Shader
I decided to add some color to the motion graphic so I needed to create a new material. It is recommended to check if the material exists before trying to create it. This can be done in one line as shown below.
material = bpy.data.materials.get(material_name) or bpy.data.materials.new(material_name)
Since there’s is no light, I’ll add an Emission
shader. This requires enabling nodes for the material with material.use_nodes = True
.
Next, I remove the default Principled_BSDF
node as well as any Emission
nodes from earlier runs. Nodes can be removed using the material.node_tree.nodes.remove()
method.
The Emission
node needs to be linked to the first slot in the Material Output
node. Nodes are linked using the material.node_tree.links.new()
method.
"""Create a material with an Emission Shader"""
# Create a material named "Material" if it does not exist
= "Material"
mat_name = bpy.data.materials.get(mat_name) or bpy.data.materials.new(mat_name)
mat
# Enable nodes for the material
= True
mat.use_nodes # Get a reference to the material's node tree
= mat.node_tree.nodes
nodes
# Remove the 'Principled BSDF' node if there is one
if (nodes.get('Principled BSDF') is not None):
'Principled BSDF'))
nodes.remove(nodes.get(
# Remove the 'Emission' node if there is one
if (nodes.get('Emission') is not None):
'Emission'))
nodes.remove(nodes.get(
# Get a reference to the material's output node
= nodes.get('Material Output')
mat_output # Create a new Emission shader
= nodes.new('ShaderNodeEmission')
emission # Link the Emission shader to the Surface value of the output node
0], emission.outputs[0]) mat.node_tree.links.new(mat_output.inputs[
Create a Plane With the Material
The object in the above motion graphic is a plain. Plains can be added using the bpy.ops.mesh.primitive_plane_add()
method.
I then assign the previously created material to the plane. Materials can be added to an object with object.data.materials.append(material)
.
"""Create a new plane with the Emission material"""
# Create a new plane
bpy.ops.mesh.primitive_plane_add()# Get the name of the new plane
= get_name()
name # Rotate the plane
90, 0, 0])
rotate_obj(name, [# Reduce the size of the plance by half
= 0.5
plane_scale *3)
scale_obj(name, [plane_scale]
# Get a reference to the plane
= bpy.context.active_object
plane # Attach the material with the Emission shader to the plane
if plane.data.materials:
0] = mat
plane.data.materials[else:
plane.data.materials.append(mat)
Cut Out Center From Plane
The next step is to make a square hole in the plane like in the above Gif. This requires modifying the mesh for the plane.
Mesh data for the currently selected object is stored at bpy.context.object.data
.
To edit the mesh, we need to get a BMesh representation. We first create an empty BMesh with bm = bmesh.new()
and then fill it with the mesh using bm.from_mesh(mesh)
.
We can make the square by adding a new inset to the plane using the bmesh.ops.inset_individual()
method. Then, we delete the new face that gets created with bmesh.ops.delete()
.
The mesh then needs to be updated with these alterations using bm.to_mesh(mesh)
. Finally, we need to free the BMesh representation we created with bm.free()
.
"""Cut out a center square from the plane"""
# Get the mesh for the plane object
= bpy.context.object.data
mesh
# Get a BMesh representation of the plane mesh
= bmesh.new()
bm
bm.from_mesh(mesh)
# Create a list of the plane faces
= [f for f in bm.faces]
faces_copy # Create a new inset for the selected face
= [faces_copy[0]], thickness=0.3, depth=0.0)
bmesh.ops.inset_individual(bm, faces
# Get a list of faces
= [f for f in bm.faces]
faces_select # Delete the middle face
=[faces_select[0]], context='FACES_ONLY')
bmesh.ops.delete(bm, geom# Update the mesh
bm.to_mesh(mesh)# Free the Bmesh representation and prevent further access
bm.free()
Add Shape Keys
We can add shape keys with the bpy.ops.object.shape_key_add()
method. To deform the plane, we need to access its vertices. We can do this in edit mode with the bmesh
module.
We first enter edit mode for the plane with bpy.ops.object.mode_set(mode="EDIT")
. We can then create a new BMesh representation for the current mesh in edit mode using bm = bmesh.from_edit_mesh(mesh)
.
The vertices are stored in bm.verts
, but we need to create our own list since we can’t index it directly.
Unlike the tutorial video, I just set the positions for the inner vertices directly. It took some trial and error to determine the correct indices for the inner vertices.
After freeing the BMesh representation, we can enter object mode with bpy.ops.object.mode_set(mode="OBJECT")
.
First Shape Key
"""Add first shape key to deform the plane"""
# Add a Basis shape key
object.shape_key_add()
bpy.ops.# Add a new shape key
object.shape_key_add()
bpy.ops.
# Enter edit mode
object.mode_set(mode="EDIT")
bpy.ops.# Create a BMesh representation from the current mesh in edit mode
= bmesh.from_edit_mesh(mesh)
bm
# Create a list of the vertices
= [v for v in bm.verts]
vertices
# Set the location for the inner four corners to the same as the outer corners
4].co.x = vertices[0].co.x
vertices[4].co.y = vertices[0].co.y
vertices[
5].co.x = vertices[1].co.x
vertices[5].co.y = vertices[1].co.y
vertices[
7].co.x = vertices[2].co.x
vertices[7].co.y = vertices[2].co.y
vertices[
6].co.x = vertices[3].co.x
vertices[6].co.y = vertices[3].co.y
vertices[
# Update the mesh
True)
bmesh.update_edit_mesh(mesh, # Free the BMesh representation and prevent further access
bm.free()
# Enter object mode
object.mode_set(mode="OBJECT") bpy.ops.
Second Shape Key
The process for the second shape key is identical except it only moves two of the inner vertices.
"""Add second shape key to deform the plane"""
# Add a new shape key
object.shape_key_add()
bpy.ops.# Enter edit mode
object.mode_set(mode="EDIT")
bpy.ops.# Create a BMesh representation from the current mesh in edit mode
= bmesh.from_edit_mesh(mesh)
bm
# Create a list of vertices
= [v for v in bm.verts]
vertices
# Move the bottom inner left corner to the bottom outer left corner
4].co.x = vertices[0].co.x
vertices[4].co.y = vertices[0].co.y
vertices[# Move the top inner right corner to the top right outer corner
6].co.x = vertices[3].co.x
vertices[6].co.y = vertices[3].co.y
vertices[
# Update the mesh
True)
bmesh.update_edit_mesh(mesh, # Free the BMesh representation and prevent further access
bm.free()
# Enter object mode
object.mode_set(mode="OBJECT") bpy.ops.
Add Keyframes
Before adding the keyframes, I set the render frame rate as well the start and end frames for the scene. The frame rate is stored at bpy.context.scene.render.fps
.
The start and end frames are stored in bpy.data.scenes['Scene'].frame_start
and bpy.data.scenes['Scene'].frame_end
respectively.
"""Set up for animation"""
# Set the render frame rate to 60
= 60
bpy.context.scene.render.fps
# Set the start frame to 0
'Scene'].frame_start = 0
bpy.data.scenes[# Set the end frame to 200
'Scene'].frame_end = 175
bpy.data.scenes[# Set the current frame to 0
'Scene'].frame_current = 0 bpy.data.scenes[
Shape Keys
The shape keys for the plane are stored in bpy.context.selected_objects[0].data.shape_keys
. Individual shape keys can be accessed with bpy.context.selected_objects[0].data.shape_keys.key_blocks[index]
.
First Shape Key
"""Add keyframes to the first shape key"""
# Get a reference to the list of shape keys
= bpy.context.selected_objects[0].data.shape_keys
shape_keys
# Get a reference to the first shape key
= shape_keys.key_blocks[1]
zoomy # Set values for keyframes
= [1.0, 0.2, 0.0, 0.0, 0.75, 1.0]
values # Set the frames for keyframes
= [0, 10, 40, 135, 145, 170]
frames # Add keyframes for the value of the first shape key
'value', values, frames) add_keyframe_sequence(zoomy,
Second Shape Key
"""Add keyframes to animate the second shape key"""
# Get a reference to the second shape key
= shape_keys.key_blocks[2]
zoomy_2 # Set values for keyframes
= [0.0, 0.265, 0.95, 0.0]
values # Set the frames for keyframes
= [100, 110, 132, 142]
frames # Add keyframes for the value of the second shape key
'value', values, frames) add_keyframe_sequence(zoomy_2,
Plane Rotation
"""Add keyframes to rotato the plane"""
# Get a reference to the planey
= bpy.context.selected_objects[0]
plane # Set values for keyframes
= [[degToRadian(angle) for angle in [90, 0, 0]],
values for angle in [90, 85, 0]],
[degToRadian(angle) for angle in [90, 90, 0]]]
[degToRadian(angle) # Set the frames for keyframes
= [0, 10, 50]
frames # Add keyframes
'rotation_euler', values, frames) add_keyframe_sequence(plane,
Material Color
The color for the Emision shader can be accessed at material.node_tree.nodes["Emission"].inputs["Color"].default_value
.
"""Add keyframes to animate the material color"""
# Get a reference to the Emission shader
= mat.node_tree.nodes["Emission"]
mat_node # Set values for keyframes
= [(0, 0.5, 1, 1), (0.96, 0.42, 0, 1), (0.96, 0.42, 0, 1), (0, 0.5, 1, 1)]
values # Set the frames for keyframes
= [100, 125, 132, 142]
frames # Add keyframes for the color of the Emission shader
'Color'], 'default_value', values, frames) add_keyframe_sequence(mat_node.inputs[
Conclusion
I feel like this exercise was worthwhile as it forced me to learn about multiple parts of the API. Although, it took quite a bit longer than the nine minute length of the tutorial video to track down all the required parts of the API. Finding out how to properly add the Emission shader was particularly time consuming. I did not realize that the name used to create the Emission shader was different than the name used to reference it. Fortunately, Blender has been around for a while and someone on the internet had already asked how to do most of the individual steps.
Tutorial Resources: GitHub Repository
I’m Christian Mills, a deep learning consultant specializing in practical AI implementations. I help clients leverage cutting-edge AI technologies to solve real-world problems.
Interested in working together? Fill out my Quick AI Project Assessment form or learn more about me.