Sub topic 2
Adding an IK Constraint and Choosing Chain Length
This chapter assumes you can create an armature, add bones in Edit Mode, switch modes, and run short Python scripts in Blender's Scripting workspace. You should also know what a bone parent is.
Adding an IK Constraint
An IK constraint on a bone makes a chain of bones reach toward a target bone. The constrained bone is the chain's tip, and the solver rotates the bones above it until the tip reaches the target.
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.chain_count = 2Choosing Chain Length
chain_count sets how many bones the solver may rotate, counting up from the tip. A value of 2 rotates the forearm and upper arm. A value of 0 lets the solver use every parent bone, which can move the whole body of a rig and is rarely what you want for a limb.
| chain_count | Bones moved by the solver |
|---|---|
| 1 | Only the constrained bone |
| 2 | Constrained bone and its parent |
| 0 | Every parent up to the root |
Controlling Bend Direction with Pole Targets
A two-bone chain has two valid bend directions, so the solver needs a hint. A pole target is a second bone that the elbow or knee points toward. pole_angle then rotates the chain around the line from root to target. Start at -90 degrees and adjust until the joint bends the right way.
import math
ik.pole_target = rig
ik.pole_subtarget = "elbow_pole"
ik.pole_angle = math.radians(-90.0)IK Options: Stretch, Use Tail, Iterations, and Weights
- Use Tail: measures the chain from the bone tail. Turn it off when the target is a bone's head.
- Stretch: lets bones lengthen to reach the target. Keep it off for rigid limbs.
- Iterations: the number of solver passes. The default is usually enough, but long chains may need more.
- Weight: the influence of the position goal. Lower values let other constraints compete.
IK Limits and Locks on Individual Bones
Limits and locks are set on the pose bone, not on the constraint. Locks stop the solver from rotating a bone on an axis. Limits set a range for that axis.
import math
forearm.lock_ik_y = True
forearm.lock_ik_z = True
forearm.use_ik_limit_x = True
forearm.ik_min_x = 0.0
forearm.ik_max_x = math.radians(150.0)This makes the elbow bend only on its local X axis, between 0 and 150 degrees.
Limit Rotation, Location, and Distance Constraints
These constraints clamp a bone's final pose, so they also work with FK and other constraints.
- Limit Rotation: sets min and max angles per axis. Set
owner_spaceto LOCAL for joint-style limits. - Limit Location: clamps position on each axis.
- Limit Distance: keeps a bone within a set radius of a target.
Exercise
You set chain_count to 0 on a forearm, and the whole rig moves when the hand target moves. Which value should you set, and why?
Show answer
Set chain_count to 2. A value of 0 lets the solver rotate every parent bone, so a value of 2 limits the solver to the forearm and upper arm.