Sub topic 18
Keyframing and Baking Constrained Animation
This chapter assumes you have built the arm rig from the earlier chapters: a two-bone chain with an IK constraint, a pole target, a hand_ik control bone and an ik_switch driver. It also assumes you can insert keyframes and switch between Pose Mode and Object Mode. The scripts use the rig from the setup example, an armature object named ArmRig.
What Gets Keyframed
A keyframe stores the value of a property at a frame. Bone constraints do not store keyframes. They compute a pose from other bones, so the keys belong on the inputs of the computation.
For the arm rig, the inputs are:
hand_iklocation, rotation and scale.elbow_polelocation.- The custom property
ik_switch. - Constraint settings such as
influence, if you animate them directly.
Do not key upper_arm or forearm while IK controls them. Their rotation values are overwritten by the solver on every evaluation. A key on them has no visible effect, and it becomes a trap when you later turn IK off.
Keying the Control Bones
This script keys the hand control up and back down, and keys the switch so the arm is fully IK for the whole shot.
import bpy
rig = bpy.data.objects["ArmRig"]
bpy.context.view_layer.objects.active = rig
bpy.ops.object.mode_set(mode='POSE')
hand = rig.pose.bones["hand_ik"]
for frame, loc in ((1, (0.0, 0.0, 0.0)),
(24, (0.0, 0.0, 0.8)),
(48, (0.0, 0.0, 0.0))):
hand.location = loc
hand.keyframe_insert(data_path="location", frame=frame)
# Custom properties on a pose bone are keyed with a bracketed data path.
for frame in (1, 48):
hand["ik_switch"] = 1.0
hand.keyframe_insert(data_path='["ik_switch"]', frame=frame)hand.location is relative to the bone's rest position, so (0, 0, 0) means "where the bone was built". The loop assigns each value and then keys it. Setting a value without keying changes the pose for the current frame only. The next frame change discards it.
Why Plain Keying Misses Constraint Results
Keying a bone stores its own channel values: the location, rotation_quaternion and scale of the pose bone. Constraints are applied after those values. The final matrix of a constrained bone can differ from what its channels say.
Visual keying records the final, evaluated result instead. It reads the world-space result of the constraint stack and converts it back into channel values. Blender offers this in several places:
| Method | Where | Records |
|---|---|---|
| Normal keyframe | I key, `keyframe_insert` | Channel values only |
| Visual keying | Preference "Visual Keying", or the Visual Location/Rotation/Scale keying sets | Evaluated pose after constraints |
| Apply Visual Transform | Pose > Apply > Visual Transform | Copies the evaluated pose into the channels, with no key |
| Bake Action | Object > Animation > Bake Action, or `bpy.ops.nla.bake` | Visual keys on every frame in a range |
Visual keying suits one-off captures. Baking suits whole ranges.
What Baking Does
Baking steps through the frame range, evaluates the full rig at each frame (constraints, drivers and IK included), and writes the resulting bone transforms as keyframes on the chosen bones. After baking, the motion no longer depends on the constraints.
Typical reasons to bake:
- Exporting to a game engine or format that does not support IK or constraints.
- Handing the animation to someone with a different rig.
- Retargeting or editing the motion curves directly.
- Freezing the result of drivers so it does not depend on scripts.
Baking with a Script
The next script bakes the two arm bones into a new action and removes the constraints from them.
import bpy
rig = bpy.data.objects["ArmRig"]
bpy.context.view_layer.objects.active = rig
bpy.ops.object.mode_set(mode='POSE')
# Keep the control animation: a fake user stops the old action being purged.
if rig.animation_data and rig.animation_data.action:
rig.animation_data.action.use_fake_user = True
# Select only the bones that should receive baked keys.
bpy.ops.pose.select_all(action='DESELECT')
for name in ("upper_arm", "forearm"):
rig.data.bones[name].select = True
bpy.ops.nla.bake(
frame_start=1,
frame_end=48,
step=1,
only_selected=True,
visual_keying=True,
clear_constraints=True,
use_current_action=False,
bake_types={'POSE'},
)The parameters that matter:
frame_startandframe_endgive the inclusive range. Use the full range you need, because frames outside it receive no keys.stepis the frame interval between keys.1is safest. A larger step gives sparse keys and the motion between them is interpolated, so fast motion can look wrong.only_selectedlimits baking to selected bones. Bones must be visible and not hidden by a collection, or they are skipped.visual_keying=Truerecords the constraint results. With it off, the bake stores unconstrained channel values, which defeats the purpose.clear_constraints=Trueremoves the constraints from the baked bones after baking.use_current_action=Falsewrites into a new action.Trueoverwrites the active action, including your control keys.
Checking and Cleaning the Result
After baking, scrub the timeline and compare the arm against the original motion. Then open the Graph Editor and look for the following.
- Dense keys. Baking writes a key on every frame for every channel. Use Key > Clean Channels or
clean_curves=Truein the bake to remove redundant keys. This changes curves slightly, so compare again. - Euler flips. If the bones use Euler rotation, a rotation can jump by 360 degrees between frames. Use Key > Discontinuity (Euler) Filter on the rotation curves.
- Lost motion on unselected bones. Anything outside
only_selectedwas not baked. Add those bones and bake again. - Custom properties. Properties such as
ik_switchare not deform data. If you export, you can leave them out.
If you need to keep both versions, leave the original rig with its control animation in the file and bake a duplicate. The baked action then goes on a copy whose bones carry no constraints.
Keying the FK/IK Switch
The switch property is itself animated. Key it at 1.0 while the hand is planted in IK, and at 0.0 when you want FK control. A driver-fed influence can blend between the two over several frames. Set the key interpolation to constant if you want an instant switch, because the default Bezier curve passes through values between 0 and 1 and produces a blended pose at the transition.
Baking at the switch point also captures the blend, so the baked FK keys follow the same transition.
Common Errors
| Symptom | Cause | Fix |
|---|---|---|
| Baked arm snaps back to rest | Constraints still active and controls not animated | Clear constraints on bake |
| Bake produces no keys | Bones not selected or hidden | Select visible bones, set `only_selected` |
| Pose in baked action differs from the original | `visual_keying` off | Turn it on |
| Control keys vanished | `use_current_action=True` overwrote them | Bake to a new action |
| Rotation spins between frames | Euler discontinuity | Apply the Euler filter |
Exercise
You baked the arm with clear_constraints=False and use_current_action=False. The new action has correct keys on upper_arm and forearm, but on playback the arm sits in its rest pose. Why does this happen, and what are two ways to fix it?
Show answer
The new action does not contain keys for hand_ik, so that control bone is back at its rest position. The IK constraint is still active and overrides the baked rotations on the arm bones every frame, so the arm reaches for the static hand target. Fix it either by baking again with clear_constraints=True, or by setting the IK constraint influence to 0 (or ik_switch to 0) after the bake so the baked FK keys take effect.