Spaces:
Running
Running
File size: 15,035 Bytes
8f65673 f70c35d 8f65673 2f0834a 8f65673 f70c35d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | from pathlib import Path
import os
import subprocess
# The upstream checkout lives on persistent storage and therefore retains edits
# across container restarts. Restore only the files managed by this patcher from
# its pinned Git commit before applying changes, making this script idempotent.
for relative_path in (
"util/dataset_mixamo_additional.py",
"model.py",
"models_ae.py",
"app.py",
"app_blender.py",
"util/blender_utils.py",
):
pristine = subprocess.check_output(
["git", "-c", "safe.directory=/data/app", "-C", "app", "show", f"HEAD:{relative_path}"]
)
Path("app", relative_path).write_bytes(pristine)
# ββ 1. dataset_mixamo_additional.py β optional extended skeleton guard βββββββββ
path = Path("app/util/dataset_mixamo_additional.py")
if path.exists():
source = path.read_text()
old = """TEMPLATE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "../data/Mixamo/bones_vroid.fbx"))
KINEMATIC_TREE = get_kinematic_tree(TEMPLATE_PATH, BONES_IDX_DICT)
assert len(KINEMATIC_TREE) == len(MIXAMO_JOINTS)
"""
new = """TEMPLATE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "../data/Mixamo/bones_vroid.fbx"))
if os.path.isfile(TEMPLATE_PATH):
KINEMATIC_TREE = get_kinematic_tree(TEMPLATE_PATH, BONES_IDX_DICT)
assert len(KINEMATIC_TREE) == len(MIXAMO_JOINTS)
else:
# The optional extended skeleton lives in the restricted Mixamo dataset.
# Standard humanoid inference does not use it (joints_additional=False).
KINEMATIC_TREE = dataset_mixamo.KINEMATIC_TREE
"""
if old in source:
path.write_text(source.replace(old, new))
print("[dataset_mixamo_additional.py] extended-skeleton guard patched")
else:
print("[dataset_mixamo_additional.py] already patched or block not found")
# ββ fps fallback: replaces torch_cluster.fps with pytorch3d equivalent βββββββββ
FPS_FALLBACK = """try:
from torch_cluster import fps
except ImportError:
import numpy as np
from pytorch3d.ops import sample_farthest_points
def fps(x, batch, ratio):
import torch
sampled = []
for batch_id in torch.unique(batch, sorted=True):
global_idx = torch.nonzero(batch == batch_id, as_tuple=False).flatten()
points = x[global_idx].unsqueeze(0)
count = max(1, int(np.ceil(points.shape[1] * ratio)))
_, local_idx = sample_farthest_points(points, K=count, random_start_point=True)
sampled.append(global_idx[local_idx[0]])
return torch.cat(sampled)
"""
CLUSTER_IMPORT = "from torch_cluster import fps"
# ββ 2. model.py βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
model_path = Path("app/model.py")
if model_path.exists():
src = model_path.read_text()
if CLUSTER_IMPORT in src:
model_path.write_text(src.replace(CLUSTER_IMPORT, FPS_FALLBACK, 1))
print("[model.py] torch_cluster fps patched")
else:
print("[model.py] already patched")
else:
print("[model.py] not found")
# ββ 3. models_ae.py β THE CRITICAL MISSING PATCH βββββββββββββββββββββββββββββ
# The original patch_runtime.py only patched model.py but NOT models_ae.py,
# which also imports torch_cluster. This caused the ModuleNotFoundError.
models_ae_path = Path("app/models_ae.py")
if models_ae_path.exists():
src = models_ae_path.read_text()
if CLUSTER_IMPORT in src:
models_ae_path.write_text(src.replace(CLUSTER_IMPORT, FPS_FALLBACK, 1))
print("[models_ae.py] torch_cluster fps patched")
else:
print("[models_ae.py] already patched")
else:
print("[models_ae.py] not found")
# ββ 4. app/app.py β @spaces.GPU duration βββββββββββββββββββββββββββββββββββββ
app_path = Path("app/app.py")
if app_path.exists():
src = app_path.read_text()
bare_gpu_decorators = src.count("@spaces.GPU\n")
if bare_gpu_decorators != 2:
raise RuntimeError(f"Expected 2 bare GPU decorators, found {bare_gpu_decorators}")
app_path.write_text(src.replace("@spaces.GPU\n", "@spaces.GPU(duration=300)\n"))
print(f"[app/app.py] {bare_gpu_decorators} @spaces.GPU -> duration=300 patched")
else:
print("[app/app.py] not found")
print("patch_runtime.py done.")
# STATICAI Standard animation library integration
def _replace_once(path, old, new, label):
source = path.read_text()
count = source.count(old)
if count != 1:
raise RuntimeError(f"{label}: expected exactly one patch target, found {count}")
path.write_text(source.replace(old, new, 1))
blender_utils_path = Path("app/util/blender_utils.py")
_replace_once(
blender_utils_path,
"def load_mixamo_anim(char_file: str, anim_file: str, do_retarget=False, inplace=False, to_tris=False):",
"def load_mixamo_anim(char_file: str, anim_file: str, do_retarget=False, inplace=False, to_tris=False, action_name=None):",
"blender_utils signature",
)
_replace_once(
blender_utils_path,
' assert anim_armature.animation_data is not None and len(bpy.data.actions) > 0, f"Animation not found in {anim_file}"\n\n set_action(char_armature, anim_armature.animation_data.action)',
''' assert anim_armature.animation_data is not None and len(bpy.data.actions) > 0, f"Animation not found in {anim_file}"
if action_name:
candidates = [
action
for action in bpy.data.actions
if action.name == action_name
or action.name.endswith("|" + action_name)
or action.name.startswith(action_name + ".")
]
if not candidates:
available = ", ".join(sorted(action.name for action in bpy.data.actions))
raise ValueError(f"Animation action {action_name!r} not found. Available: {available}")
anim_armature.animation_data.action = candidates[0]
print(f"Selected animation action: {candidates[0].name}")
set_action(char_armature, anim_armature.animation_data.action)''',
"blender_utils action selection",
)
app_blender_path = Path("app/app_blender.py")
_replace_once(
app_blender_path,
''' blender_utils.load_mixamo_anim(
[armature_obj, mesh_obj], args.animation_path, do_retarget=args.retarget, inplace=args.inplace
)''',
''' blender_utils.load_mixamo_anim(
[armature_obj, mesh_obj],
args.animation_path,
do_retarget=args.retarget,
inplace=args.inplace,
action_name=args.animation_action,
)''',
"app_blender load_mixamo_anim",
)
_replace_once(
app_blender_path,
' parser.add_argument("--animation_path", type=str, default=None)\n',
' parser.add_argument("--animation_path", type=str, default=None)\n parser.add_argument("--animation_action", type=str, default=None)\n',
"app_blender parser action",
)
app_path = Path("app/app.py")
_replace_once(app_path, "import gc\nimport os\n", "import gc\nimport json\nimport os\n", "app json import")
_replace_once(
app_path,
''' animation_file: str,
retarget: bool,''',
''' animation_file: str,
animation_action: str,
retarget: bool,''',
"vis_blender action argument",
)
_replace_once(
app_path,
''' animation_path=animation_file,
retarget=retarget,''',
''' animation_path=animation_file,
animation_action=animation_action,
retarget=retarget,''',
"Namespace action argument",
)
_replace_once(
app_path,
''' if animation_file is not None:
cmd += f" --animation_path '{os.path.abspath(animation_file)}'"
if retarget:
cmd += " --retarget"
if inplace:
cmd += " --inplace"''',
''' if animation_file is not None:
cmd += f" --animation_path '{os.path.abspath(animation_file)}'"
if animation_action:
safe_action = animation_action.replace("'", "")
cmd += f" --animation_action '{safe_action}'"
if retarget:
cmd += " --retarget"
if inplace:
cmd += " --inplace"''',
"subprocess action argument",
)
_replace_once(
app_path,
''' animation_file: str = None,
retarget=True,''',
''' animation_file: str = None,
animation_action: str = None,
retarget=True,''',
"pipeline action argument",
)
_replace_once(
app_path,
''' yield vis_blender(
reset_to_rest, no_fingers, rest_pose_type, ignore_pose_parts, animation_file, retarget, inplace, db
)''',
''' yield vis_blender(
reset_to_rest,
no_fingers,
rest_pose_type,
ignore_pose_parts,
animation_file,
animation_action,
retarget,
inplace,
db,
)''',
"pipeline vis_blender action",
)
_replace_once(app_path, ' title = "Make-It-Animatable"\n', ' title = "Motion Rig"\n', "Motion Rig title")
_replace_once(
app_path,
' animation_dir = "data/Mixamo/animation"\n',
''' animation_dir = "data/Mixamo/animation"
standard_library_root = "/data/animations/Standard"
standard_manifest_path = os.path.join(standard_library_root, "ANIMATION_MANIFEST.json")
standard_animation_lookup = {}
standard_animation_choices = []
if os.path.isfile(standard_manifest_path):
with open(standard_manifest_path, "r", encoding="utf-8") as fh:
manifest = json.load(fh)
for entry_index, entry in enumerate(manifest.get("entries", [])):
motion_file = os.path.join(standard_library_root, entry["file"])
variant = "Root Motion" if entry.get("root_motion") else "In Place"
for action in entry.get("actions", []):
if not action or action == "A_TPose":
continue
key = f"{entry_index}:{action}"
label = f"Standard {entry.get('set')} | {action} | {variant}"
standard_animation_lookup[key] = {"path": motion_file, "action": action}
standard_animation_choices.append((label, key))
''',
"Standard library manifest load",
)
_replace_once(
app_path,
' sorted(glob("*.fbx", root_dir=animation_dir)),\n',
' standard_animation_choices,\n',
"Standard animation choices",
)
_replace_once(
app_path,
' label="Select Animation File",\n',
' label="Standard Animation Library",\n',
"Standard animation label",
)
_replace_once(
app_path,
''' input_animation_file = gr.File(
label="Animation File",
file_types=[".fbx"],
value=lambda: "./data/Standard Run.fbx",
interactive=True,
)''',
''' input_animation_file = gr.File(
label="Animation File",
file_types=[".fbx"],
value=lambda: "./data/Standard Run.fbx",
interactive=True,
)
animation_action_state = gr.State(value=None)''',
"animation action state",
)
_replace_once(
app_path,
''' input_animation_file,
input_retarget,''',
''' input_animation_file,
animation_action_state,
input_retarget,''',
"inputs tuple action state",
)
_replace_once(
app_path,
''' animation_file=inputs[input_animation_file],
retarget=inputs[input_retarget],''',
''' animation_file=inputs[input_animation_file],
animation_action=inputs[animation_action_state],
retarget=inputs[input_retarget],''',
"pipeline UI action",
)
_replace_once(
app_path,
''' input_animation_file,
input_retarget,''',
''' input_animation_file,
animation_action_state,
input_retarget,''',
"animate inputs action state",
)
_replace_once(
app_path,
''' def select2file(selected: str):
return None if selected is None else os.path.join(animation_dir, selected)
select_animation_file.input(
lambda x: (select2file(x), gr.Checkbox(value=True)),
inputs=select_animation_file,
outputs=[input_animation_file, input_reset_to_rest],
preprocess=False,
)
input_animation_file.upload(
lambda: (gr.Dropdown(value=None), gr.Checkbox(value=True)),
outputs=[select_animation_file, input_reset_to_rest],
)
input_animation_file.clear(
lambda: gr.Dropdown(value=None), outputs=select_animation_file, show_progress="hidden"
)''',
''' def select2file(selected: str):
if selected is None:
return None, None
item = standard_animation_lookup.get(selected)
if item is None:
raise gr.Error(f"Unknown Standard animation selection: {selected}")
return item["path"], item["action"]
def select_library_animation(selected: str):
path, action = select2file(selected)
return path, action, gr.Checkbox(value=True)
select_animation_file.input(
select_library_animation,
inputs=select_animation_file,
outputs=[input_animation_file, animation_action_state, input_reset_to_rest],
preprocess=False,
)
input_animation_file.upload(
lambda: (gr.Dropdown(value=None), None, gr.Checkbox(value=True)),
outputs=[select_animation_file, animation_action_state, input_reset_to_rest],
)
input_animation_file.clear(
lambda: (gr.Dropdown(value=None), None),
outputs=[select_animation_file, animation_action_state],
show_progress="hidden",
)''',
"Standard selection events",
)
print("STATICAI Standard animation library integration patched.")
|