cozyclay 1.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/LICENSE +674 -0
- package/README.md +109 -0
- package/THIRD_PARTY_NOTICES.md +29 -0
- package/bin/cozyclay.mjs +194 -0
- package/dist/ardy/cskel27-rest.json +557 -0
- package/dist/assets/basis_transcoder-VXdx5NbI.wasm +0 -0
- package/dist/assets/basis_transcoder-o4Hde_L7.js +19 -0
- package/dist/assets/draco_decoder-C32yEggz.wasm +0 -0
- package/dist/assets/draco_decoder-Z1_iN-Ht.wasm +0 -0
- package/dist/assets/draco_decoder-fzg4nYZr.js +34 -0
- package/dist/assets/draco_wasm_wrapper-DxJM36Ib.js +117 -0
- package/dist/assets/draco_wasm_wrapper-fZCQGLGb.js +116 -0
- package/dist/assets/index-BW_S1YRy.js +4395 -0
- package/dist/assets/index-nRmmJgap.css +1 -0
- package/dist/demo/walk-then-stop.npz +0 -0
- package/dist/fonts/instrument-serif-italic-latin.woff2 +0 -0
- package/dist/fonts/instrument-serif-latin.woff2 +0 -0
- package/dist/fonts/inter-latin.woff2 +0 -0
- package/dist/index.html +13 -0
- package/dist/models/x-bot-tpose.fbx +0 -0
- package/dist/models/y-bot-tpose.fbx +0 -0
- package/package.json +80 -0
- package/src/App.jsx +3840 -0
- package/src/ardy/client.js +140 -0
- package/src/ardy/convert.js +313 -0
- package/src/ardy/cskel27-neutral.js +39 -0
- package/src/ardy/cskel27.js +68 -0
- package/src/ardy/export.js +157 -0
- package/src/ardy/ik.js +610 -0
- package/src/ardy/npz.js +520 -0
- package/src/ardy/playback.js +414 -0
- package/src/ardy/prompt-clips.js +16 -0
- package/src/ardy/timeline-coordinates.js +17 -0
- package/src/ardy/timeline-resize.js +11 -0
- package/src/ardy/timeline.jsx +723 -0
- package/src/ardy/to-cskel27.js +205 -0
- package/src/ardy/waypoints.js +364 -0
- package/src/camera-follow.js +366 -0
- package/src/camera-move.js +286 -0
- package/src/controls.jsx +245 -0
- package/src/dualview.jsx +305 -0
- package/src/hierarchy-model.js +77 -0
- package/src/hierarchy-panel.jsx +365 -0
- package/src/history.js +90 -0
- package/src/main.jsx +10 -0
- package/src/object-catalog.jsx +90 -0
- package/src/object-gizmo.jsx +755 -0
- package/src/planview.jsx +653 -0
- package/src/poses.js +424 -0
- package/src/posestudio.jsx +811 -0
- package/src/props.jsx +302 -0
- package/src/room.jsx +64 -0
- package/src/scene-history.js +125 -0
- package/src/scene-objects.js +420 -0
- package/src/shot-authoring.js +113 -0
- package/src/shot.js +239 -0
- package/src/styles.css +5639 -0
- package/src/ui.jsx +391 -0
- package/src/use-render-activity.js +96 -0
- package/tools/ardy/BRIDGE.md +255 -0
- package/tools/ardy/README.md +136 -0
- package/tools/ardy/__pycache__/cclay_sequence_generate.cpython-313.pyc +0 -0
- package/tools/ardy/bridge.mjs +1427 -0
- package/tools/ardy/cclay_motion_edit.py +445 -0
- package/tools/ardy/cclay_sequence_generate.py +595 -0
- package/tools/ardy/dump-npz.py +205 -0
- package/tools/ardy/extract-rest.mjs +299 -0
- package/tools/ardy/npz.mjs +335 -0
- package/tools/ardy/out/gen-1786443326924-c6019e-generated.npz +0 -0
- package/tools/ardy/out/gen-1786443609325-0053db-generated.npz +0 -0
- package/tools/ardy/out/gen-1786443835628-749ed1-generated.npz +0 -0
- package/tools/ardy/out/gen-1786462605247-03cb19-generated.npz +0 -0
- package/tools/ardy/pose-to-npz.mjs +106 -0
- package/tools/ardy/run-edit-on-box.sh +73 -0
- package/tools/ardy/run-on-box.sh +568 -0
- package/tools/ardy/run-sequence-on-box.sh +162 -0
- package/tools/ardy/visual-qa.mjs +188 -0
- package/tools/ardy/vq-car.mjs +52 -0
- package/tools/dev-full.mjs +29 -0
- package/tools/process-supervisor.mjs +63 -0
- package/tools/qa-browser.mjs +84 -0
- package/tools/qa-crop3.mjs +22 -0
- package/tools/qa-playview.mjs +49 -0
- package/tools/qa-screenshot.mjs +24 -0
- package/tools/qa-visual.mjs +51 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Context-aware sparse motion editing for CozyClay.
|
|
3
|
+
|
|
4
|
+
The source motion stays authoritative. ARDY receives real pre-edit history,
|
|
5
|
+
sparse per-joint constraints inside the editable interval, the existing root
|
|
6
|
+
trajectory as a separate channel, and dense future context. Only the editable
|
|
7
|
+
interval is committed back into the full source motion.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
TRACK_JOINTS = {
|
|
16
|
+
"leftHand": "LeftHand",
|
|
17
|
+
"rightHand": "RightHand",
|
|
18
|
+
"leftFoot": "LeftFoot",
|
|
19
|
+
"rightFoot": "RightFoot",
|
|
20
|
+
"leftElbow": "LeftForeArm",
|
|
21
|
+
"rightElbow": "RightForeArm",
|
|
22
|
+
"leftKnee": "LeftLeg",
|
|
23
|
+
"rightKnee": "RightLeg",
|
|
24
|
+
"spine": "Spine",
|
|
25
|
+
"chest": "Spine1",
|
|
26
|
+
"neck": "Neck",
|
|
27
|
+
"head": "Head",
|
|
28
|
+
"leftShoulder": "LeftShoulder",
|
|
29
|
+
"rightShoulder": "RightShoulder",
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
TRACK_COMMIT_CHAINS = {
|
|
33
|
+
"leftHand": ["LeftArm", "LeftForeArm", "LeftHand"],
|
|
34
|
+
"rightHand": ["RightArm", "RightForeArm", "RightHand"],
|
|
35
|
+
"leftFoot": ["LeftUpLeg", "LeftLeg", "LeftFoot"],
|
|
36
|
+
"rightFoot": ["RightUpLeg", "RightLeg", "RightFoot"],
|
|
37
|
+
"leftElbow": ["LeftForeArm"],
|
|
38
|
+
"rightElbow": ["RightForeArm"],
|
|
39
|
+
"leftKnee": ["LeftLeg"],
|
|
40
|
+
"rightKnee": ["RightLeg"],
|
|
41
|
+
"hips": ["Hips"],
|
|
42
|
+
"spine": ["Spine"],
|
|
43
|
+
"chest": ["Spine1"],
|
|
44
|
+
"neck": ["Neck"],
|
|
45
|
+
"head": ["Head"],
|
|
46
|
+
"leftShoulder": ["LeftShoulder"],
|
|
47
|
+
"rightShoulder": ["RightShoulder"],
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def parse_args():
|
|
52
|
+
parser = argparse.ArgumentParser(description="Context-aware sparse ARDY motion edit")
|
|
53
|
+
parser.add_argument("--source", required=True)
|
|
54
|
+
parser.add_argument("--manifest", required=True)
|
|
55
|
+
parser.add_argument("--output", required=True)
|
|
56
|
+
parser.add_argument("--prompt", required=True)
|
|
57
|
+
parser.add_argument("--context-before", type=int, default=40)
|
|
58
|
+
parser.add_argument("--context-after", type=int, default=20)
|
|
59
|
+
parser.add_argument("--seed", type=int)
|
|
60
|
+
return parser.parse_args()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class SparseJointConstraint:
|
|
64
|
+
"""Constraint only the named joints; deliberately does not touch root."""
|
|
65
|
+
|
|
66
|
+
name = "cozyclay-sparse-joint"
|
|
67
|
+
|
|
68
|
+
def __init__(self, skeleton, frame_indices, positions, rotations, joint_names):
|
|
69
|
+
import torch
|
|
70
|
+
|
|
71
|
+
self.skeleton = skeleton
|
|
72
|
+
self.frame_indices = frame_indices
|
|
73
|
+
self.global_joints_positions = positions
|
|
74
|
+
self.global_joints_rots = rotations
|
|
75
|
+
self.joint_names = joint_names
|
|
76
|
+
self.rot_indices = torch.tensor(
|
|
77
|
+
[skeleton.bone_index[name] for name in joint_names],
|
|
78
|
+
)
|
|
79
|
+
self.pos_indices = torch.tensor(
|
|
80
|
+
sorted({skeleton.root_idx, *self.rot_indices.tolist()}),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
def update_constraints(self, data_dict, index_dict):
|
|
84
|
+
import torch
|
|
85
|
+
|
|
86
|
+
crop_frames = torch.arange(len(self.frame_indices))
|
|
87
|
+
real = torch.cartesian_prod(self.frame_indices, self.pos_indices)
|
|
88
|
+
crop = torch.cartesian_prod(crop_frames, self.pos_indices)
|
|
89
|
+
data_dict["global_joints_positions"].append(
|
|
90
|
+
self.global_joints_positions[crop[:, 0], crop[:, 1]]
|
|
91
|
+
)
|
|
92
|
+
index_dict["global_joints_positions"].append(real)
|
|
93
|
+
real = torch.cartesian_prod(self.frame_indices, self.rot_indices)
|
|
94
|
+
crop = torch.cartesian_prod(crop_frames, self.rot_indices)
|
|
95
|
+
data_dict["global_joints_rots"].append(
|
|
96
|
+
self.global_joints_rots[crop[:, 0], crop[:, 1]]
|
|
97
|
+
)
|
|
98
|
+
index_dict["global_joints_rots"].append(real)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class RootTrackConstraint:
|
|
102
|
+
"""Root X/Y/Z and heading are a separate channel from body constraints."""
|
|
103
|
+
|
|
104
|
+
name = "cozyclay-root-track"
|
|
105
|
+
|
|
106
|
+
def __init__(self, skeleton, frame_indices, positions, headings):
|
|
107
|
+
self.skeleton = skeleton
|
|
108
|
+
self.frame_indices = frame_indices
|
|
109
|
+
self.positions = positions
|
|
110
|
+
self.headings = headings
|
|
111
|
+
|
|
112
|
+
def update_constraints(self, data_dict, index_dict):
|
|
113
|
+
data_dict["root_2d"].append(self.positions[:, [0, 2]])
|
|
114
|
+
index_dict["root_2d"].append(self.frame_indices)
|
|
115
|
+
data_dict["root_y_pos"].append(self.positions[:, 1])
|
|
116
|
+
index_dict["root_y_pos"].append(self.frame_indices)
|
|
117
|
+
data_dict["global_root_heading"].append(self.headings)
|
|
118
|
+
index_dict["global_root_heading"].append(self.frame_indices)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def load_pose(path, skeleton, device):
|
|
122
|
+
import numpy as np
|
|
123
|
+
import torch
|
|
124
|
+
|
|
125
|
+
with np.load(path, allow_pickle=False) as data:
|
|
126
|
+
local = torch.from_numpy(np.asarray(data["local_rot_mats"])).float().to(device)
|
|
127
|
+
posed = torch.from_numpy(np.asarray(data["posed_joints"])).float().to(device)
|
|
128
|
+
root = posed[:1, skeleton.root_idx]
|
|
129
|
+
rotations, positions, _ = skeleton.fk(local[:1], root)
|
|
130
|
+
return positions[0], rotations[0], local[0]
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def heading_from_positions(positions, skeleton):
|
|
134
|
+
import torch
|
|
135
|
+
from ardy.motion_rep.tools import compute_heading_angle
|
|
136
|
+
|
|
137
|
+
angle = compute_heading_angle(positions[None], skeleton)[0]
|
|
138
|
+
return torch.stack([torch.cos(angle), torch.sin(angle)], dim=-1)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def main():
|
|
142
|
+
args = parse_args()
|
|
143
|
+
import numpy as np
|
|
144
|
+
import torch
|
|
145
|
+
from ardy.constraints import FullBodyConstraintSet
|
|
146
|
+
from ardy.geometry import axis_angle_to_matrix, matrix_to_axis_angle
|
|
147
|
+
from ardy.model import DEFAULT_MODEL, load_model
|
|
148
|
+
from ardy.model.loading import get_env_var
|
|
149
|
+
from ardy.model.registry import resolve_model_name
|
|
150
|
+
from ardy.motion_rep.tools import length_to_mask
|
|
151
|
+
from ardy.tools import seed_everything, to_numpy
|
|
152
|
+
|
|
153
|
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
154
|
+
checkpoints_dir = get_env_var("CHECKPOINTS_DIR", None)
|
|
155
|
+
resolved_model = resolve_model_name(DEFAULT_MODEL, checkpoints_dir=checkpoints_dir)
|
|
156
|
+
model = load_model(resolved_model, device=device, checkpoints_dir=checkpoints_dir)
|
|
157
|
+
model.eval()
|
|
158
|
+
skeleton = model.skeleton
|
|
159
|
+
fps = int(model.motion_rep.fps)
|
|
160
|
+
|
|
161
|
+
with open(args.manifest, "r", encoding="utf-8") as handle:
|
|
162
|
+
manifest = json.load(handle)
|
|
163
|
+
manifest_dir = os.path.dirname(os.path.abspath(args.manifest))
|
|
164
|
+
start = int(manifest["start_frame"])
|
|
165
|
+
end = int(manifest["end_frame"])
|
|
166
|
+
edits = manifest["edits"]
|
|
167
|
+
|
|
168
|
+
with np.load(args.source, allow_pickle=False) as data:
|
|
169
|
+
source_arrays = {key: np.asarray(data[key]).copy() for key in data.files}
|
|
170
|
+
source_local = np.asarray(source_arrays["local_rot_mats"], dtype=np.float32)
|
|
171
|
+
source_root = np.asarray(source_arrays["root_positions"], dtype=np.float32)
|
|
172
|
+
total_frames = source_local.shape[0]
|
|
173
|
+
if not 0 <= start < end <= total_frames:
|
|
174
|
+
raise ValueError(f"edit range {start}..{end} is outside source motion 0..{total_frames}")
|
|
175
|
+
|
|
176
|
+
history_start = max(0, start - args.context_before)
|
|
177
|
+
future_end = min(total_frames, end + args.context_after)
|
|
178
|
+
history_frames = start - history_start
|
|
179
|
+
edit_frames = end - start
|
|
180
|
+
generation_frames = future_end - start
|
|
181
|
+
model_frames = history_frames + generation_frames
|
|
182
|
+
|
|
183
|
+
local_tensor = torch.from_numpy(source_local).float().to(device)
|
|
184
|
+
root_tensor = torch.from_numpy(source_root).float().to(device)
|
|
185
|
+
history = None
|
|
186
|
+
if start > history_start:
|
|
187
|
+
history = model.motion_rep(
|
|
188
|
+
local_tensor[None, history_start:start],
|
|
189
|
+
root_tensor[None, history_start:start],
|
|
190
|
+
to_normalize=True,
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
gen_source_local = local_tensor[start:future_end]
|
|
194
|
+
gen_source_root = root_tensor[start:future_end]
|
|
195
|
+
source_global_rots, source_positions, _ = skeleton.fk(
|
|
196
|
+
gen_source_local,
|
|
197
|
+
gen_source_root,
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
constraints = []
|
|
201
|
+
exact_keys = []
|
|
202
|
+
root_positions = gen_source_root.clone()
|
|
203
|
+
root_headings = heading_from_positions(source_positions, skeleton).clone()
|
|
204
|
+
root_edit_anchors = []
|
|
205
|
+
for edit in edits:
|
|
206
|
+
local_frame = int(edit["frame"]) - start
|
|
207
|
+
if not 0 <= local_frame < edit_frames:
|
|
208
|
+
raise ValueError(f"edit frame {edit['frame']} is outside {start}..{end}")
|
|
209
|
+
pose_path = edit["pose_path"]
|
|
210
|
+
if not os.path.isabs(pose_path):
|
|
211
|
+
pose_path = os.path.join(manifest_dir, pose_path)
|
|
212
|
+
positions, rotations, local_rotations = load_pose(pose_path, skeleton, device)
|
|
213
|
+
# ARDY's global-position representation requires a pelvis observation
|
|
214
|
+
# alongside every sparse body point. Use the unchanged source pelvis;
|
|
215
|
+
# this is an anchor, not permission for a hand/foot edit to move root.
|
|
216
|
+
positions[skeleton.root_idx] = source_positions[local_frame, skeleton.root_idx]
|
|
217
|
+
body_joints = sorted({
|
|
218
|
+
TRACK_JOINTS[track]
|
|
219
|
+
for track in edit["tracks"]
|
|
220
|
+
if track in TRACK_JOINTS
|
|
221
|
+
})
|
|
222
|
+
if body_joints:
|
|
223
|
+
constraints.append(
|
|
224
|
+
SparseJointConstraint(
|
|
225
|
+
skeleton,
|
|
226
|
+
torch.tensor([history_frames + local_frame]),
|
|
227
|
+
positions[None],
|
|
228
|
+
rotations[None],
|
|
229
|
+
body_joints,
|
|
230
|
+
)
|
|
231
|
+
)
|
|
232
|
+
if "hips" in edit["tracks"]:
|
|
233
|
+
authored_root = torch.tensor(
|
|
234
|
+
edit.get("root", positions[skeleton.root_idx].tolist()),
|
|
235
|
+
device=device,
|
|
236
|
+
dtype=torch.float32,
|
|
237
|
+
)
|
|
238
|
+
root_edit_anchors.append(
|
|
239
|
+
(local_frame, authored_root, heading_from_positions(positions, skeleton))
|
|
240
|
+
)
|
|
241
|
+
exact_keys.append({
|
|
242
|
+
"frame": local_frame,
|
|
243
|
+
"tracks": edit["tracks"],
|
|
244
|
+
"local_rotations": local_rotations,
|
|
245
|
+
"root_position": torch.tensor(
|
|
246
|
+
edit.get("root", positions[skeleton.root_idx].tolist()),
|
|
247
|
+
device=device,
|
|
248
|
+
dtype=torch.float32,
|
|
249
|
+
),
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
# Root stays on the source trajectory unless the user explicitly edits
|
|
253
|
+
# hips. Hips edits become smooth offset anchors; body edits never move root.
|
|
254
|
+
if root_edit_anchors:
|
|
255
|
+
anchors = [(0, torch.zeros(3, device=device), torch.zeros(2, device=device))]
|
|
256
|
+
for frame, position, heading in root_edit_anchors:
|
|
257
|
+
anchors.append(
|
|
258
|
+
(
|
|
259
|
+
frame,
|
|
260
|
+
position - gen_source_root[frame],
|
|
261
|
+
heading - root_headings[frame],
|
|
262
|
+
)
|
|
263
|
+
)
|
|
264
|
+
anchors.append(
|
|
265
|
+
(
|
|
266
|
+
edit_frames - 1,
|
|
267
|
+
torch.zeros(3, device=device),
|
|
268
|
+
torch.zeros(2, device=device),
|
|
269
|
+
)
|
|
270
|
+
)
|
|
271
|
+
anchors.sort(key=lambda item: item[0])
|
|
272
|
+
for left, right in zip(anchors, anchors[1:]):
|
|
273
|
+
f0, p0, h0 = left
|
|
274
|
+
f1, p1, h1 = right
|
|
275
|
+
span = max(1, f1 - f0)
|
|
276
|
+
for frame in range(f0, f1 + 1):
|
|
277
|
+
alpha = (frame - f0) / span
|
|
278
|
+
root_positions[frame] += torch.lerp(p0, p1, alpha)
|
|
279
|
+
root_headings[frame] += torch.lerp(h0, h1, alpha)
|
|
280
|
+
|
|
281
|
+
all_generation_frames = torch.arange(history_frames, model_frames)
|
|
282
|
+
constraints.append(
|
|
283
|
+
RootTrackConstraint(
|
|
284
|
+
skeleton,
|
|
285
|
+
all_generation_frames,
|
|
286
|
+
root_positions,
|
|
287
|
+
root_headings,
|
|
288
|
+
)
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
# Future context is observed, not regenerated. It closes the edit against
|
|
292
|
+
# the following source motion while history conditions the left side.
|
|
293
|
+
if future_end > end:
|
|
294
|
+
future_indices = torch.arange(
|
|
295
|
+
history_frames + edit_frames,
|
|
296
|
+
model_frames,
|
|
297
|
+
)
|
|
298
|
+
constraints.append(
|
|
299
|
+
FullBodyConstraintSet(
|
|
300
|
+
skeleton,
|
|
301
|
+
future_indices,
|
|
302
|
+
source_positions[edit_frames:],
|
|
303
|
+
source_global_rots[edit_frames:],
|
|
304
|
+
)
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
lengths = torch.tensor([model_frames], device=device)
|
|
308
|
+
observed_motion, motion_mask = model.motion_rep.create_conditions_from_constraints_batched(
|
|
309
|
+
constraints,
|
|
310
|
+
lengths,
|
|
311
|
+
to_normalize=True,
|
|
312
|
+
device=device,
|
|
313
|
+
)
|
|
314
|
+
if args.seed is not None:
|
|
315
|
+
seed_everything(args.seed)
|
|
316
|
+
with torch.no_grad():
|
|
317
|
+
motion = model(
|
|
318
|
+
[args.prompt],
|
|
319
|
+
model_frames,
|
|
320
|
+
num_denoising_steps=int(model.diffusion.num_base_steps),
|
|
321
|
+
pad_mask=length_to_mask(lengths),
|
|
322
|
+
first_heading_angle=(
|
|
323
|
+
None
|
|
324
|
+
if history is not None
|
|
325
|
+
else torch.atan2(root_headings[:1, 1], root_headings[:1, 0])
|
|
326
|
+
),
|
|
327
|
+
motion_mask=motion_mask,
|
|
328
|
+
observed_motion=observed_motion,
|
|
329
|
+
cfg_weight=2.0,
|
|
330
|
+
progress_bar=lambda iterable: iterable,
|
|
331
|
+
init_history_sequence=history,
|
|
332
|
+
)
|
|
333
|
+
generated_motion = motion[:, history_frames:history_frames + generation_frames]
|
|
334
|
+
sampled = to_numpy(model.motion_rep.inverse(generated_motion, is_normalized=True))
|
|
335
|
+
|
|
336
|
+
has_root_edits = any("hips" in edit["tracks"] for edit in edits)
|
|
337
|
+
has_foot_edits = any(
|
|
338
|
+
track in {"leftFoot", "rightFoot", "leftKnee", "rightKnee"}
|
|
339
|
+
for edit in edits
|
|
340
|
+
for track in edit["tracks"]
|
|
341
|
+
)
|
|
342
|
+
sampled = {
|
|
343
|
+
key: (np.asarray(value)[0] if np.asarray(value).ndim > 0 and np.asarray(value).shape[0] == 1 else np.asarray(value))
|
|
344
|
+
for key, value in sampled.items()
|
|
345
|
+
}
|
|
346
|
+
# ARDY owns the in-between frames, but authored keys are exact animation
|
|
347
|
+
# frames rather than soft diffusion suggestions. Commit only the joints in
|
|
348
|
+
# each edited chain, and commit root position only for explicit hips edits.
|
|
349
|
+
committed_local = torch.from_numpy(sampled["local_rot_mats"]).float().to(device)
|
|
350
|
+
committed_root = torch.from_numpy(sampled["root_positions"]).float().to(device)
|
|
351
|
+
if has_root_edits:
|
|
352
|
+
committed_root[:edit_frames] = root_positions[:edit_frames]
|
|
353
|
+
for key in exact_keys:
|
|
354
|
+
frame = key["frame"]
|
|
355
|
+
joint_names = {
|
|
356
|
+
joint_name
|
|
357
|
+
for track in key["tracks"]
|
|
358
|
+
for joint_name in TRACK_COMMIT_CHAINS.get(track, [])
|
|
359
|
+
}
|
|
360
|
+
for joint_name in joint_names:
|
|
361
|
+
joint = skeleton.bone_index[joint_name]
|
|
362
|
+
generated_at_key = committed_local[frame, joint].clone()
|
|
363
|
+
correction = key["local_rotations"][joint] @ generated_at_key.transpose(-1, -2)
|
|
364
|
+
correction_axis_angle = matrix_to_axis_angle(correction)
|
|
365
|
+
radius = 6
|
|
366
|
+
for tween_frame in range(
|
|
367
|
+
max(0, frame - radius),
|
|
368
|
+
min(edit_frames, frame + radius + 1),
|
|
369
|
+
):
|
|
370
|
+
weight = 1.0 - abs(tween_frame - frame) / (radius + 1)
|
|
371
|
+
tween_correction = axis_angle_to_matrix(correction_axis_angle * weight)
|
|
372
|
+
committed_local[tween_frame, joint] = (
|
|
373
|
+
tween_correction @ committed_local[tween_frame, joint]
|
|
374
|
+
)
|
|
375
|
+
committed_local[frame, joint] = key["local_rotations"][joint]
|
|
376
|
+
if "hips" in key["tracks"]:
|
|
377
|
+
committed_root[frame] = key["root_position"]
|
|
378
|
+
committed_global_rots, committed_positions, _ = skeleton.fk(
|
|
379
|
+
committed_local,
|
|
380
|
+
committed_root,
|
|
381
|
+
)
|
|
382
|
+
commit_verified = True
|
|
383
|
+
for key in exact_keys:
|
|
384
|
+
frame = key["frame"]
|
|
385
|
+
for track in key["tracks"]:
|
|
386
|
+
for joint_name in TRACK_COMMIT_CHAINS.get(track, []):
|
|
387
|
+
joint = skeleton.bone_index[joint_name]
|
|
388
|
+
commit_verified = commit_verified and torch.allclose(
|
|
389
|
+
committed_local[frame, joint],
|
|
390
|
+
key["local_rotations"][joint],
|
|
391
|
+
atol=1e-6,
|
|
392
|
+
rtol=0,
|
|
393
|
+
)
|
|
394
|
+
if "hips" in key["tracks"]:
|
|
395
|
+
commit_verified = commit_verified and torch.allclose(
|
|
396
|
+
committed_root[frame],
|
|
397
|
+
key["root_position"],
|
|
398
|
+
atol=1e-6,
|
|
399
|
+
rtol=0,
|
|
400
|
+
)
|
|
401
|
+
if not commit_verified:
|
|
402
|
+
raise RuntimeError("authored IK key commit verification failed")
|
|
403
|
+
sampled["local_rot_mats"] = to_numpy(committed_local)
|
|
404
|
+
sampled["root_positions"] = to_numpy(committed_root)
|
|
405
|
+
sampled["posed_joints"] = to_numpy(committed_positions)
|
|
406
|
+
if "global_rot_mats" in sampled:
|
|
407
|
+
sampled["global_rot_mats"] = to_numpy(committed_global_rots)
|
|
408
|
+
|
|
409
|
+
if not has_root_edits and "root_positions" in sampled:
|
|
410
|
+
generated_root = sampled["root_positions"][:edit_frames].copy()
|
|
411
|
+
source_edit_root = source_root[start:end]
|
|
412
|
+
if "posed_joints" in sampled:
|
|
413
|
+
sampled["posed_joints"][:edit_frames] += (
|
|
414
|
+
source_edit_root - generated_root
|
|
415
|
+
)[:, None, :]
|
|
416
|
+
sampled["root_positions"][:edit_frames] = source_edit_root
|
|
417
|
+
|
|
418
|
+
for key, value in sampled.items():
|
|
419
|
+
if not has_foot_edits and "contact" in key:
|
|
420
|
+
continue
|
|
421
|
+
if key in source_arrays and value.ndim > 0 and source_arrays[key].shape[0] == total_frames:
|
|
422
|
+
source_arrays[key][start:end] = value[:edit_frames]
|
|
423
|
+
source_arrays["fps"] = np.asarray(fps)
|
|
424
|
+
source_arrays["text"] = np.asarray(args.prompt)
|
|
425
|
+
output = args.output if args.output.endswith(".npz") else f"{args.output}.npz"
|
|
426
|
+
os.makedirs(os.path.dirname(output) or ".", exist_ok=True)
|
|
427
|
+
np.savez(output, **source_arrays)
|
|
428
|
+
print(json.dumps({
|
|
429
|
+
"frames": total_frames,
|
|
430
|
+
"fps": fps,
|
|
431
|
+
"edit_range": [start, end],
|
|
432
|
+
"history_range": [history_start, start],
|
|
433
|
+
"future_range": [end, future_end],
|
|
434
|
+
"sparse_constraints": sum(
|
|
435
|
+
len([track for track in edit["tracks"] if track in TRACK_JOINTS])
|
|
436
|
+
for edit in edits
|
|
437
|
+
),
|
|
438
|
+
"root_edits": sum("hips" in edit["tracks"] for edit in edits),
|
|
439
|
+
"committed_keys": [start + key["frame"] for key in exact_keys],
|
|
440
|
+
"commit_verified": commit_verified,
|
|
441
|
+
}))
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
if __name__ == "__main__":
|
|
445
|
+
main()
|