Skip to content

Turn any video into a Mixamo-rig animation - GVHMR estimator, spec-driven retarget, FK apply in Blender via MCP. Works with any Mixamo character; built to be operated end-to-end by an AI agent.

head_look.level_face: closed-loop gaze levelling

Sachaa7887d46d7d2
diff --git a/action_specs/spin.json b/action_specs/spin.json
index a88f017..5ce787c 100644
--- a/action_specs/spin.json
+++ b/action_specs/spin.json
@@ -43,8 +43,8 @@
],
"head_look": [
- { "src": [172, 204], "ramp_src": 6, "amount": 0.7, "pitch_deg": -12,
- "comment": "owner pass-3: face direction off vs reference. Pass-4: skull also leaned back 8-14 deg (reference stays level, -6..+3) - pitch it forward 12 deg so he looks straight at the camera." }
+ { "src": [172, 204], "ramp_src": 6, "amount": 0.7, "level_face": 1.0, "level_target_deg": 0,
+ "comment": "owner pass-3/4/5: he kept looking at the sky. Measured face axis (head local +Z) was +19..+27 deg above horizon; a fixed pitch_deg guess could not track the varying torso lean, so the apply now levels the face in closed loop." }
],
"qa_src_frames": [1, 40, 55, 71, 85, 97, 117, 130, 146, 157, 170, 184, 192, 220, 241]
diff --git a/docs/PIPELINE.md b/docs/PIPELINE.md
index 2ec2b53..0cc5e11 100644
--- a/docs/PIPELINE.md
+++ b/docs/PIPELINE.md
@@ -141,10 +141,16 @@ Two more windowed correctors for systematic estimator bias in a phase
{ "src": [172, 204], "ramp_src": 5, "side": "right", "offset": [0, -0.05, 0] }
],
-// Blend the head's horizontal gaze toward character-forward
-// (amount 0..1) and/or pitch the skull axis (pitch_deg: + looks up,
-// − levels a backward-leaning head).
-"head_look": [ { "src": [172, 204], "ramp_src": 6, "amount": 0.7, "pitch_deg": -12 } ],
+// Gaze control. `amount` blends the horizontal heading toward
+// character-forward. `level_face` (0..1) puts the FACE at
+// `level_target_deg` above the horizon — a closed loop measured on the
+// rig, which is what you want for "he's looking at the sky": the FK
+// apply only aims the skull axis, so face pitch is a by-product of the
+// torso lean and a hand-guessed `pitch_deg` cannot track it.
+"head_look": [
+ { "src": [172, 204], "ramp_src": 6, "amount": 0.7,
+ "level_face": 1.0, "level_target_deg": 0 }
+],
// Restore strike extension. Scales the hand's distance from its
// shoulder socket along its own direction, then re-places the chain
diff --git a/docs/PITFALLS.md b/docs/PITFALLS.md
index 6f2c34a..a90ef11 100644
--- a/docs/PITFALLS.md
+++ b/docs/PITFALLS.md
@@ -81,12 +81,14 @@ touching a clip a human has partially signed off.
exactly this reason; anything else that renders the scene must do
the same.
-17. **The head aim ignores the lifted head's vertical.** `apply_mixamo_fk`
- rebuilds the head/neck aim from the torso up-axis plus the head's
- HORIZONTAL offset, so the skull follows the torso lean and a gaze
- correction written into the head joint silently disappears. Vertical
- gaze must travel as the explicit per-frame `head_pitch_deg` field
- (spec `head_look.pitch_deg`), which the apply rotates the aim by.
+17. **Gaze is the FACE axis (head local +Z), not the skull axis.** The
+ FK apply only aims the skull axis (+Y), so face pitch is whatever
+ the torso lean leaves — a head can read "vertical" while looking
+ 20–27° at the sky, and a metric built on +Y will confirm the wrong
+ thing. Measure `(matrix_world @ pb.matrix).to_3x3() @ (0,0,1)` and
+ take `asin(z)`. Fix with `head_look.level_face` (closed loop on the
+ rig); hand-guessed pitch angles cannot track a varying lean, and a
+ gaze correction written into the head *joint* never reaches the bone.
18. **Smoothing eats strikes.** A `smooth` window wide enough to calm a
fast flurry (9 frames) also averages away its extension peaks —
punches visibly shrink. Keep flurry windows narrow (5) and restore
diff --git a/pipeline/apply_mixamo_fk.py b/pipeline/apply_mixamo_fk.py
index cea5398..5138271 100644
--- a/pipeline/apply_mixamo_fk.py
+++ b/pipeline/apply_mixamo_fk.py
@@ -255,6 +255,67 @@ def apply_fingers(arm, amount: float) -> None:
pb.location = Vector((0.0, 0.0, 0.0))
+def _face_elev(arm, pb) -> float:
+ """Elevation of a head bone's FACE axis above the horizon, radians."""
+ bpy.context.view_layer.update()
+ face = ((arm.matrix_world @ pb.matrix).to_3x3() @ Vector((0.0, 0.0, 1.0))).normalized()
+ return math.asin(max(-1.0, min(1.0, face.z)))
+
+
+def _pitch_head(arm, pb, delta: float) -> None:
+ """Rotate a head bone by `delta` about the world-horizontal axis
+ perpendicular to its face direction."""
+ world3 = (arm.matrix_world @ pb.matrix).to_3x3()
+ lateral = (world3 @ Vector((1.0, 0.0, 0.0)))
+ lateral.z = 0.0
+ if lateral.length < 1e-5:
+ return
+ axis_arm = dir_to_arm(arm, lateral.normalized())
+ head = pb.matrix.to_translation()
+ mat = Quaternion(axis_arm, delta).to_matrix().to_4x4() @ pb.matrix.to_3x3().to_4x4()
+ mat.translation = head
+ pb.matrix = mat
+ bpy.context.view_layer.update()
+ q = pb.rotation_quaternion.copy()
+ pb.location = Vector((0.0, 0.0, 0.0))
+ pb.rotation_quaternion = q
+ bpy.context.view_layer.update()
+
+
+def level_face(arm, name: str, amount: float, target_deg: float = 0.0) -> None:
+ """Put a head bone's FACE axis at `target_deg` above the horizon,
+ by `amount` (0..1). Self-calibrating.
+
+ The face direction on a mixamorig head is the bone's local +Z (at
+ rest, world -Y = character forward); +Y runs up the skull. The FK
+ apply only aims +Y, so the face ends up pitched by whatever the
+ torso is doing — a head can look at the sky while the skull axis
+ reads vertical. Rather than assume a rotation sign in the head's
+ (heavily yawed) frame, this probes the response with a small test
+ rotation, solves for the exact correction, and refines once.
+ """
+ if amount <= 1e-4:
+ return
+ pb = arm.pose.bones[name]
+ target = math.radians(target_deg)
+ e0 = _face_elev(arm, pb)
+ want = (target - e0) * amount
+ if abs(want) < 1e-4:
+ return
+ probe = math.copysign(0.05, want)
+ _pitch_head(arm, pb, probe)
+ e1 = _face_elev(arm, pb)
+ slope = (e1 - e0) / probe
+ if abs(slope) < 0.1: # unresponsive axis: undo and bail
+ _pitch_head(arm, pb, -probe)
+ return
+ _pitch_head(arm, pb, ((target - e1) * amount) / slope)
+ e2 = _face_elev(arm, pb) # one refinement pass
+ resid = (target - e2) * amount
+ if abs(resid) > math.radians(0.5):
+ _pitch_head(arm, pb, resid / slope)
+
+
def ensure_action(arm, name: str, end: int):
if arm.animation_data is None:
arm.animation_data_create()
@@ -395,6 +456,8 @@ def run(spec_path: str) -> dict:
aim_bone(arm, bone, neck_p + aim)
else:
aim_bone(arm, bone, v(fr[key]))
+ level_face(arm, "mixamorig:Head", float(fr.get("head_level", 0.0) or 0.0),
+ float(fr.get("head_level_target_deg", 0.0) or 0.0))
apply_fingers(arm, fist)
if rest >= 0.65:
diff --git a/pipeline/lift_to_mixamo.py b/pipeline/lift_to_mixamo.py
index 0de01bc..6c0bd1a 100644
--- a/pipeline/lift_to_mixamo.py
+++ b/pipeline/lift_to_mixamo.py
@@ -463,6 +463,8 @@ def main():
rec[f"{s}_hand"] = new_wrist + unit(new_wrist - elbow) * LEN[f"{s}_hand"]
head_pitch_deg = 0.0
+ head_level = 0.0
+ head_level_target = 0.0
# Windowed gaze correction (spec "head_look"): blend the head's
# horizontal direction toward character-forward, and/or pitch the
# skull axis (`pitch_deg`, + = look up, − = look down). For phases
@@ -498,6 +500,11 @@ def main():
ang = np.radians(float(hl["pitch_deg"])) * win
v = rodrigues(v, np.asarray(rec["basis_x"], float), ang)
head_pitch_deg += float(hl["pitch_deg"]) * win
+ if hl.get("level_face"):
+ # Closed-loop: the apply measures the resulting face
+ # elevation and levels it to `level_target_deg`.
+ head_level = max(head_level, float(hl["level_face"]) * win)
+ head_level_target = float(hl.get("level_target_deg", 0.0))
rec["head"] = neck + v
rest_amt = smoother((sf - rb["start_src"]) / max(1.0, rb["full_src"] - rb["start_src"]))
@@ -517,7 +524,9 @@ def main():
extras.append({"frame": f, "t": float(dst_times[i]), "rest": rest_amt, "fist": fist,
"plant": plant_at(sf) if rest_amt < 0.65 else "both",
"pelvis_height": float(pelvis_h[i]),
- "head_pitch_deg": head_pitch_deg})
+ "head_pitch_deg": head_pitch_deg,
+ "head_level": head_level,
+ "head_level_target_deg": head_level_target})
# Optional extra temporal smoothing on selected joints in selected
# windows (spec "smooth") — for fast flurries the raw estimator
@@ -575,6 +584,8 @@ def main():
out["plant"] = ex["plant"]
out["pelvis_height"] = round(float(ex["pelvis_height"]), 5)
out["head_pitch_deg"] = round(float(ex["head_pitch_deg"]), 3)
+ out["head_level"] = round(float(ex["head_level"]), 4)
+ out["head_level_target_deg"] = round(float(ex["head_level_target_deg"]), 2)
joints.append(out)
payload = {