Sub topic 7
Adding an IK Constraint and Choosing Chain Length
Assume you can create an armature, edit bones, and switch to Pose Mode. This chapter uses Python, so you should also know how to run a script in Blender's scripting workspace.
What the IK Constraint Does
Inverse kinematics (IK) solves a bone chain from a target. Instead of rotating the upper arm and forearm separately, you move a target bone near the hand. Blender calculates rotations for the chain so its end reaches that target.
The IK constraint belongs on the last deform bone in the chain. For an arm, that is usually the forearm. The constraint then works upward through that bone's parents.
The target can be a separate object or a bone in the same armature. When the target is a bone, set the constraint's target to the armature object and set subtarget to the target bone's name.
Chain Length Controls
chain_count determines how many bones the solver may rotate. The constrained bone counts as the first bone.
| Chain length | Bones affected in an arm | Typical use |
|---|---|---|
| `1` | Forearm only | A local correction |
| `2` | Forearm and upper arm | A normal arm IK setup |
| `0` | All available parents | A full-chain solve |
A chain length of 2 is usually the correct starting point for an arm or leg. A value of 0 can unexpectedly rotate a shoulder, spine, or root bone if those bones are ancestors of the limb.
Add the Constraint with Python
This script builds a small armature and adds an IK constraint to its forearm. The slight bend in the rest pose gives the solver an initial bend direction. A later chapter adds a pole target for explicit control.
import bpy
arm_data = bpy.data.armatures.new("ArmRig")
rig = bpy.data.objects.new("ArmRig", arm_data)
bpy.context.collection.objects.link(rig)
bpy.context.view_layer.objects.active = rig
rig.select_set(True)
bpy.ops.object.mode_set(mode="EDIT")
bones = arm_data.edit_bones
upper = bones.new("upper_arm")
upper.head = (0.0, 0.0, 0.0)
upper.tail = (1.0, 0.2, 0.0)
fore = bones.new("forearm")
fore.head = upper.tail
fore.tail = (2.0, 0.0, 0.0)
fore.parent = upper
fore.use_connect = True
hand_ik = bones.new("hand_ik")
hand_ik.head = (2.0, 0.0, 0.0)
hand_ik.tail = (2.0, 0.0, 0.3)
bpy.ops.object.mode_set(mode="POSE")
forearm = rig.pose.bones["forearm"]
ik = forearm.constraints.new("IK")
ik.name = "Hand IK"
ik.target = rig
ik.subtarget = "hand_ik"
ik.chain_count = 2Move hand_ik in Pose Mode to test the result. Blender rotates both forearm and upper_arm, while the target bone itself remains a control.
Exercise
Exercise
An IK constraint is on the forearm, and the shoulder rotates unexpectedly when the hand target moves. What setting should you check first?
Show answer
Check chain_count. Set it to 2 so the solver affects only the forearm and upper arm instead of all available parent bones.