Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
video2motion: author clips in the canonical rig convention (fix the fold)
nirholas27de4e15401b
diff --git a/workers/model-video2motion/canonical_rest.py b/workers/model-video2motion/canonical_rest.pynew file mode 100644index 000000000..8fd20fcf9--- /dev/null+++ b/workers/model-video2motion/canonical_rest.py@@ -0,0 +1,118 @@+"""Canonical rig bind pose (Mixamo/Wolf3D convention), mirrored from+src/animation-canonical-rest.js. Auto-exported — keep in sync with the JS.++REST_LOCAL[bone] = the bone LOCAL bind quaternion [x,y,z,w] (Rs).+REST_WORLD[bone] = the bone WORLD (model-frame) bind quaternion (WS).+The video->clip solver authors keyframes in THIS convention so the browser+retarget (src/animation-retarget.js) round-trips instead of mangling them.+"""++REST_LOCAL = {+ "Head": [-0.117068819, 9e-9, 3.7e-8, 0.993123805],+ "Hips": [0.001074115, 6.5e-8, 0, 0.999999423],+ "LeftArm": [-0.003089573, 0.080181485, -0.02504183, 0.996460883],+ "LeftFoot": [0.552373703, 0.000251888, 0.003653811, 0.833588554],+ "LeftForeArm": [-2.68e-7, 1.28e-7, 0.01236938, 0.999923496],+ "LeftHand": [-0.001291012, 0.002537816, 0.010116449, 0.999944774],+ "LeftHandIndex1": [0.02880329, 0.011362422, 0.055965724, 0.997952456],+ "LeftHandIndex2": [-0.005195238, 0.014315447, 0.000215817, 0.999884009],+ "LeftHandIndex3": [-0.001315102, -0.016941197, -0.006458334, 0.999834764],+ "LeftHandMiddle1": [0.027532617, -0.031607746, 0.016931179, 0.998977598],+ "LeftHandMiddle2": [-0.001267982, 0.001046862, -0.004466176, 0.999988675],+ "LeftHandMiddle3": [-0.001580353, 0.001835289, 0.003804071, 0.999989832],+ "LeftHandPinky1": [0.009345047, -0.07377448, -0.042077842, 0.996343039],+ "LeftHandPinky2": [-0.001900218, 0.000277086, 0.000496819, 0.999998033],+ "LeftHandPinky3": [-0.001415752, -0.005595319, -0.004606741, 0.999972733],+ "LeftHandRing1": [0.018207541, -0.034360087, -0.036855349, 0.998563745],+ "LeftHandRing2": [-0.000657129, 0.015477225, 0.01003472, 0.99982965],+ "LeftHandRing3": [0.001815607, -0.003099596, 0.007204617, 0.999967594],+ "LeftHandThumb1": [0.19002276, -0.058826117, 0.347878363, 0.916194021],+ "LeftHandThumb2": [0.018013631, -0.020968588, -0.220635763, 0.974964454],+ "LeftHandThumb3": [-0.014498566, 0.034661609, 0.011599642, 0.999226607],+ "LeftLeg": [-0.022987686, -0.000660891, 0.000079607, 0.999735527],+ "LeftShoulder": [0.51817069, 0.513438736, -0.48299308, 0.484352649],+ "LeftToeBase": [0.236522177, 0.000664411, -0.002051153, 0.971623698],+ "LeftUpLeg": [-0.000049521, 0.003256762, 0.999994619, -0.000392036],+ "Neck": [0.22251078, -2.34e-7, -2e-8, 0.97493023],+ "RightArm": [-0.003089571, -0.080181649, 0.025041836, 0.996460869],+ "RightFoot": [0.552373711, -0.000251888, -0.003653811, 0.833588549],+ "RightForeArm": [-2.71e-7, 6e-9, -0.012369383, 0.999923496],+ "RightHand": [-0.001291011, -0.002537816, -0.01011645, 0.999944774],+ "RightHandIndex1": [0.028803285, -0.011362425, -0.055965713, 0.997952456],+ "RightHandIndex2": [-0.005195229, -0.014315442, -0.000215825, 0.999884009],+ "RightHandIndex3": [-0.001315105, 0.01694132, 0.006458327, 0.999834762],+ "RightHandMiddle1": [0.027532663, 0.031607786, -0.016931205, 0.998977595],+ "RightHandMiddle2": [-0.001268027, -0.001046905, 0.004466199, 0.999988675],+ "RightHandMiddle3": [-0.001580356, -0.001835289, -0.003804068, 0.999989832],+ "RightHandPinky1": [0.00934503, 0.073774463, 0.042077846, 0.99634304],+ "RightHandPinky2": [-0.001900201, -0.000276962, -0.000496823, 0.999998033],+ "RightHandPinky3": [-0.00141575, 0.005595188, 0.004606743, 0.999972733],+ "RightHandRing1": [0.018207544, 0.034360087, 0.036855354, 0.998563745],+ "RightHandRing2": [-0.000657134, -0.01547723, -0.010034723, 0.999829649],+ "RightHandRing3": [0.001815607, 0.003099599, -0.007204627, 0.999967594],+ "RightHandThumb1": [0.190022761, 0.058826187, -0.347878339, 0.916194023],+ "RightHandThumb2": [0.018013663, 0.020968551, 0.22063585, 0.974964435],+ "RightHandThumb3": [-0.014498582, -0.03466162, -0.011599713, 0.999226606],+ "RightLeg": [-0.022987686, 0.00066094, -0.000079607, 0.999735527],+ "RightShoulder": [0.518170601, -0.513438795, 0.482993177, 0.484352589],+ "RightToeBase": [0.23652222, -0.000664411, 0.002051153, 0.971623688],+ "RightUpLeg": [0.000049439, 0.003256759, 0.999994619, 0.000392036],+ "Spine": [-0.018332563, -1.45e-7, 0, 0.999831944],+ "Spine1": [-0.038156286, 2e-8, 5e-9, 0.999271784],+ "Spine2": [-0.056998032, 1.35e-7, -1e-9, 0.998374291],+}++REST_WORLD = {+ "Head": [-0.005402767, -1.54e-7, 1e-9, 0.999985405],+ "Hips": [0.001074115, 6.5e-8, 0, 0.999999423],+ "LeftArm": [0.488931842, 0.51082873, -0.510829088, 0.488930782],+ "LeftFoot": [-0.000071124, 0.534908533, 0.8448986, -0.004383146],+ "LeftForeArm": [0.495213006, 0.504742065, -0.504742038, 0.495212082],+ "LeftHand": [0.500933472, 0.501612778, -0.497795988, 0.499649313],+ "LeftHandIndex1": [0.548028623, 0.463889663, -0.477569773, 0.506357704],+ "LeftHandIndex2": [0.552271148, 0.473447408, -0.467149807, 0.502608389],+ "LeftHandIndex3": [0.540547153, 0.469035491, -0.479052134, 0.50825539],+ "LeftHandMiddle1": [0.506936656, 0.463120118, -0.518471479, 0.509629584],+ "LeftHandMiddle2": [0.504759107, 0.466569866, -0.519623781, 0.507466191],+ "LeftHandMiddle3": [0.506680523, 0.466397518, -0.516024336, 0.509379124],+ "LeftHandPinky1": [0.445939401, 0.479343304, -0.558643433, 0.509200909],+ "LeftHandPinky2": [0.44536387, 0.480323447, -0.557354933, 0.510192015],+ "LeftHandPinky3": [0.439298117, 0.480296415, -0.561501992, 0.510928601],+ "LeftHandRing1": [0.473719962, 0.493122778, -0.541841029, 0.488699936],+ "LeftHandRing2": [0.486652669, 0.496204905, -0.529188844, 0.486733032],+ "LeftHandRing3": [0.489455309, 0.490213205, -0.528074309, 0.491184326],+ "LeftHandThumb1": [0.699113821, 0.161325716, -0.407046338, 0.565267337],+ "LeftHandThumb2": [0.647664232, 0.292351109, -0.539139393, 0.452095776],+ "LeftHandThumb3": [0.6626872, 0.308099449, -0.506790521, 0.457256795],+ "LeftLeg": [0.000620211, -0.020805294, 0.999783243, -0.00047119],+ "LeftShoulder": [0.460545094, 0.455995659, -0.537559007, 0.539440909],+ "LeftToeBase": [-0.002764356, 0.719564006, 0.694414715, -0.002864329],+ "LeftUpLeg": [-0.000049876, 0.002182651, 0.99999754, -0.000391983],+ "Neck": [0.111701494, -1.63e-7, -1.8e-8, 0.993741806],+ "RightArm": [0.488931906, -0.510828798, 0.510829028, 0.488930712],+ "RightFoot": [0.000071124, 0.53490854, 0.844898596, 0.004383145],+ "RightForeArm": [0.495213002, -0.504742067, 0.504742042, 0.495212082],+ "RightHand": [0.50093347, -0.501612779, 0.497795991, 0.499649315],+ "RightHandIndex1": [0.548028614, -0.463889675, 0.477569779, 0.506357701],+ "RightHandIndex2": [0.552271145, -0.473447408, 0.467149815, 0.502608388],+ "RightHandIndex3": [0.540547093, -0.469035426, 0.479052203, 0.50825545],+ "RightHandMiddle1": [0.506936668, -0.463120062, 0.51847151, 0.509629594],+ "RightHandMiddle2": [0.504759106, -0.466569867, 0.519623782, 0.507466192],+ "RightHandMiddle3": [0.50668052, -0.466397523, 0.516024337, 0.509379125],+ "RightHandPinky1": [0.445939397, -0.479343324, 0.558643422, 0.509200909],+ "RightHandPinky2": [0.445363807, -0.480323392, 0.557354983, 0.510192069],+ "RightHandPinky3": [0.439298127, -0.480296427, 0.561501987, 0.51092859],+ "RightHandRing1": [0.473719958, -0.493122779, 0.541841036, 0.488699934],+ "RightHandRing2": [0.486652668, -0.49620491, 0.529188844, 0.486733031],+ "RightHandRing3": [0.489455311, -0.490213203, 0.528074306, 0.491184332],+ "RightHandThumb1": [0.699113773, -0.161325695, 0.407046389, 0.565267364],+ "RightHandThumb2": [0.647664196, -0.292351142, 0.539139467, 0.452095716],+ "RightHandThumb3": [0.662687186, -0.308099448, 0.506790552, 0.45725678],+ "RightLeg": [-0.000620211, -0.020805295, 0.999783243, 0.00047119],+ "RightShoulder": [0.460545075, -0.455995643, 0.537559023, 0.539440925],+ "RightToeBase": [0.002764356, 0.719564042, 0.694414677, 0.002864329],+ "RightUpLeg": [0.000049925, 0.002182648, 0.99999754, 0.000391982],+ "Spine": [-0.017258618, -8e-8, 1e-9, 0.999851059],+ "Spine1": [-0.055396652, -6e-8, 3e-9, 0.998464426],+ "Spine2": [-0.112217101, 7.5e-8, -8e-9, 0.993683713],+}diff --git a/workers/model-video2motion/pose_solver.py b/workers/model-video2motion/pose_solver.pyindex 3dc58dc7e..7eb55e83c 100644--- a/workers/model-video2motion/pose_solver.py+++ b/workers/model-video2motion/pose_solver.py@@ -258,6 +258,142 @@ for _side in ("Left", "Right"):_FINGER_PARENTS[f"{_side}Hand{_finger}2"] = f"{_side}Hand{_finger}1"_FINGER_PARENTS[f"{_side}Hand{_finger}3"] = f"{_side}Hand{_finger}2"+# Full canonical parent map (body + fingers), used by the canonical-convention+# authoring below.+_BODY_PARENTS = {+ "Hips": None, "Spine": "Hips", "Spine1": "Spine", "Spine2": "Spine1",+ "Neck": "Spine2", "Head": "Neck",+ "LeftUpLeg": "Hips", "LeftLeg": "LeftUpLeg", "LeftFoot": "LeftLeg",+ "RightUpLeg": "Hips", "RightLeg": "RightUpLeg", "RightFoot": "RightLeg",+ "LeftArm": "Spine2", "LeftForeArm": "LeftArm", "LeftHand": "LeftForeArm",+ "RightArm": "Spine2", "RightForeArm": "RightArm", "RightHand": "RightForeArm",+}+_ALL_PARENTS = {**_BODY_PARENTS, **_FINGER_PARENTS}+++# ---------------------------------------------------------------------------+# Canonical-convention authoring+# ---------------------------------------------------------------------------+# The solver computes each bone's WORLD orientation from the landmarks in its own+# y-up rig frame, where identity local = a T-pose. But the browser retarget+# (src/animation-retarget.js) interprets a clip's keyframes as rotations relative+# to the CANONICAL rig bind pose (Mixamo/Wolf3D — legs baked 180° about Z, arms+# in a specific frame; see canonical_rest.REST_LOCAL). Emitting our own T-pose+# convention makes the retarget's world-delta correction reframe every bone into+# garbage (legs fold over the head). So we author each keyframe in the canonical+# convention with the standard world-delta retarget:+#+# q_canon = WSp⁻¹ · (G · Grest⁻¹) · WSp · Rs+#+# where G is the measured world orientation, Grest the solver's world orientation+# for the SAME bone at the reference rest pose, WSp = REST_WORLD[parent] (parent+# world bind), and Rs = REST_LOCAL[bone] (bone local bind). At rest (G == Grest)+# this returns exactly Rs, so a rest bone emits the canonical bind local and the+# retarget round-trips instead of mangling it.+from canonical_rest import REST_LOCAL, REST_WORLD # noqa: E402++_REST_GLOBALS_CACHE: Optional[dict] = None+++def _reference_rest_pose() -> tuple:+ """Synthetic reference rest: a T-pose facing the camera with flat hands, in+ MediaPipe world convention (y DOWN). Identical geometry to the solver's rest+ assumptions, so solving it yields each bone's rest world orientation."""+ p = np.zeros((33, 3))+ p[LEFT_HIP] = [0.10, 0.0, 0.0]; p[RIGHT_HIP] = [-0.10, 0.0, 0.0]+ p[LEFT_SHOULDER] = [0.18, -0.50, 0.0]; p[RIGHT_SHOULDER] = [-0.18, -0.50, 0.0]+ p[NOSE] = [0.0, -0.72, -0.10]+ p[LEFT_EAR] = [0.07, -0.70, 0.0]; p[RIGHT_EAR] = [-0.07, -0.70, 0.0]+ p[LEFT_ELBOW] = [0.45, -0.50, 0.0]; p[LEFT_WRIST] = [0.70, -0.50, 0.0]+ p[LEFT_INDEX] = [0.80, -0.50, 0.0]; p[LEFT_PINKY] = [0.79, -0.50, 0.02]+ p[RIGHT_ELBOW] = [-0.45, -0.50, 0.0]; p[RIGHT_WRIST] = [-0.70, -0.50, 0.0]+ p[RIGHT_INDEX] = [-0.80, -0.50, 0.0]; p[RIGHT_PINKY] = [-0.79, -0.50, 0.02]+ p[LEFT_KNEE] = [0.10, 0.45, 0.0]; p[LEFT_ANKLE] = [0.10, 0.90, 0.0]+ p[LEFT_HEEL] = [0.10, 0.95, 0.03]; p[LEFT_FOOT_INDEX] = [0.10, 0.95, -0.12]+ p[RIGHT_KNEE] = [-0.10, 0.45, 0.0]; p[RIGHT_ANKLE] = [-0.10, 0.90, 0.0]+ p[RIGHT_HEEL] = [-0.10, 0.95, 0.03]; p[RIGHT_FOOT_INDEX] = [-0.10, 0.95, -0.12]++ def flat_hand(side: str) -> np.ndarray:+ s = 1.0 if side == "Left" else -1.0+ h = np.zeros((21, 3))+ w = np.array([s * 0.70, -0.50, 0.0]); h[0] = w+ for base, z in {5: -0.025, 9: 0.0, 13: 0.022, 17: 0.045}.items():+ h[base] = w + [s * 0.09, 0.0, z]+ for j in range(1, 4):+ h[base + j] = h[base] + [s * 0.035 * j, 0.0, 0.0]+ thumb = np.array([s * 0.707, 0.0, -0.707])+ h[1] = w + [s * 0.025, 0.0, -0.03]+ for j, step in ((2, 0.035), (3, 0.065), (4, 0.09)):+ h[j] = h[1] + step * thumb+ return h++ hands = np.stack([flat_hand("Left"), flat_hand("Right")])+ return p, hands+++def _rest_globals() -> dict:+ global _REST_GLOBALS_CACHE+ if _REST_GLOBALS_CACHE is None:+ pose, hands = _reference_rest_pose()+ rig = _to_rig_space(pose[None, ...])[0]+ rig_hands = _to_rig_space(hands)+ frame_hands = {side: rig_hands[i] for i, side in enumerate(("Left", "Right"))}+ _REST_GLOBALS_CACHE = solve_frame(rig, None, frame_hands)["globals"]+ return _REST_GLOBALS_CACHE+++def _to_canonical_local(bone: str, g: np.ndarray, rest_globals: dict) -> np.ndarray:+ """World orientation `g` of `bone` → canonical-convention local keyframe."""+ rs = REST_LOCAL.get(bone)+ if rs is None:+ return _IDENTITY.copy()+ rs = np.asarray(rs, dtype=np.float64)+ grest = rest_globals.get(bone)+ if grest is None:+ return rs+ # D = g · grest⁻¹ (physical world delta from rest)+ delta = _quat_multiply(g, _quat_conjugate(grest))+ parent = _ALL_PARENTS.get(bone)+ wsp = np.asarray(REST_WORLD[parent], dtype=np.float64) if parent else _IDENTITY+ wsp_inv = _quat_conjugate(wsp)+ q = _quat_multiply(_quat_multiply(_quat_multiply(wsp_inv, delta), wsp), rs)+ return _quat_normalize(q)+++def _smooth_quaternion_track(values: np.ndarray, fps: float) -> np.ndarray:+ """One-Euro low-pass on a (T,4) quaternion track: heavy smoothing when the+ joint is near-still (kills tracking jitter), light when it moves fast (keeps+ real motion crisp). Operates by slerping toward each raw sample with an+ adaptive factor derived from the local angular velocity."""+ n = values.shape[0]+ if n < 3:+ return values+ # One-Euro params tuned for markerless pose: heavy smoothing at rest,+ # loosening only for genuine fast motion. A single-frame spike beyond+ # SPIKE_DEG (occlusion teleport, not real motion) is rejected — the filter+ # holds and catches up over the next frames instead of snapping.+ min_cutoff, beta = 0.8, 0.025+ SPIKE_DEG = 45.0+ out = values.copy()+ prev = values[0].copy()+ prev_speed = 0.0+ for t in range(1, n):+ raw = values[t]+ if float(np.dot(prev, raw)) < 0.0:+ raw = -raw+ dot = float(np.clip(abs(np.dot(prev, raw)), -1.0, 1.0))+ step_deg = np.degrees(2.0 * np.arccos(dot))+ speed = np.radians(step_deg) * fps # rad/s+ prev_speed = 0.7 * prev_speed + 0.3 * speed+ cutoff = min_cutoff + beta * np.degrees(prev_speed)+ tau = 1.0 / (2.0 * np.pi * cutoff)+ alpha = 1.0 / (1.0 + tau * fps)+ if step_deg > SPIKE_DEG:+ alpha = min(alpha, 0.25) # suspected teleport: ease in, don't snap+ prev = _quat_slerp(prev, raw, float(np.clip(alpha, 0.04, 1.0)))+ out[t] = prev+ return out+def smooth_landmarks(world: np.ndarray, fps: float, cutoff_hz: float = 4.0) -> np.ndarray:"""fps-aware exponential smoothing over the time axis of (T, 33, 3)."""@@ -574,6 +710,7 @@ def landmarks_to_clip(rig = smooth_landmarks(rig, fps)track_bones = CLIP_BONES + FINGER_BONES if hands is not None else CLIP_BONES+ rest_globals = _rest_globals()per_bone: dict[str, list[np.ndarray]] = {b: [] for b in track_bones}prev_solution: Optional[dict] = Noneprev_quats: dict[str, np.ndarray] = {}@@ -586,7 +723,16 @@ def landmarks_to_clip(}sol = solve_frame(rig[t], prev_solution, frame_hands)for bone in track_bones:- q = _hemisphere(prev_quats.get(bone), sol["locals"][bone])+ # Author in the canonical rig convention (see _to_canonical_local),+ # not the solver's own T-pose frame, so the browser retarget+ # round-trips instead of folding the skeleton. A held bone (no global+ # this frame, e.g. an undetected finger) rests at its bind local.+ gb = sol["globals"].get(bone)+ if gb is None:+ q = np.asarray(REST_LOCAL.get(bone, _IDENTITY), dtype=np.float64)+ else:+ q = _to_canonical_local(bone, gb, rest_globals)+ q = _hemisphere(prev_quats.get(bone), q)prev_quats[bone] = qper_bone[bone].append(q)prev_solution = sol@@ -596,6 +742,8 @@ def landmarks_to_clip(tracks = []for bone in track_bones:values = np.stack(per_bone[bone], axis=0)+ if smooth:+ values = _smooth_quaternion_track(values, float(fps))tracks.append({"type": "quaternion",diff --git a/workers/model-video2motion/test_pose_solver.py b/workers/model-video2motion/test_pose_solver.pyindex 25edf4fc8..002a50ac7 100644--- a/workers/model-video2motion/test_pose_solver.py+++ b/workers/model-video2motion/test_pose_solver.py@@ -232,13 +232,37 @@ def test_clip_with_hands_carries_finger_tracks():continuevals = np.asarray(track["values"]).reshape(-1, 4)assert np.allclose(np.linalg.norm(vals, axis=1), 1.0, atol=1e-6)- # The never-detected right hand stays at rest for the whole clip.+ # The never-detected right hand holds its canonical bind local (not+ # identity — the clip is authored in the canonical rig convention) for+ # the whole clip: every frame equals frame 0.if track["name"].startswith("RightHandIndex"):- assert np.allclose(np.abs(vals[:, 3]), 1.0, atol=1e-6)+ assert np.allclose(vals - vals[0], 0.0, atol=1e-6)b = landmarks_to_clip(frames, fps=24, name="hands", hands=hands)assert clip == b+def test_rest_pose_emits_canonical_bind_local():+ """A person at the reference rest pose must author each keyframe as the+ canonical rig's BIND local (e.g. LeftUpLeg ~180° about Z), not identity —+ otherwise the browser retarget folds the skeleton (the shipped-broken bug).+ """+ from canonical_rest import REST_LOCAL++ frames = np.stack([t_pose_world()] * 4)+ clip = landmarks_to_clip(frames, fps=24, name="rest")+ tracks = {t["name"].split(".")[0]: t for t in clip["tracks"] if t["type"] == "quaternion"}+ for bone in ("LeftUpLeg", "RightUpLeg", "LeftArm", "Hips", "Spine"):+ got = np.asarray(tracks[bone]["values"]).reshape(-1, 4)[0]+ want = np.asarray(REST_LOCAL[bone])+ # quaternion equality up to sign+ assert abs(abs(float(np.dot(got, want))) - 1.0) < 2e-2, (+ f"{bone} rest should be its canonical bind local {want}, got {got}"+ )+ # LeftUpLeg's canonical bind is a ~180° flip — the exact thing identity got wrong.+ up = np.asarray(tracks["LeftUpLeg"]["values"]).reshape(-1, 4)[0]+ assert abs(up[3]) < 0.1, "LeftUpLeg bind must be a large rotation, not identity"++def test_clip_without_hands_unchanged():frames = np.stack([t_pose_world()] * 4)clip = landmarks_to_clip(frames, fps=24, name="nohands")