Sub topic 18
Building and Posing a Rig with Python
Assume you can navigate Blender, switch modes, and understand object transforms. This chapter uses Blender's bpy API, which is Python's interface to Blender data and operations.
Creating an armature with `bpy`
An armature object holds armature data. The data contains the bones and their rest positions. In Edit Mode, you create and edit EditBone objects. In Pose Mode, you change PoseBone objects.
The following script creates a two-bone chain, poses the child bone, and inserts keyframes:
import bpy
import math
# Ensure the script starts in Object Mode.
if bpy.context.object and bpy.context.object.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
# Create armature data and its object.
armature_data = bpy.data.armatures.new("PythonArmature")
armature_object = bpy.data.objects.new("PythonRig", armature_data)
bpy.context.collection.objects.link(armature_object)
# Make the armature active.
bpy.context.view_layer.objects.active = armature_object
armature_object.select_set(True)
# Create the bones in Edit Mode.
bpy.ops.object.mode_set(mode='EDIT')
root = armature_data.edit_bones.new("root")
root.head = (0, 0, 0)
root.tail = (0, 0, 1)
child = armature_data.edit_bones.new("child")
child.head = root.tail
child.tail = (0, 0, 2)
child.parent = root
child.use_connect = True
bpy.ops.object.mode_set(mode='OBJECT')
# Pose the child bone and keyframe the rotation.
pose_bone = armature_object.pose.bones["child"]
pose_bone.rotation_mode = 'XYZ'
bpy.context.scene.frame_set(1)
pose_bone.rotation_euler = (0, 0, 0)
pose_bone.keyframe_insert(data_path="rotation_euler")
bpy.context.scene.frame_set(24)
pose_bone.rotation_euler = (math.radians(45), 0, 0)
pose_bone.keyframe_insert(data_path="rotation_euler")The script uses armature_data.edit_bones while building the rest skeleton. After returning to Object Mode, it accesses armature_object.pose.bones to animate the pose.
| Task | Python collection or property |
|---|---|
| Create rest bones | `armature_data.edit_bones` |
| Set a parent | `child.parent = root` |
| Pose a bone | `armature_object.pose.bones` |
| Animate a property | `keyframe_insert()` |
Understanding the script's structure
A rig-building script usually follows this order:
Keep object references when a script needs to perform several operations. Use bone names when reopening or processing an existing rig. Scripts should also set the active object explicitly because Blender operators depend on the current context.
Rigify add-on overview
Rigify is a Blender add-on that generates an animator-friendly rig from a simpler metarig. Enable it in Edit > Preferences > Add-ons, then add a suitable metarig from Add > Armature > Human (Meta-Rig). Adjust the metarig in Edit Mode, select it, and choose Generate Rig.
Rigify separates the setup skeleton from the generated control rig. The generated rig typically provides:
- Custom controls for selecting and posing limbs.
- IK and FK controls.
- Deformation bones that influence the mesh.
- Organized bone collections and animation controls.
Rigify saves setup time, but its generated names and structure are more complex than a small custom rig. Use a Python-built rig for experiments or specialized systems. Use Rigify when you need a tested humanoid workflow and can accept its conventions.
Exercise
Which collection should a script use to create a new bone, and which collection should it use to rotate an existing pose?
Show answer
Use armature_data.edit_bones to create the rest bone and armature_object.pose.bones to rotate the posed bone.