Sub topic 7
Adding an IK Constraint and Choosing Chain Length
This chapter shows how to add an IK constraint to a bone, point it at a target, and choose how many bones the solver controls. It assumes you can create an armature, add bones in Edit Mode, switch between Object, Edit and Pose Mode, and read short Python scripts. It also assumes you know what forward kinematics (FK) and a bone parent chain are.
What the IK constraint does
An IK constraint is a bone constraint that makes a chain of bones reach for a target. You add it to the last bone of the chain, which is the tip. The solver then rotates that bone and some of its parents so the tip's tail (or head, see the Use Tail option in a later chapter) lands on the target.
Three pieces are involved:
- The owner is the bone that carries the constraint. In an arm, this is the forearm.
- The target is the object or bone the chain reaches for. For a rig, this is usually a separate control bone, such as
hand_ik. - The chain is the owner plus the parent bones above it. Its length is set by Chain Length.
The IK constraint does not move the target. It only rotates the bones in the chain.
Prepare the rig
The IK constraint needs a chain and a target. The script below builds a two-bone arm, a hand_ik target bone, and an elbow_pole bone. The pole bone is used in the next chapter, but creating it now keeps the rig the same across chapters.
The upper arm has a slight bend in Y. The solver needs a bend direction to start from. A perfectly straight chain gives it nothing to choose from.
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')
eb = arm_data.edit_bones
upper = eb.new("upper_arm")
upper.head = (0.0, 0.0, 0.0)
upper.tail = (1.0, 0.1, 0.0) # slight bend so the IK solver has a bend direction
fore = eb.new("forearm")
fore.head = (1.0, 0.1, 0.0)
fore.tail = (2.0, 0.0, 0.0)
fore.parent = upper
fore.use_connect = True
hand_ik = eb.new("hand_ik")
hand_ik.head = (2.0, 0.0, 0.0)
hand_ik.tail = (2.0, 0.0, 0.3)
elbow_pole = eb.new("elbow_pole")
elbow_pole.head = (1.0, 2.0, 0.0)
elbow_pole.tail = (1.0, 2.0, 0.3)
bpy.ops.object.mode_set(mode='POSE')Note that hand_ik has no parent. It is not part of the chain, so the solver can never rotate it. If it were a child of forearm, the target would move with the chain and the solver would chase it forever.
Add the constraint in the interface
- In Pose Mode, select the
hand_ikbone, then Shift-click theforearmbone soforearmis active. - Open the Bone Constraints tab in the Properties editor.
- Choose Add Bone Constraint, then Inverse Kinematics.
- Set Target to the armature object, then set Bone to
hand_ik. - Set Chain Length to 2.
The forearm snaps toward hand_ik. Move hand_ik in Pose Mode and the arm follows.
Add the constraint with Python
The same setup in a script uses constraints.new('IK') on the pose bone. The target field takes the armature object, and subtarget takes the bone name.
import math
import bpy
rig = bpy.data.objects["ArmRig"]
forearm = rig.pose.bones["forearm"]
ik = forearm.constraints.new('IK')
ik.target = rig
ik.subtarget = "hand_ik"
ik.pole_target = rig
ik.pole_subtarget = "elbow_pole"
ik.pole_angle = math.radians(-90.0) # adjust until the elbow bends toward the pole
ik.chain_count = 2 # forearm and upper_arm; 0 would use the whole chainThis script also sets the pole target, which the next chapter explains. With only a target and a chain length, the arm already reaches for hand_ik, but the elbow bend direction is left to the solver.
How chain length works
chain_count in Python, or Chain Length in the interface, sets how many bones the solver may rotate, counting from the owner upward through its parents.
| Chain length | Bones the solver controls | Typical use |
|---|---|---|
| 1 | Owner only | A single bone that should aim at a target |
| 2 | Owner and its parent | Arm or leg with upper and lower segments |
| 3 | Owner and two parents | Leg with a foot, or an arm with a shoulder bone |
| 0 | Every parent up to the root | Tails, tentacles, spines (use with care) |
Bones above the chain are not changed by the solver. They stay under FK control, so you can still pose them by hand. A shoulder bone above a two-bone arm is a common example. The solver leaves it alone, and moving it carries the whole IK arm along.
Choosing a chain length
Pick the length from what you want the solver to own.
- Two-bone limbs. Use 2. The upper and lower bone form a triangle with the target, and the elbow or knee is the only joint that bends between them.
- Limbs with a foot or hand bone. Put the constraint on the lower leg bone with length 2, and control the foot with a separate Copy Rotation constraint. A length of 3 is possible, but the solver then rotates the foot to reach the target too, which usually makes the foot slide off the floor.
- Long chains. For a tail or tentacle, a length of 5 to 10 gives smooth curves. Use 0 only when you really want every ancestor to move.
A length of 0 includes every bone up to the root, even bones you meant to keep fixed, such as the hips or the whole body root. If the entire character starts to bend or twist when you move the target, check for a length of 0.
Limits of the reach
An IK chain has a maximum reach equal to the sum of its bone lengths. In the rig above that is about 2 units. If the target moves beyond that distance, the chain straightens and stops short of it. The bones do not stretch unless you turn on the Stretch option (covered in a later chapter).
A target that sits very close to the chain's start can also cause problems. The chain folds on itself and the solver's choice of bend direction may jump. That is one source of the flipping covered in the troubleshooting chapter.
Common mistakes
- Constraint on the wrong bone. The constraint goes on the tip bone, not the root. If you add it to
upper_armwith length 1, the upper arm aims at the target and the forearm stays out of the solve. - Target inside the chain. If the target is a child of the owner or of any bone in the chain, a dependency cycle results. Blender shows the constraint in red and the chain stops responding.
- Disconnected bones. A bone with Connected turned off still joins the chain through its parent, but the gap between the parent tail and child head acts like a fixed offset. The reach is no longer the sum of the bone lengths, which can look like the arm refusing to straighten.
- Pose Mode and Edit Mode confusion. The constraint lives on the pose bone. Adding bones in Edit Mode after setting the constraint does not change it, but moving a bone's head or tail in Edit Mode changes the rest pose the solver works from.
Checking your work
Move hand_ik in Pose Mode and watch both bones. They should both rotate, with the elbow as the only joint that visibly bends. Then change the chain length to 1 and move the target again. Only the forearm should rotate, and the upper arm should stay still. If it does, the chain length is working as described.
Exercise
You add an IK constraint to forearm with Chain Length 2, and the target is a bone named hand_ik. When you drag hand_ik far away, the arm straightens and the hand stops short of it. A teammate says the chain length is too small. Is that right, and what controls this behavior?
Show answer
No. Chain Length only sets which bones the solver rotates. The arm stops short because its reach is the sum of the two bone lengths, and without the Stretch option the bones keep their length. A longer chain length would only bring more parent bones into the solve, not extend the reach of these two.
Exercise
A character's whole body leans when you move the hand target, including the hips. The constraint is on the hand-side bone of the arm. What is the most likely cause, and how do you fix it?
Show answer
Chain Length is probably 0, which includes every parent bone up to the root. Set Chain Length to 2 (or the number of bones in the arm) so the solver stops before the shoulder and body.