cozyclay 1.9.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +171 -0
- package/bin/agent/agent-routes.mjs +74 -70
- package/bin/agent/agent-runner.mjs +10 -3
- package/bin/agent/motion-runtime.mjs +32 -260
- package/bin/agent/providers.mjs +46 -3
- package/bin/agent/session-store.mjs +41 -5
- package/bin/agent/studio-prompt.mjs +1 -1
- package/bin/agent/studio-tools.mjs +48 -11
- package/bin/cozyclay.mjs +26 -8
- package/bin/live/cli.mjs +22 -3
- package/bin/mcp-runtime.mjs +35 -20
- package/bin/telemetry-state.mjs +5 -2
- package/bin/update-check.mjs +15 -0
- package/dist/app/index.html +6 -6
- package/dist/assets/analytics-MVZOEW8O.js +1 -0
- package/dist/assets/app-C1sdVbg4.css +1 -0
- package/dist/assets/app-CA55tcI3.js +4876 -0
- package/dist/assets/{demo-B-eg1CT8.js → demo-ByeYp-dY.js} +1 -1
- package/dist/assets/{first-shot-handoff-CGgnvNu8.js → first-shot-handoff-DgT28ZiV.js} +1 -1
- package/dist/assets/{landing-IbRp3FnQ.js → landing-BaXkpFip.js} +1 -1
- package/dist/assets/shot-prompt-CbqWcWYi.css +1 -0
- package/dist/assets/shot-prompt-Yrt99wZx.js +17 -0
- package/dist/assets/{ticket-BgIzOBOf.js → ticket-BcmsnHn5.js} +1 -1
- package/dist/assets/workflow-DRdwoDBT.css +1 -0
- package/dist/assets/workflow-dNPGY9xz.js +179 -0
- package/dist/cozyclay-package.json +1 -1
- package/dist/fonts/IBM-Plex-Sans-OFL.txt +92 -0
- package/dist/fonts/JetBrains-Mono-OFL.txt +93 -0
- package/dist/fonts/README.md +13 -9
- package/dist/fonts/ibm-plex-sans-400-latin.woff2 +0 -0
- package/dist/fonts/ibm-plex-sans-500-latin.woff2 +0 -0
- package/dist/fonts/ibm-plex-sans-600-latin.woff2 +0 -0
- package/dist/fonts/jetbrains-mono-400-latin.woff2 +0 -0
- package/dist/fonts/jetbrains-mono-500-latin.woff2 +0 -0
- package/dist/index.html +7 -7
- package/dist/privacy/index.html +13 -8
- package/dist/sitemap.xml +6 -6
- package/dist/workflow/index.html +5 -5
- package/mcp/LIVE-PROTOCOL.md +1 -1
- package/mcp/live-hub.mjs +35 -93
- package/mcp/mesh-file.mjs +48 -0
- package/mcp/server.mjs +6 -77
- package/mcp/tool-handlers.mjs +224 -377
- package/package.json +2 -1
- package/src/App.jsx +1273 -9075
- package/src/analytics.js +393 -9
- package/src/app-context.js +211 -0
- package/src/app-stage.jsx +25 -24
- package/src/ardy/auto-fix-panel.css +108 -0
- package/src/ardy/collision-blockers.js +9 -3
- package/src/ardy/fix-collisions.js +10 -66
- package/src/ardy/ground.js +4 -4
- package/src/ardy/ik-drag.js +101 -0
- package/src/ardy/ik-key-json.js +43 -0
- package/src/ardy/ik.js +122 -76
- package/src/ardy/physics-panel.css +2 -0
- package/src/ardy/physics-panel.jsx +17 -5
- package/src/ardy/platform-fit-panel.css +64 -0
- package/src/ardy/platform-fit-panel.jsx +40 -0
- package/src/ardy/platform-fit.js +326 -0
- package/src/ardy/playback.js +166 -20
- package/src/ardy/range-pin.js +299 -0
- package/src/ardy/timeline-coordinates.js +26 -0
- package/src/ardy/timeline.css +1199 -0
- package/src/ardy/timeline.jsx +149 -41
- package/src/ardy/waypoints.js +2 -2
- package/src/asset-pane.css +462 -0
- package/src/asset-pane.jsx +461 -232
- package/src/command-bus.js +349 -0
- package/src/commands/ai.js +31 -0
- package/src/commands/cast.js +140 -0
- package/src/commands/elements/character.js +13 -0
- package/src/commands/elements/motion.js +14 -0
- package/src/commands/elements/object.js +22 -0
- package/src/commands/elements/scene.js +16 -0
- package/src/commands/elements/shot.js +9 -0
- package/src/commands/elements/stage.js +11 -0
- package/src/commands/elements.js +155 -0
- package/src/commands/export.js +20 -0
- package/src/commands/index.js +40 -0
- package/src/commands/motion.js +231 -0
- package/src/commands/objects.js +174 -0
- package/src/commands/project.js +53 -0
- package/src/commands/scene.js +81 -0
- package/src/commands/shared.js +19 -0
- package/src/commands/shot.js +103 -0
- package/src/commands/stage.js +17 -0
- package/src/commands/view.js +42 -0
- package/src/document-store.js +144 -0
- package/src/domains/cast.js +1007 -0
- package/src/domains/motion.js +3219 -0
- package/src/domains/objects.js +932 -0
- package/src/domains/scenes.js +817 -0
- package/src/domains/shots.js +404 -0
- package/src/domains/stage.js +93 -0
- package/src/dualview.jsx +113 -57
- package/src/facing-marks.js +3 -0
- package/src/first-success-guide.jsx +14 -14
- package/src/grid-view.js +18 -5
- package/src/hierarchy-model.js +24 -16
- package/src/hierarchy-panel.css +386 -0
- package/src/hierarchy-panel.jsx +150 -21
- package/src/{fal-motion-client.js → i2v-motion-client.js} +13 -13
- package/src/i2v-motion-studio.jsx +180 -0
- package/src/ik-camera.js +3 -0
- package/src/main.jsx +6 -0
- package/src/motion/generation.js +105 -0
- package/src/motion-readiness-ui.jsx +4 -4
- package/src/motion-readiness.js +11 -0
- package/src/motion-trail.js +366 -9
- package/src/object-gizmo.jsx +7 -1
- package/src/otio.js +2 -1
- package/src/panels/CameraPanel.jsx +60 -0
- package/src/panels/CharacterTransformPanel.jsx +48 -0
- package/src/panels/EnvironmentPanel.jsx +40 -0
- package/src/panels/Foldout.jsx +32 -0
- package/src/panels/LightPanel.jsx +24 -0
- package/src/panels/ObjectTransformPanel.jsx +554 -0
- package/src/panels/PosePanel.jsx +97 -0
- package/src/panels/ProjectPanel.jsx +33 -0
- package/src/panels/PromptBlocksPanel.jsx +442 -0
- package/src/panels/PropsPanel.jsx +70 -0
- package/src/panels/ReferenceImageField.jsx +91 -0
- package/src/panels/RigControlPanel.jsx +78 -0
- package/src/panels/RigPanel.jsx +37 -0
- package/src/panels/SubjectBox.jsx +47 -0
- package/src/panels/SubjectsPanel.jsx +39 -0
- package/src/panels/VideoCapturePanel.jsx +180 -0
- package/src/panels/details.css +1171 -0
- package/src/panels/motion.css +197 -0
- package/src/panels/pose.css +39 -0
- package/src/planview.jsx +60 -31
- package/src/posestudio.jsx +59 -4
- package/src/project-browser.css +1073 -0
- package/src/project-browser.jsx +284 -123
- package/src/range-pin-object-transform.js +19 -0
- package/src/range-pin-panel.css +518 -0
- package/src/range-pin-panel.jsx +344 -0
- package/src/result-modal.jsx +5 -5
- package/src/room.jsx +79 -51
- package/src/scene-objects.js +37 -1
- package/src/scenes.js +5 -0
- package/src/semantic-edit.js +2 -0
- package/src/settings-menu.jsx +39 -105
- package/src/shell/BottomDock.jsx +450 -0
- package/src/shell/DetailsSlot.jsx +513 -0
- package/src/shell/LibrarySlot.jsx +86 -0
- package/src/shell/MenuBar.jsx +601 -0
- package/src/shell/OutlinerSlot.jsx +50 -0
- package/src/shell/PreferencesDialog.jsx +538 -0
- package/src/shell/PreferencesSlot.jsx +40 -0
- package/src/shell/StatusBar.jsx +125 -0
- package/src/shell/StudioShell.jsx +92 -0
- package/src/shell/TopBar.jsx +129 -0
- package/src/shell/ViewportToolbar.jsx +656 -0
- package/src/shell/agent-glass.css +296 -0
- package/src/shell/dock.css +93 -0
- package/src/shell/glass-regions.css +985 -0
- package/src/shell/glass.css +687 -0
- package/src/shell/log-store.js +52 -0
- package/src/shell/mode.css +9 -0
- package/src/shell/preferences.css +651 -0
- package/src/shell/shell.css +282 -0
- package/src/shell/studio-shell-context.js +19 -0
- package/src/shell/topbar.css +435 -0
- package/src/shell/viewport.css +451 -0
- package/src/store/authored-intent.js +22 -0
- package/src/store/runtime-adapters.js +92 -0
- package/src/store/scene-stage.js +21 -0
- package/src/store/use-document-store.js +14 -0
- package/src/studio-actions.js +210 -0
- package/src/studio-agent-commands.js +41 -271
- package/src/studio-agent-context.js +39 -10
- package/src/studio-agent-motion.js +122 -373
- package/src/studio-agent-protocol.js +67 -31
- package/src/studio-app-binding.js +379 -0
- package/src/studio-contact-sheet.js +73 -0
- package/src/studio-elements.js +58 -51
- package/src/styles/themes.css +200 -0
- package/src/styles/tokens.css +69 -0
- package/src/styles.css +460 -1742
- package/src/theme.js +44 -0
- package/src/timeline-extent.js +16 -0
- package/src/trail-key-conflicts.js +39 -0
- package/src/trail-pick.js +59 -0
- package/src/ui.jsx +8 -8
- package/src/use-case-question.jsx +66 -0
- package/src/workflow/AgentPanel.jsx +82 -69
- package/src/workflow/agent-client.js +12 -2
- package/src/workflow/agent-panel.css +34 -32
- package/src/workflow/cozy-scene-node.css +2 -0
- package/src/workflow/workflow.css +2 -0
- package/tools/ardy/__pycache__/cclay_gvhmr_worker.cpython-313.pyc +0 -0
- package/tools/ardy/bridge.mjs +164 -146
- package/tools/ardy/visual-qa.mjs +5 -5
- package/tools/bench/EXP3.md +61 -0
- package/tools/bench/cclay_bench_extract_incam.py +125 -0
- package/tools/bench/cclay_bench_extract_obs.py +204 -0
- package/tools/bench/cclay_bench_runner.py +42 -0
- package/tools/bench/cube-contact.mjs +162 -0
- package/tools/bench/exp3.mjs +152 -0
- package/tools/bench/extract-bench-lib.mjs +216 -0
- package/tools/bench/extract-bench.mjs +302 -0
- package/tools/bench/fal-generate.mjs +88 -0
- package/tools/bench/fit/README.md +189 -0
- package/tools/bench/fit/camera.mjs +59 -0
- package/tools/bench/fit/contact.mjs +419 -0
- package/tools/bench/fit/footlock.mjs +234 -0
- package/tools/bench/fit/motion.mjs +85 -0
- package/tools/bench/fit/pin.mjs +27 -0
- package/tools/bench/fit/remote.mjs +105 -0
- package/tools/bench/fit-bench.mjs +126 -0
- package/tools/bench/fit-sanity.mjs +45 -0
- package/tools/bench/metrics.mjs +313 -0
- package/tools/bench/obs/depth.mjs +258 -0
- package/tools/bench/obs/extrinsics.mjs +175 -0
- package/tools/bench/obs/fit_mannequin_betas.py +340 -0
- package/tools/bench/obs/ground.mjs +294 -0
- package/tools/bench/obs/heading.mjs +151 -0
- package/tools/bench/obs/ladder.mjs +555 -0
- package/tools/bench/obs/mannequin-betas.json +124 -0
- package/tools/bench/obs/remote.mjs +83 -0
- package/tools/bench/obs/rest_joints.py +52 -0
- package/tools/bench/obs/ybot-targets.mjs +49 -0
- package/tools/bench/obs-bench.mjs +520 -0
- package/tools/bench/score.mjs +343 -0
- package/tools/bench/summarize.mjs +86 -0
- package/tools/dev/pages/privacy.html +13 -8
- package/tools/gt-render/browser.mjs +159 -0
- package/tools/gt-render/camera-math.mjs +276 -0
- package/tools/gt-render/page.mjs +267 -0
- package/tools/gt-render/render.mjs +514 -0
- package/tools/gt-render/scene-box.mjs +31 -0
- package/tools/gt-render/take-transform.mjs +87 -0
- package/tools/morphgs/assets/gen_truth.py +39 -0
- package/tools/morphgs/assets/mesh_ori_rig.txt +27 -0
- package/tools/morphgs/assets/playback-check.mjs +30 -0
- package/tools/morphgs/demo-gate.sh +26 -0
- package/tools/morphgs/fbx2morphgs.mjs +68 -0
- package/tools/morphgs/morphgs-to-cskel27.mjs +105 -0
- package/tools/morphgs/patches/preprocess_src-none-mode.patch +76 -0
- package/tools/morphgs/setup-on-cluster.sh +126 -0
- package/tools/qa/css-rule-usage.mjs +283 -0
- package/tools/qa/studio-control-count.mjs +70 -32
- package/tools/run-tests.mjs +134 -12
- package/tools/track/DESIGN.md +513 -0
- package/tools/track/backfill-provenance.mjs +116 -0
- package/tools/track/budget.mjs +8 -0
- package/tools/track/check-rig.mjs +209 -0
- package/tools/track/diagnostics.schema.json +60 -0
- package/tools/track/export-rig.mjs +256 -0
- package/tools/track/fallback.mjs +7 -0
- package/tools/track/fk-parity-fixture.mjs +161 -0
- package/tools/track/gate.mjs +349 -0
- package/tools/track/masks.mjs +318 -0
- package/tools/track/metrics.mjs +201 -0
- package/tools/track/publish-obs.mjs +108 -0
- package/tools/track/py/check_env.py +61 -0
- package/tools/track/py/eval_lr.py +229 -0
- package/tools/track/py/lr_viterbi.py +294 -0
- package/tools/track/py/masks.py +405 -0
- package/tools/track/py/objective.py +382 -0
- package/tools/track/py/rig.py +508 -0
- package/tools/track/py/scene.py +326 -0
- package/tools/track/py/test_joint_indices.py +123 -0
- package/tools/track/py/test_lr_viterbi.py +195 -0
- package/tools/track/py/test_masks.py +229 -0
- package/tools/track/py/test_rig.py +288 -0
- package/tools/track/py/test_scene.py +354 -0
- package/tools/track/py/test_track.py +328 -0
- package/tools/track/py/track.py +411 -0
- package/tools/track/remote.mjs +140 -0
- package/tools/track/rig-dump.mjs +227 -0
- package/tools/track/run-box-tests.mjs +14 -0
- package/tools/track/run-box.mjs +168 -0
- package/tools/track/setup-box.sh +59 -0
- package/tools/track/study-2d.mjs +728 -0
- package/dist/assets/analytics-B1hnH66c.js +0 -1
- package/dist/assets/app-BWusbqgO.js +0 -4861
- package/dist/assets/app-qKDo4PBX.css +0 -1
- package/dist/assets/shot-prompt-CHUtw6af.js +0 -17
- package/dist/assets/shot-prompt-C_g2BHVc.css +0 -1
- package/dist/assets/workflow-CVwzMhz2.js +0 -179
- package/dist/assets/workflow-DsudxKHF.css +0 -1
- package/src/fal-motion-studio.jsx +0 -180
- package/src/scene-history.js +0 -129
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
"""Differentiable Studio rig for the known-character tracker (plan mocap-rearch todo 6).
|
|
2
|
+
|
|
3
|
+
Two deliberately separate layers:
|
|
4
|
+
|
|
5
|
+
1. `cskel27_fk(state, bone_scale, rig)` -> posedJoints. cskel27 forward kinematics
|
|
6
|
+
over the canonical neutral skeleton grown by `bone_scale` - exactly the
|
|
7
|
+
quantity a motion NPZ stores as `posed_joints` (tools/ardy/smpl-cskel27.mjs,
|
|
8
|
+
tools/bench/fit/motion.mjs regenerateJoints) and the scorers read. Keypoint and
|
|
9
|
+
contact terms use these joints.
|
|
10
|
+
2. `studio_skin(globals, posed_joints, rig, ...)` -> bone world transforms ->
|
|
11
|
+
vertices. A torch port of Studio playback's positional skinning
|
|
12
|
+
(src/ardy/playback.js applyMotionFrame, prepOf, scaledOffsets, boneStretch)
|
|
13
|
+
followed by linear-blend skinning of the exported rig mesh. Silhouette and box
|
|
14
|
+
terms use these vertices.
|
|
15
|
+
|
|
16
|
+
State convention (NPZ `local_rot_mats` convention): per frame
|
|
17
|
+
transl (T, 3) root/pelvis world position = posed_joints[:, 0]
|
|
18
|
+
root6d (T, 6) a_t, the root rotation = local_rot_mats[:, 0]
|
|
19
|
+
local6d (T, 26, 6) u_t,j for cskel27 joints 1..26 (local to the parent)
|
|
20
|
+
Zero pose = identity locals = the neutral T-pose, so 6D vectors are the rotations
|
|
21
|
+
themselves (no rest delta). `rot6d_to_matrix` follows the ultrabrain design
|
|
22
|
+
section 1: the 6D vector is the first two COLUMNS of the matrix, Gram-Schmidt.
|
|
23
|
+
|
|
24
|
+
Rig input: the npz written by tools/track/export-rig.mjs (layout and units in
|
|
25
|
+
tools/track/rig-dump.mjs). `load_rig` validates it (`RigError.code`:
|
|
26
|
+
`bad-rig`, `bad-skin-weights`) and decimates the merged mesh to <= 8,000 faces,
|
|
27
|
+
carrying skin weights from the nearest original vertex (renormalised).
|
|
28
|
+
|
|
29
|
+
World frame: Studio world, metres, +Y up. `studio_skin` returns the rig as Studio
|
|
30
|
+
places it under a Character group at the origin with no clip-to-scene yaw:
|
|
31
|
+
rig-space bone = s * (posed - anchor_xz) + R_global @ offset
|
|
32
|
+
with s = prep scale (rig leg height / canonical leg height, rig units) and the
|
|
33
|
+
0.01 rig root scale taking rig units to metres. Pass `anchor_xz=None` for the
|
|
34
|
+
tracker (no anchoring: vertices live in the posedJoints frame, scaled by the
|
|
35
|
+
rig's leg ratio about the XZ origin exactly as Studio scales them about the
|
|
36
|
+
anchor), or the anchor frame's root XZ to reproduce Studio playback bit-for-bit.
|
|
37
|
+
Nothing here optimises bone lengths, shape or camera.
|
|
38
|
+
"""
|
|
39
|
+
from __future__ import annotations
|
|
40
|
+
|
|
41
|
+
import json
|
|
42
|
+
from dataclasses import dataclass
|
|
43
|
+
from pathlib import Path
|
|
44
|
+
|
|
45
|
+
import numpy as np
|
|
46
|
+
import torch
|
|
47
|
+
|
|
48
|
+
J = 27
|
|
49
|
+
INFLUENCES = 4
|
|
50
|
+
WEIGHT_TOLERANCE = 1e-5 # same as tools/track/rig-dump.mjs
|
|
51
|
+
MAX_FACES = 8000
|
|
52
|
+
EPS = 1e-8
|
|
53
|
+
|
|
54
|
+
REQUIRED_MEMBERS = (
|
|
55
|
+
"meta_json", "rig_root_scale", "vertices", "faces", "skin_index", "skin_weight",
|
|
56
|
+
"bone_parent", "bone_rest_local", "bone_bind_inverse",
|
|
57
|
+
"prep_bone", "prep_scale", "prep_offsets", "prep_bind_pos", "prep_bind_quat",
|
|
58
|
+
"prep_bind_scale", "prep_bind_local_pos", "prep_parent_bind_world",
|
|
59
|
+
"prep_chain_parent", "prep_chain_rel", "prep_canonical_bone_length",
|
|
60
|
+
"prep_rig_bone_length", "prep_stretched_leaves", "prep_stretched_leaf_local_pos",
|
|
61
|
+
"prep_stretched_leaf_local_quat", "hierarchy_preserved", "girdle_joints",
|
|
62
|
+
"ardy_neutral_min_y", "cskel27_parents", "cskel27_neutral",
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class RigError(ValueError):
|
|
67
|
+
"""Malformed rig input. `code` is the machine-readable reason."""
|
|
68
|
+
|
|
69
|
+
def __init__(self, code: str, detail: str):
|
|
70
|
+
super().__init__(f"{code}: {detail}")
|
|
71
|
+
self.code = code
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
# --- rotations -----------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
def rot6d_to_matrix(a: torch.Tensor) -> torch.Tensor:
|
|
77
|
+
"""(..., 6) -> (..., 3, 3). a = [a1, a2]; columns b1, b2, b3 (Gram-Schmidt)."""
|
|
78
|
+
# Clamp (not add) the epsilon: columns stay exactly unit length unless degenerate.
|
|
79
|
+
a1, a2 = a[..., 0:3], a[..., 3:6]
|
|
80
|
+
b1 = a1 / a1.norm(dim=-1, keepdim=True).clamp_min(EPS)
|
|
81
|
+
a2 = a2 - b1 * (b1 * a2).sum(dim=-1, keepdim=True)
|
|
82
|
+
b2 = a2 / a2.norm(dim=-1, keepdim=True).clamp_min(EPS)
|
|
83
|
+
b3 = torch.cross(b1, b2, dim=-1)
|
|
84
|
+
return torch.stack((b1, b2, b3), dim=-1)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def matrix_to_rot6d(R: torch.Tensor) -> torch.Tensor:
|
|
88
|
+
"""(..., 3, 3) -> (..., 6): the first two columns."""
|
|
89
|
+
return torch.cat((R[..., :, 0], R[..., :, 1]), dim=-1)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def quat_to_matrix(q: torch.Tensor) -> torch.Tensor:
|
|
93
|
+
"""(..., 4) three.js [x, y, z, w] (normalised here) -> (..., 3, 3)."""
|
|
94
|
+
q = q / q.norm(dim=-1, keepdim=True)
|
|
95
|
+
x, y, z, w = q.unbind(-1)
|
|
96
|
+
return torch.stack((
|
|
97
|
+
1 - 2 * (y * y + z * z), 2 * (x * y - w * z), 2 * (x * z + w * y),
|
|
98
|
+
2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x),
|
|
99
|
+
2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y),
|
|
100
|
+
), dim=-1).reshape(q.shape[:-1] + (3, 3))
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _affine(R: torch.Tensor, t: torch.Tensor) -> torch.Tensor:
|
|
104
|
+
"""(..., 3, 3), (..., 3) -> (..., 4, 4)."""
|
|
105
|
+
top = torch.cat((R, t.unsqueeze(-1)), dim=-1)
|
|
106
|
+
bottom = torch.zeros(top.shape[:-2] + (1, 4), dtype=top.dtype, device=top.device)
|
|
107
|
+
bottom[..., 0, 3] = 1
|
|
108
|
+
return torch.cat((top, bottom), dim=-2)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _apply(M: torch.Tensor, p: torch.Tensor) -> torch.Tensor:
|
|
112
|
+
"""Affine (..., 4, 4) applied to points (..., 3)."""
|
|
113
|
+
return (M[..., :3, :3] @ p.unsqueeze(-1)).squeeze(-1) + M[..., :3, 3]
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
# --- rig -----------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
@dataclass
|
|
119
|
+
class Rig:
|
|
120
|
+
"""Exported Studio rig as torch tensors (float, on `device`)."""
|
|
121
|
+
|
|
122
|
+
meta: dict
|
|
123
|
+
parents: list # cskel27 parent per joint, -1 for the root
|
|
124
|
+
neutral: torch.Tensor # (27, 3) canonical neutral, hips at the origin
|
|
125
|
+
min_y: float # ARDY_NEUTRAL_MIN_Y
|
|
126
|
+
root_scale: float # rig units -> metres (0.01)
|
|
127
|
+
bone_names: list
|
|
128
|
+
bone_parent: list # per exported bone, -1 for bones under the (static) rig root
|
|
129
|
+
bone_rest_local: torch.Tensor # (B, 4, 4) rig units
|
|
130
|
+
bone_rest_scale: torch.Tensor # (B, 3)
|
|
131
|
+
bind_inverse: torch.Tensor # (B, 4, 4) metres
|
|
132
|
+
# prepOf
|
|
133
|
+
prep_bone: list # cskel27 -> bone index or -1
|
|
134
|
+
prep_scale: float
|
|
135
|
+
prep_offsets: torch.Tensor # (27, 3)
|
|
136
|
+
prep_bind_pos: torch.Tensor # (27, 3)
|
|
137
|
+
prep_bind_rot: torch.Tensor # (27, 3, 3)
|
|
138
|
+
prep_bind_scale: torch.Tensor # (27, 3)
|
|
139
|
+
prep_bind_local_pos: torch.Tensor # (27, 3)
|
|
140
|
+
prep_parent_bind_world: torch.Tensor # (27, 4, 4)
|
|
141
|
+
prep_chain_parent: list
|
|
142
|
+
prep_chain_rel: torch.Tensor # (27, 4, 4)
|
|
143
|
+
canonical_bone_length: np.ndarray # (27,)
|
|
144
|
+
rig_bone_length: np.ndarray # (27,)
|
|
145
|
+
leaves: list # [(bone, joint, local_pos (3,), local_rot (3, 3))]
|
|
146
|
+
hierarchy_preserved: np.ndarray # (27,) bool
|
|
147
|
+
girdle: np.ndarray # (27,) bool
|
|
148
|
+
# full-resolution merged mesh (rest pose, metres)
|
|
149
|
+
vertices: torch.Tensor # (V, 3)
|
|
150
|
+
skin_index: torch.Tensor # (V, 4) long
|
|
151
|
+
skin_weight: torch.Tensor # (V, 4)
|
|
152
|
+
# decimated mesh used by the tracker's silhouette/box terms
|
|
153
|
+
dec_vertices: torch.Tensor # (Vd, 3)
|
|
154
|
+
dec_faces: torch.Tensor # (Fd, 3) long
|
|
155
|
+
dec_skin_index: torch.Tensor # (Vd, 4) long
|
|
156
|
+
dec_skin_weight: torch.Tensor # (Vd, 4)
|
|
157
|
+
dec_weights: torch.Tensor # (Vd, B) dense blend weights
|
|
158
|
+
dec_source: np.ndarray # (Vd,) nearest original vertex
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _require(z, name, shape=None, dtype_kind=None):
|
|
162
|
+
if name not in z:
|
|
163
|
+
raise RigError("bad-rig", f"missing member {name}")
|
|
164
|
+
value = np.asarray(z[name])
|
|
165
|
+
if shape is not None and (value.ndim != len(shape) or any(s is not None and s != d for s, d in zip(shape, value.shape))):
|
|
166
|
+
raise RigError("bad-rig", f"{name} has shape {value.shape}, expected {shape}")
|
|
167
|
+
if dtype_kind == "f" and not np.all(np.isfinite(value)):
|
|
168
|
+
raise RigError("bad-rig", f"{name} has non-finite values")
|
|
169
|
+
return value
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def validate_skin_weights(skin_weight: np.ndarray, tol: float = WEIGHT_TOLERANCE) -> None:
|
|
173
|
+
"""Raise `bad-skin-weights` unless every row is non-negative and sums to 1."""
|
|
174
|
+
w = np.asarray(skin_weight, dtype=np.float64)
|
|
175
|
+
if not np.all(np.isfinite(w)):
|
|
176
|
+
raise RigError("bad-skin-weights", "non-finite skin weight")
|
|
177
|
+
if (w < 0).any():
|
|
178
|
+
v = int(np.argwhere(w < 0)[0, 0])
|
|
179
|
+
raise RigError("bad-skin-weights", f"vertex {v} has a negative weight")
|
|
180
|
+
err = np.abs(w.sum(axis=1) - 1)
|
|
181
|
+
if (err > tol).any():
|
|
182
|
+
v = int(err.argmax())
|
|
183
|
+
raise RigError("bad-skin-weights", f"vertex {v} weights sum to {w[v].sum():.7f}")
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def decimate(vertices: np.ndarray, faces: np.ndarray, skin_index: np.ndarray, skin_weight: np.ndarray,
|
|
187
|
+
max_faces: int = MAX_FACES):
|
|
188
|
+
"""Weld, simplify to <= max_faces, transfer skin weights from the nearest original vertex.
|
|
189
|
+
|
|
190
|
+
Returns (vertices (Vd,3), faces (Fd,3), skin_index (Vd,4), skin_weight (Vd,4), source (Vd,)).
|
|
191
|
+
The export is unindexed per triangle, so identical positions are welded first
|
|
192
|
+
(fast_simplification needs shared vertices to collapse edges).
|
|
193
|
+
"""
|
|
194
|
+
import fast_simplification
|
|
195
|
+
from scipy.spatial import cKDTree
|
|
196
|
+
|
|
197
|
+
welded, first, inverse = np.unique(vertices, axis=0, return_index=True, return_inverse=True)
|
|
198
|
+
inverse = inverse.reshape(-1)
|
|
199
|
+
wf = inverse[faces]
|
|
200
|
+
keep = (wf[:, 0] != wf[:, 1]) & (wf[:, 1] != wf[:, 2]) & (wf[:, 0] != wf[:, 2])
|
|
201
|
+
wf = wf[keep]
|
|
202
|
+
reduction = 1.0 - max_faces / len(wf) if len(wf) > max_faces else 0.0
|
|
203
|
+
for _ in range(20):
|
|
204
|
+
if reduction <= 0:
|
|
205
|
+
points, out_faces = welded, wf
|
|
206
|
+
else:
|
|
207
|
+
points, out_faces = fast_simplification.simplify(
|
|
208
|
+
welded.astype(np.float32), wf.astype(np.int32), target_reduction=min(reduction, 0.999))
|
|
209
|
+
if len(out_faces) <= max_faces:
|
|
210
|
+
break
|
|
211
|
+
reduction += (1 - reduction) * 0.05
|
|
212
|
+
else:
|
|
213
|
+
raise RigError("bad-rig", f"could not decimate below {max_faces} faces")
|
|
214
|
+
points = np.asarray(points, dtype=np.float32)
|
|
215
|
+
out_faces = np.asarray(out_faces, dtype=np.int64)
|
|
216
|
+
used = np.unique(out_faces) # drop vertices no face references
|
|
217
|
+
remap = np.full(len(points), -1, dtype=np.int64)
|
|
218
|
+
remap[used] = np.arange(len(used))
|
|
219
|
+
points, out_faces = points[used], remap[out_faces]
|
|
220
|
+
_, nearest = cKDTree(vertices).query(points, k=1)
|
|
221
|
+
source = np.asarray(nearest, dtype=np.int64)
|
|
222
|
+
w = skin_weight[source].astype(np.float64)
|
|
223
|
+
w = w / w.sum(axis=1, keepdims=True)
|
|
224
|
+
return points, out_faces, skin_index[source].astype(np.int64), w.astype(np.float32), source
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def load_rig(path, device="cpu", dtype=torch.float32, max_faces: int = MAX_FACES) -> Rig:
|
|
228
|
+
path = Path(path)
|
|
229
|
+
if not path.is_file():
|
|
230
|
+
raise RigError("bad-rig", f"rig npz not found: {path}")
|
|
231
|
+
with np.load(path) as z:
|
|
232
|
+
missing = [name for name in REQUIRED_MEMBERS if name not in z]
|
|
233
|
+
if missing:
|
|
234
|
+
raise RigError("bad-rig", f"missing members: {', '.join(missing)}")
|
|
235
|
+
meta = json.loads(bytes(z["meta_json"].astype(np.uint8)))
|
|
236
|
+
vertices = _require(z, "vertices", (None, 3), "f").astype(np.float32)
|
|
237
|
+
V = len(vertices)
|
|
238
|
+
faces = _require(z, "faces", (None, 3)).astype(np.int64)
|
|
239
|
+
skin_index = _require(z, "skin_index", (V, INFLUENCES)).astype(np.int64)
|
|
240
|
+
skin_weight = _require(z, "skin_weight", (V, INFLUENCES), "f").astype(np.float32)
|
|
241
|
+
bone_parent = _require(z, "bone_parent", (None,)).astype(np.int64)
|
|
242
|
+
B = len(bone_parent)
|
|
243
|
+
arrays = {name: _require(z, name, shape, "f") for name, shape in (
|
|
244
|
+
("bone_rest_local", (B, 4, 4)), ("bone_bind_inverse", (B, 4, 4)),
|
|
245
|
+
("prep_offsets", (J, 3)), ("prep_bind_pos", (J, 3)), ("prep_bind_quat", (J, 4)),
|
|
246
|
+
("prep_bind_scale", (J, 3)), ("prep_bind_local_pos", (J, 3)),
|
|
247
|
+
("prep_parent_bind_world", (J, 4, 4)), ("prep_chain_rel", (J, 4, 4)),
|
|
248
|
+
("prep_canonical_bone_length", (J,)), ("prep_rig_bone_length", (J,)),
|
|
249
|
+
("cskel27_neutral", (J, 3)), ("rig_root_scale", (1,)), ("prep_scale", (1,)),
|
|
250
|
+
("ardy_neutral_min_y", (1,)),
|
|
251
|
+
)}
|
|
252
|
+
prep_bone = _require(z, "prep_bone", (J,)).astype(np.int64)
|
|
253
|
+
chain_parent = _require(z, "prep_chain_parent", (J,)).astype(np.int64)
|
|
254
|
+
parents = _require(z, "cskel27_parents", (J,)).astype(np.int64)
|
|
255
|
+
leaves_idx = _require(z, "prep_stretched_leaves", (None, 2)).astype(np.int64)
|
|
256
|
+
leaf_pos = _require(z, "prep_stretched_leaf_local_pos", (len(leaves_idx), 3), "f")
|
|
257
|
+
leaf_quat = _require(z, "prep_stretched_leaf_local_quat", (len(leaves_idx), 4), "f")
|
|
258
|
+
preserved = _require(z, "hierarchy_preserved", (J,)).astype(bool)
|
|
259
|
+
girdle = _require(z, "girdle_joints", (J,)).astype(bool)
|
|
260
|
+
|
|
261
|
+
if len(meta.get("boneNames", [])) != B:
|
|
262
|
+
raise RigError("bad-rig", f"meta boneNames has {len(meta.get('boneNames', []))} names for {B} bones")
|
|
263
|
+
if faces.size and (faces.min() < 0 or faces.max() >= V):
|
|
264
|
+
raise RigError("bad-rig", "face index out of range")
|
|
265
|
+
if skin_index.min() < 0 or skin_index.max() >= B:
|
|
266
|
+
raise RigError("bad-rig", "skin index out of range")
|
|
267
|
+
for i, p in enumerate(bone_parent):
|
|
268
|
+
if not -1 <= p < i:
|
|
269
|
+
raise RigError("bad-rig", f"bone {i} parent {p} does not precede it")
|
|
270
|
+
for j, p in enumerate(parents):
|
|
271
|
+
if not (p == -1 if j == 0 else 0 <= p < j):
|
|
272
|
+
raise RigError("bad-rig", f"cskel27 parent {p} of joint {j} is not topological")
|
|
273
|
+
for j in range(J):
|
|
274
|
+
if not -1 <= prep_bone[j] < B:
|
|
275
|
+
raise RigError("bad-rig", f"prep_bone[{j}] out of range")
|
|
276
|
+
cp = chain_parent[j]
|
|
277
|
+
if cp >= 0 and (cp >= j or prep_bone[cp] < 0 or prep_bone[j] < 0):
|
|
278
|
+
raise RigError("bad-rig", f"prep_chain_parent[{j}]={cp} must be an earlier mapped joint")
|
|
279
|
+
validate_skin_weights(skin_weight)
|
|
280
|
+
|
|
281
|
+
dv, df, dsi, dsw, source = decimate(vertices, faces, skin_index, skin_weight, max_faces)
|
|
282
|
+
dense = np.zeros((len(dv), B), dtype=np.float32)
|
|
283
|
+
np.add.at(dense, (np.repeat(np.arange(len(dv)), INFLUENCES), dsi.reshape(-1)), dsw.reshape(-1))
|
|
284
|
+
|
|
285
|
+
t = lambda a: torch.as_tensor(np.asarray(a, dtype=np.float32), device=device).to(dtype) # noqa: E731
|
|
286
|
+
rest_local = arrays["bone_rest_local"]
|
|
287
|
+
return Rig(
|
|
288
|
+
meta=meta,
|
|
289
|
+
parents=[int(p) for p in parents],
|
|
290
|
+
neutral=t(arrays["cskel27_neutral"]),
|
|
291
|
+
min_y=float(arrays["ardy_neutral_min_y"][0]),
|
|
292
|
+
root_scale=float(arrays["rig_root_scale"][0]),
|
|
293
|
+
bone_names=list(meta["boneNames"]),
|
|
294
|
+
bone_parent=[int(p) for p in bone_parent],
|
|
295
|
+
bone_rest_local=t(rest_local),
|
|
296
|
+
bone_rest_scale=t(np.linalg.norm(rest_local[:, :3, :3], axis=1)),
|
|
297
|
+
bind_inverse=t(arrays["bone_bind_inverse"]),
|
|
298
|
+
prep_bone=[int(b) for b in prep_bone],
|
|
299
|
+
prep_scale=float(arrays["prep_scale"][0]),
|
|
300
|
+
prep_offsets=t(arrays["prep_offsets"]),
|
|
301
|
+
prep_bind_pos=t(arrays["prep_bind_pos"]),
|
|
302
|
+
prep_bind_rot=quat_to_matrix(t(arrays["prep_bind_quat"])),
|
|
303
|
+
prep_bind_scale=t(arrays["prep_bind_scale"]),
|
|
304
|
+
prep_bind_local_pos=t(arrays["prep_bind_local_pos"]),
|
|
305
|
+
prep_parent_bind_world=t(arrays["prep_parent_bind_world"]),
|
|
306
|
+
prep_chain_parent=[int(c) for c in chain_parent],
|
|
307
|
+
prep_chain_rel=t(arrays["prep_chain_rel"]),
|
|
308
|
+
canonical_bone_length=np.asarray(arrays["prep_canonical_bone_length"], dtype=np.float64),
|
|
309
|
+
rig_bone_length=np.asarray(arrays["prep_rig_bone_length"], dtype=np.float64),
|
|
310
|
+
leaves=[(int(b), int(j), t(p), quat_to_matrix(t(q))) for (b, j), p, q in zip(leaves_idx, leaf_pos, leaf_quat)],
|
|
311
|
+
hierarchy_preserved=preserved,
|
|
312
|
+
girdle=girdle,
|
|
313
|
+
vertices=t(vertices),
|
|
314
|
+
skin_index=torch.as_tensor(skin_index, device=device),
|
|
315
|
+
skin_weight=t(skin_weight),
|
|
316
|
+
dec_vertices=t(dv),
|
|
317
|
+
dec_faces=torch.as_tensor(df, device=device),
|
|
318
|
+
dec_skin_index=torch.as_tensor(dsi, device=device),
|
|
319
|
+
dec_skin_weight=t(dsw),
|
|
320
|
+
dec_weights=t(dense),
|
|
321
|
+
dec_source=source,
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
# --- layer 1: cskel27 FK ---------------------------------------------------------
|
|
326
|
+
|
|
327
|
+
@dataclass
|
|
328
|
+
class State:
|
|
329
|
+
"""Optimisable per-frame pose (see module docstring for the convention)."""
|
|
330
|
+
|
|
331
|
+
transl: torch.Tensor # (T, 3)
|
|
332
|
+
root6d: torch.Tensor # (T, 6)
|
|
333
|
+
local6d: torch.Tensor # (T, 26, 6), joints 1..26
|
|
334
|
+
|
|
335
|
+
@staticmethod
|
|
336
|
+
def from_motion(local_rot_mats, root_positions, device="cpu", dtype=torch.float32) -> "State":
|
|
337
|
+
R = torch.as_tensor(np.asarray(local_rot_mats), device=device).to(dtype)
|
|
338
|
+
x = torch.as_tensor(np.asarray(root_positions), device=device).to(dtype)
|
|
339
|
+
if R.ndim != 4 or R.shape[1:] != (J, 3, 3) or x.shape != (R.shape[0], 3):
|
|
340
|
+
raise RigError("bad-motion", f"expected local_rot_mats (T,27,3,3) and root (T,3), got {tuple(R.shape)} {tuple(x.shape)}")
|
|
341
|
+
return State(x.clone(), matrix_to_rot6d(R[:, 0]), matrix_to_rot6d(R[:, 1:]))
|
|
342
|
+
|
|
343
|
+
@staticmethod
|
|
344
|
+
def identity(frames: int, root=(0.0, 0.0, 0.0), device="cpu", dtype=torch.float32) -> "State":
|
|
345
|
+
eye6 = torch.tensor([1.0, 0, 0, 0, 1, 0], device=device, dtype=dtype)
|
|
346
|
+
return State(torch.tensor(root, device=device, dtype=dtype).expand(frames, 3).clone(),
|
|
347
|
+
eye6.expand(frames, 6).clone(), eye6.expand(frames, J - 1, 6).clone())
|
|
348
|
+
|
|
349
|
+
def local_rot_mats(self) -> torch.Tensor:
|
|
350
|
+
"""(T, 27, 3, 3) in the NPZ local_rot_mats convention."""
|
|
351
|
+
return torch.cat((rot6d_to_matrix(self.root6d).unsqueeze(1), rot6d_to_matrix(self.local6d)), dim=1)
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def global_rotations(local: torch.Tensor, parents) -> torch.Tensor:
|
|
355
|
+
"""(T, 27, 3, 3) local -> global rotations down the cskel27 tree."""
|
|
356
|
+
out = [None] * J
|
|
357
|
+
for j in range(J):
|
|
358
|
+
p = parents[j]
|
|
359
|
+
out[j] = local[:, j] if p < 0 else out[p] @ local[:, j]
|
|
360
|
+
return torch.stack(out, dim=1)
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def cskel27_offsets(rig: Rig, bone_scale=None) -> torch.Tensor:
|
|
364
|
+
"""(27, 3) bone offsets in the parent frame: boneScale[j] * (neutral[j] - neutral[parent])."""
|
|
365
|
+
n = rig.neutral
|
|
366
|
+
parents = torch.tensor([max(p, 0) for p in rig.parents], device=n.device)
|
|
367
|
+
offsets = n - n[parents] # the root row is n[0] - n[0] = 0
|
|
368
|
+
if bone_scale is not None:
|
|
369
|
+
offsets = offsets * _bone_scale(bone_scale, n).unsqueeze(-1)
|
|
370
|
+
return offsets
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
def _bone_scale(bone_scale, like: torch.Tensor) -> torch.Tensor:
|
|
374
|
+
bs = torch.as_tensor(np.asarray(bone_scale) if not torch.is_tensor(bone_scale) else bone_scale, device=like.device).to(like.dtype)
|
|
375
|
+
if bs.shape != (J,) or not torch.isfinite(bs).all() or (bs <= 0).any():
|
|
376
|
+
raise RigError("bad-bone-scale", "bone_scale must be 27 positive finite factors")
|
|
377
|
+
return bs
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def cskel27_fk(state: State, bone_scale, rig: Rig):
|
|
381
|
+
"""Layer 1. Returns (posed_joints (T, 27, 3), globals (T, 27, 3, 3)).
|
|
382
|
+
|
|
383
|
+
posed_joints is the motion NPZ `posed_joints`: FK over the neutral skeleton
|
|
384
|
+
grown by `bone_scale` (None == all ones) with the root at `state.transl`.
|
|
385
|
+
"""
|
|
386
|
+
globals_ = global_rotations(state.local_rot_mats(), rig.parents)
|
|
387
|
+
offsets = cskel27_offsets(rig, bone_scale)
|
|
388
|
+
pos = [None] * J
|
|
389
|
+
for j in range(J):
|
|
390
|
+
p = rig.parents[j]
|
|
391
|
+
pos[j] = state.transl if p < 0 else pos[p] + (globals_[:, p] @ offsets[j].unsqueeze(-1)).squeeze(-1)
|
|
392
|
+
return torch.stack(pos, dim=1), globals_
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
# --- layer 2: Studio positional skinning + LBS ------------------------------------
|
|
396
|
+
|
|
397
|
+
def _studio_offsets(rig: Rig, bone_scale) -> torch.Tensor:
|
|
398
|
+
"""playback.js prep.offsets, or scaledOffsets(prep, boneScale) for a take with boneScale."""
|
|
399
|
+
if bone_scale is None:
|
|
400
|
+
return rig.prep_offsets
|
|
401
|
+
grown = cskel27_offsets(rig, bone_scale)
|
|
402
|
+
g = [None] * J
|
|
403
|
+
for j in range(J):
|
|
404
|
+
p = rig.parents[j]
|
|
405
|
+
g[j] = rig.neutral[j] if p < 0 else g[p] + grown[j]
|
|
406
|
+
g = torch.stack(g)
|
|
407
|
+
shift = torch.tensor([0.0, rig.min_y, 0.0], dtype=g.dtype, device=g.device)
|
|
408
|
+
return rig.prep_bind_pos - rig.prep_scale * (g - shift)
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
def _stretch(rig: Rig, bone_scale, j: int):
|
|
412
|
+
"""playback.js boneStretch: 1 without boneScale; girdle joints take the raw factor."""
|
|
413
|
+
if bone_scale is None:
|
|
414
|
+
return 1.0
|
|
415
|
+
if rig.girdle[j]:
|
|
416
|
+
return bone_scale[j]
|
|
417
|
+
canonical, rig_bind = rig.canonical_bone_length[j], rig.rig_bone_length[j]
|
|
418
|
+
if not (canonical > 1e-6) or not (rig_bind > 1e-6):
|
|
419
|
+
return bone_scale[j]
|
|
420
|
+
return bone_scale[j] * float(canonical / rig_bind)
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def studio_bone_worlds(globals_: torch.Tensor, posed_joints: torch.Tensor, rig: Rig,
|
|
424
|
+
bone_scale=None, anchor_xz=None) -> torch.Tensor:
|
|
425
|
+
"""Every exported bone's world matrix (T, B, 4, 4) in metres under Studio playback.
|
|
426
|
+
|
|
427
|
+
globals_: (T, 27, 3, 3) cskel27 global rotations; posed_joints: (T, 27, 3).
|
|
428
|
+
bone_scale: None reproduces a take without bone_scale (no arm stretch); a
|
|
429
|
+
27-vector reproduces a mocap take (grown offsets, stretched arm chain/hands).
|
|
430
|
+
anchor_xz: None (no anchoring) or (2,)/(T, 2) root XZ subtracted like the
|
|
431
|
+
anchor frame's root in applyMotionFrame.
|
|
432
|
+
"""
|
|
433
|
+
T = globals_.shape[0]
|
|
434
|
+
dtype, device = globals_.dtype, globals_.device
|
|
435
|
+
bs = None if bone_scale is None else _bone_scale(bone_scale, globals_)
|
|
436
|
+
offsets = _studio_offsets(rig, bs)
|
|
437
|
+
s = rig.prep_scale
|
|
438
|
+
posed = posed_joints
|
|
439
|
+
if anchor_xz is not None:
|
|
440
|
+
a = torch.as_tensor(anchor_xz, dtype=dtype, device=device).reshape(-1, 2)
|
|
441
|
+
zero = torch.zeros_like(a[:, :1])
|
|
442
|
+
posed = posed - torch.cat((a[:, :1], zero, a[:, 1:]), dim=-1).unsqueeze(1)
|
|
443
|
+
|
|
444
|
+
desired = [None] * J
|
|
445
|
+
assumed_parent = {}
|
|
446
|
+
for j in range(J):
|
|
447
|
+
b = rig.prep_bone[j]
|
|
448
|
+
if b < 0:
|
|
449
|
+
continue
|
|
450
|
+
R = globals_[:, j]
|
|
451
|
+
pos = s * posed[:, j] + (R @ offsets[j].unsqueeze(-1)).squeeze(-1)
|
|
452
|
+
cp = rig.prep_chain_parent[j]
|
|
453
|
+
parent = desired[cp] @ rig.prep_chain_rel[j] if cp >= 0 else rig.prep_parent_bind_world[j].expand(T, 4, 4)
|
|
454
|
+
if rig.hierarchy_preserved[j]:
|
|
455
|
+
pos = _apply(parent, (rig.prep_bind_local_pos[j] * _stretch(rig, bs, j)).expand(T, 3))
|
|
456
|
+
rot = R @ rig.prep_bind_rot[j] * rig.prep_bind_scale[j] # compose(pos, quat, scale)
|
|
457
|
+
desired[j] = _affine(rot, pos)
|
|
458
|
+
assumed_parent[b] = (parent, j)
|
|
459
|
+
|
|
460
|
+
leaves = {b: (joint, p, Rq) for b, joint, p, Rq in rig.leaves}
|
|
461
|
+
worlds = []
|
|
462
|
+
for b, parent_index in enumerate(rig.bone_parent):
|
|
463
|
+
if b in assumed_parent:
|
|
464
|
+
parent, j = assumed_parent[b]
|
|
465
|
+
local = torch.linalg.inv(parent) @ desired[j]
|
|
466
|
+
elif b in leaves:
|
|
467
|
+
joint, p, Rq = leaves[b]
|
|
468
|
+
local = _affine(Rq * rig.bone_rest_scale[b], p * _stretch(rig, bs, joint)).expand(T, 4, 4)
|
|
469
|
+
else:
|
|
470
|
+
local = rig.bone_rest_local[b].expand(T, 4, 4)
|
|
471
|
+
worlds.append(local if parent_index < 0 else worlds[parent_index] @ local)
|
|
472
|
+
W = torch.stack(worlds, dim=1)
|
|
473
|
+
root = torch.diag(torch.tensor([rig.root_scale] * 3 + [1.0], dtype=dtype, device=device))
|
|
474
|
+
return root @ W
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
def lbs(bone_worlds: torch.Tensor, rig: Rig, rest: torch.Tensor, weights: torch.Tensor) -> torch.Tensor:
|
|
478
|
+
"""Linear-blend skinning: v = sum_b w_vb * (W_b @ bindInverse_b) @ v_rest.
|
|
479
|
+
|
|
480
|
+
bone_worlds (T, B, 4, 4) metres; rest (V, 3); weights dense (V, B). -> (T, V, 3).
|
|
481
|
+
"""
|
|
482
|
+
skin = bone_worlds @ rig.bind_inverse # (T, B, 4, 4)
|
|
483
|
+
blended = torch.einsum("vb,tbij->tvij", weights, skin[:, :, :3, :]) # (T, V, 3, 4)
|
|
484
|
+
return (blended[..., :3] @ rest.unsqueeze(-1)).squeeze(-1) + blended[..., 3]
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
def dense_weights(rig: Rig, indices=None) -> tuple:
|
|
488
|
+
"""(rest (V, 3), dense weights (V, B)) of full-resolution vertices `indices` (all when None)."""
|
|
489
|
+
idx = slice(None) if indices is None else torch.as_tensor(indices, device=rig.vertices.device, dtype=torch.long)
|
|
490
|
+
si, sw = rig.skin_index[idx], rig.skin_weight[idx]
|
|
491
|
+
dense = torch.zeros(si.shape[0], len(rig.bone_names), dtype=sw.dtype, device=sw.device)
|
|
492
|
+
dense.scatter_add_(1, si, sw)
|
|
493
|
+
return rig.vertices[idx], dense
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
def studio_skin(globals_: torch.Tensor, posed_joints: torch.Tensor, rig: Rig, bone_scale=None,
|
|
497
|
+
anchor_xz=None, vertex_indices=None):
|
|
498
|
+
"""Layer 2. Returns (bone_worlds (T, B, 4, 4), vertices (T, V, 3)), metres.
|
|
499
|
+
|
|
500
|
+
Vertices are the decimated tracker mesh by default, or the full-resolution
|
|
501
|
+
export vertices `vertex_indices` (parity checks against Studio).
|
|
502
|
+
"""
|
|
503
|
+
W = studio_bone_worlds(globals_, posed_joints, rig, bone_scale, anchor_xz)
|
|
504
|
+
if vertex_indices is None:
|
|
505
|
+
rest, weights = rig.dec_vertices, rig.dec_weights
|
|
506
|
+
else:
|
|
507
|
+
rest, weights = dense_weights(rig, vertex_indices)
|
|
508
|
+
return W, lbs(W, rig, rest, weights)
|