Spaces:
Running
on
Zero
Running
on
Zero
Commit
Β·
1889706
1
Parent(s):
45481b3
Update .gitignore and refactor UniRigDemo for improved file handling and pipeline execution
Browse files- .gitignore +12 -1
- app.py +212 -382
.gitignore
CHANGED
|
@@ -171,4 +171,15 @@ cython_debug/
|
|
| 171 |
.ruff_cache/
|
| 172 |
|
| 173 |
# PyPI configuration file
|
| 174 |
-
.pypirc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
.ruff_cache/
|
| 172 |
|
| 173 |
# PyPI configuration file
|
| 174 |
+
.pypirc
|
| 175 |
+
|
| 176 |
+
# 3D model files
|
| 177 |
+
*.glb
|
| 178 |
+
*.fbx
|
| 179 |
+
*.obj
|
| 180 |
+
*.npz
|
| 181 |
+
|
| 182 |
+
# residual files
|
| 183 |
+
.gradio/
|
| 184 |
+
lightning_logs/
|
| 185 |
+
logs/
|
app.py
CHANGED
|
@@ -1,20 +1,23 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import tempfile
|
| 3 |
-
import os
|
| 4 |
import shutil
|
| 5 |
import subprocess
|
|
|
|
| 6 |
import traceback
|
| 7 |
from pathlib import Path
|
| 8 |
-
from typing import
|
|
|
|
|
|
|
|
|
|
| 9 |
import spaces
|
| 10 |
import torch
|
|
|
|
| 11 |
|
| 12 |
-
import subprocess
|
| 13 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 14 |
|
|
|
|
| 15 |
# Get the PyTorch and CUDA versions
|
| 16 |
torch_version = torch.__version__.split("+")[0] # Strips any "+cuXXX" suffix
|
| 17 |
cuda_version = torch.version.cuda
|
|
|
|
| 18 |
|
| 19 |
# Format CUDA version to match the URL convention (e.g., "cu118" for CUDA 11.8)
|
| 20 |
if cuda_version:
|
|
@@ -22,83 +25,31 @@ if cuda_version:
|
|
| 22 |
else:
|
| 23 |
cuda_version = "cpu" # Fallback in case CUDA is not available
|
| 24 |
|
| 25 |
-
spconv_version = f"-{cuda_version}" if cuda_version != "cpu" else ""
|
| 26 |
|
| 27 |
subprocess.run(f'pip install spconv{spconv_version}', shell=True)
|
| 28 |
subprocess.run(f'pip install torch_scatter torch_cluster -f https://data.pyg.org/whl/torch-{torch_version}+{cuda_version}.html --no-cache-dir', shell=True)
|
| 29 |
|
| 30 |
|
| 31 |
-
import trimesh
|
| 32 |
-
import yaml
|
| 33 |
-
|
| 34 |
class UniRigDemo:
|
| 35 |
"""Main class for the UniRig Gradio demo application."""
|
| 36 |
|
| 37 |
def __init__(self):
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
# Supported file formats
|
| 43 |
-
self.supported_formats = ['.obj', '.fbx', '.glb'
|
| 44 |
|
| 45 |
-
# Initialize models (will be loaded on demand)
|
| 46 |
-
self.skeleton_model = None
|
| 47 |
-
self.skin_model = None
|
| 48 |
-
|
| 49 |
-
def load_config(self, config_path: str) -> dict:
|
| 50 |
-
"""Load YAML configuration file."""
|
| 51 |
-
try:
|
| 52 |
-
with open(config_path, 'r') as f:
|
| 53 |
-
return yaml.safe_load(f)
|
| 54 |
-
except Exception as e:
|
| 55 |
-
raise RuntimeError(f"Failed to load config {config_path}: {str(e)}")
|
| 56 |
-
|
| 57 |
def validate_input_file(self, file_path: str) -> bool:
|
| 58 |
"""Validate if the input file format is supported."""
|
| 59 |
-
if not file_path or not
|
| 60 |
return False
|
| 61 |
|
| 62 |
file_ext = Path(file_path).suffix.lower()
|
| 63 |
return file_ext in self.supported_formats
|
| 64 |
|
| 65 |
-
def preprocess_model(self, input_file: str, output_dir: str) -> str:
|
| 66 |
-
"""
|
| 67 |
-
Preprocess the 3D model for inference.
|
| 68 |
-
This extracts mesh data and saves it as .npz format.
|
| 69 |
-
"""
|
| 70 |
-
try:
|
| 71 |
-
# Create extraction command
|
| 72 |
-
extract_cmd = [
|
| 73 |
-
'python', '-m', 'src.data.extract',
|
| 74 |
-
'--config', 'configs/data/quick_inference.yaml',
|
| 75 |
-
'--input', input_file,
|
| 76 |
-
'--output_dir', output_dir,
|
| 77 |
-
'--force_override', 'true',
|
| 78 |
-
'--faces_target_count', '50000'
|
| 79 |
-
]
|
| 80 |
-
|
| 81 |
-
# Run extraction
|
| 82 |
-
result = subprocess.run(
|
| 83 |
-
extract_cmd,
|
| 84 |
-
cwd=str(Path(__file__).parent),
|
| 85 |
-
capture_output=True,
|
| 86 |
-
text=True
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
if result.returncode != 0:
|
| 90 |
-
raise RuntimeError(f"Extraction failed: {result.stderr}")
|
| 91 |
-
|
| 92 |
-
# Find the generated .npz file
|
| 93 |
-
npz_files = list(Path(output_dir).glob("*.npz"))
|
| 94 |
-
if not npz_files:
|
| 95 |
-
raise RuntimeError("No .npz file generated during preprocessing")
|
| 96 |
-
|
| 97 |
-
return str(npz_files[0])
|
| 98 |
-
|
| 99 |
-
except Exception as e:
|
| 100 |
-
raise RuntimeError(f"Preprocessing failed: {str(e)}")
|
| 101 |
-
|
| 102 |
@spaces.GPU()
|
| 103 |
def generate_skeleton(self, input_file: str, seed: int = 12345) -> Tuple[str, str, str]:
|
| 104 |
"""
|
|
@@ -116,62 +67,28 @@ class UniRigDemo:
|
|
| 116 |
return "Error: Invalid or unsupported file format. Supported: " + ", ".join(self.supported_formats), "", ""
|
| 117 |
|
| 118 |
# Create working directory
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
shutil.copy2(input_file,
|
|
|
|
|
|
|
| 126 |
|
| 127 |
# Generate skeleton using Python (replaces bash script)
|
| 128 |
-
output_file =
|
| 129 |
|
| 130 |
-
self.run_skeleton_inference_python(
|
| 131 |
-
|
| 132 |
-
if not
|
| 133 |
return "Error: Skeleton file was not generated", "", ""
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
return "β
Skeleton generated successfully!", output_file, preview_info
|
| 139 |
|
| 140 |
-
|
| 141 |
-
def generate_skinning(self, skeleton_file: str) -> Tuple[str, str, str]:
|
| 142 |
-
"""
|
| 143 |
-
OPERATION 2: Generate skinning weights for the skeleton using Python functions.
|
| 144 |
-
|
| 145 |
-
Args:
|
| 146 |
-
skeleton_file: Path to the skeleton file (from skeleton generation step)
|
| 147 |
-
|
| 148 |
-
Returns:
|
| 149 |
-
Tuple of (status_message, output_file_path, preview_info)
|
| 150 |
-
"""
|
| 151 |
-
if not skeleton_file or not os.path.exists(skeleton_file):
|
| 152 |
-
return "Error: No skeleton file provided or file doesn't exist", "", ""
|
| 153 |
-
|
| 154 |
-
# Create output directory
|
| 155 |
-
work_dir = Path(skeleton_file).parent
|
| 156 |
-
output_file = os.path.join(work_dir, f"{Path(skeleton_file).stem}_skin.fbx")
|
| 157 |
-
|
| 158 |
-
# Run skinning generation using Python function
|
| 159 |
-
try:
|
| 160 |
-
self.run_skin_inference_python(skeleton_file, output_file)
|
| 161 |
-
except Exception as e:
|
| 162 |
-
error_msg = f"Error: Skinning generation failed: {str(e)}"
|
| 163 |
-
traceback.print_exc()
|
| 164 |
-
return error_msg, "", ""
|
| 165 |
-
|
| 166 |
-
if not os.path.exists(output_file):
|
| 167 |
-
return "Error: Skinning file was not generated", "", ""
|
| 168 |
-
|
| 169 |
-
# Generate preview information
|
| 170 |
-
preview_info = self.generate_model_preview(output_file)
|
| 171 |
-
|
| 172 |
-
return "β
Skinning weights generated successfully!", output_file, preview_info
|
| 173 |
-
|
| 174 |
-
def merge_results(self, original_file: str, rigged_file: str) -> Tuple[str, str, str]:
|
| 175 |
"""
|
| 176 |
OPERATION 3: Merge the rigged skeleton/skin with the original model using Python functions.
|
| 177 |
|
|
@@ -182,78 +99,37 @@ class UniRigDemo:
|
|
| 182 |
Returns:
|
| 183 |
Tuple of (status_message, output_file_path, preview_info)
|
| 184 |
"""
|
| 185 |
-
if not original_file or not
|
| 186 |
return "Error: Original file not provided or doesn't exist", "", ""
|
| 187 |
|
| 188 |
-
if not rigged_file or not
|
| 189 |
return "Error: Rigged file not provided or doesn't exist", "", ""
|
| 190 |
|
| 191 |
# Create output file
|
| 192 |
work_dir = Path(rigged_file).parent
|
| 193 |
-
output_file =
|
| 194 |
|
| 195 |
# Run merge using Python function
|
| 196 |
try:
|
| 197 |
-
self.merge_results_python(rigged_file, original_file, output_file)
|
| 198 |
except Exception as e:
|
| 199 |
error_msg = f"Error: Merge failed: {str(e)}"
|
| 200 |
traceback.print_exc()
|
| 201 |
return error_msg, "", ""
|
| 202 |
|
| 203 |
-
|
|
|
|
|
|
|
| 204 |
return "Error: Merged file was not generated", "", ""
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
def generate_model_preview(self, model_path: str) -> str:
|
| 212 |
-
"""
|
| 213 |
-
Generate preview information for a 3D model.
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
Returns:
|
| 219 |
-
HTML string with model information
|
| 220 |
-
"""
|
| 221 |
-
try:
|
| 222 |
-
if not os.path.exists(model_path):
|
| 223 |
-
return "Model file not found"
|
| 224 |
-
|
| 225 |
-
# Try to load with trimesh for basic info
|
| 226 |
-
try:
|
| 227 |
-
mesh = trimesh.load(model_path)
|
| 228 |
-
if hasattr(mesh, 'vertices'):
|
| 229 |
-
vertices_count = len(mesh.vertices)
|
| 230 |
-
faces_count = len(mesh.faces) if hasattr(mesh, 'faces') else 0
|
| 231 |
-
else:
|
| 232 |
-
vertices_count = 0
|
| 233 |
-
faces_count = 0
|
| 234 |
-
except Exception:
|
| 235 |
-
vertices_count = 0
|
| 236 |
-
faces_count = 0
|
| 237 |
-
|
| 238 |
-
file_size = os.path.getsize(model_path)
|
| 239 |
-
file_size_mb = file_size / (1024 * 1024)
|
| 240 |
-
|
| 241 |
-
preview_html = f"""
|
| 242 |
-
<div style="padding: 10px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9;">
|
| 243 |
-
<h4>π Model Information</h4>
|
| 244 |
-
<p><strong>File:</strong> {Path(model_path).name}</p>
|
| 245 |
-
<p><strong>Size:</strong> {file_size_mb:.2f} MB</p>
|
| 246 |
-
<p><strong>Vertices:</strong> {vertices_count:,}</p>
|
| 247 |
-
<p><strong>Faces:</strong> {faces_count:,}</p>
|
| 248 |
-
<p><strong>Format:</strong> {Path(model_path).suffix.upper()}</p>
|
| 249 |
-
</div>
|
| 250 |
-
"""
|
| 251 |
-
|
| 252 |
-
return preview_html
|
| 253 |
-
|
| 254 |
-
except Exception as e:
|
| 255 |
-
return f"Error generating preview: {str(e)}"
|
| 256 |
-
|
| 257 |
def complete_pipeline(self, input_file: str, seed: int = 12345) -> Tuple[str, str, str, str, str]:
|
| 258 |
"""
|
| 259 |
Run the complete rigging pipeline: skeleton generation β skinning β merge.
|
|
@@ -265,100 +141,114 @@ class UniRigDemo:
|
|
| 265 |
Returns:
|
| 266 |
Tuple of status messages and file paths for each step
|
| 267 |
"""
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
if not skin_file:
|
| 277 |
-
return f"{skeleton_status}\n{skin_status}", skeleton_file, "", "", ""
|
| 278 |
-
|
| 279 |
-
# Step 3: Merge results
|
| 280 |
-
merge_status, final_file, final_preview = self.merge_results(input_file, skin_file)
|
| 281 |
-
|
| 282 |
-
# Combine all status messages
|
| 283 |
-
combined_status = f"""
|
| 284 |
-
ποΈ **Pipeline Complete!**
|
| 285 |
-
|
| 286 |
-
**Step 1 - Skeleton Generation:** β
Complete
|
| 287 |
-
**Step 2 - Skinning Weights:** β
Complete
|
| 288 |
-
**Step 3 - Final Merge:** β
Complete
|
| 289 |
-
|
| 290 |
-
{merge_status}
|
| 291 |
-
"""
|
| 292 |
-
|
| 293 |
-
return combined_status, skeleton_file, skin_file, final_file, final_preview
|
| 294 |
-
|
| 295 |
-
except Exception as e:
|
| 296 |
-
error_msg = f"Pipeline Error: {str(e)}"
|
| 297 |
-
traceback.print_exc()
|
| 298 |
-
return error_msg, "", "", "", ""
|
| 299 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
def extract_mesh_python(self, input_file: str, output_dir: str) -> str:
|
| 306 |
"""
|
| 307 |
Extract mesh data from 3D model using Python (replaces extract.sh)
|
| 308 |
Returns path to generated .npz file
|
| 309 |
"""
|
| 310 |
# Import required modules
|
| 311 |
-
from src.data.extract import get_files
|
| 312 |
|
| 313 |
# Create extraction parameters
|
| 314 |
files = get_files(
|
| 315 |
data_name="raw_data.npz",
|
| 316 |
-
inputs=input_file,
|
| 317 |
input_dataset_dir=None,
|
| 318 |
output_dataset_dir=output_dir,
|
| 319 |
force_override=True,
|
| 320 |
warning=False,
|
| 321 |
)
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
|
| 327 |
-
|
|
|
|
|
|
|
|
|
|
| 328 |
|
| 329 |
def run_skeleton_inference_python(self, input_file: str, output_file: str, seed: int = 12345) -> str:
|
| 330 |
"""
|
| 331 |
Run skeleton inference using Python (replaces skeleton part of generate_skeleton.sh)
|
| 332 |
Returns path to skeleton FBX file
|
| 333 |
"""
|
| 334 |
-
import lightning as L
|
| 335 |
from box import Box
|
| 336 |
-
|
| 337 |
from src.data.datapath import Datapath
|
|
|
|
| 338 |
from src.data.transform import TransformConfig
|
| 339 |
-
from src.
|
| 340 |
-
from src.tokenizer.parse import get_tokenizer
|
| 341 |
from src.model.parse import get_model
|
| 342 |
from src.system.parse import get_system, get_writer
|
| 343 |
-
from src.
|
|
|
|
| 344 |
|
| 345 |
# Set random seed
|
| 346 |
L.seed_everything(seed, workers=True)
|
| 347 |
|
| 348 |
# Load task configuration
|
| 349 |
task_config_path = "configs/task/quick_inference_skeleton_articulationxl_ar_256.yaml"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
with open(task_config_path, 'r') as f:
|
| 351 |
task = Box(yaml.safe_load(f))
|
| 352 |
|
| 353 |
# Create temporary npz directory
|
| 354 |
-
npz_dir =
|
| 355 |
-
|
| 356 |
|
| 357 |
# Extract mesh data
|
| 358 |
-
|
| 359 |
|
| 360 |
-
# Setup datapath
|
| 361 |
-
datapath = Datapath(files=[
|
| 362 |
|
| 363 |
# Load configurations
|
| 364 |
data_config = Box(yaml.safe_load(open("configs/data/quick_inference.yaml", 'r')))
|
|
@@ -391,10 +281,12 @@ class UniRigDemo:
|
|
| 391 |
# Setup callbacks and writer
|
| 392 |
callbacks = []
|
| 393 |
writer_config = task.writer.copy()
|
| 394 |
-
writer_config['npz_dir'] = npz_dir
|
| 395 |
-
writer_config['output_dir'] =
|
| 396 |
-
writer_config['output_name'] = output_file
|
| 397 |
-
writer_config['user_mode'] =
|
|
|
|
|
|
|
| 398 |
callbacks.append(get_writer(**writer_config, order_config=predict_transform_config.order_config))
|
| 399 |
|
| 400 |
# Get system
|
|
@@ -410,36 +302,58 @@ class UniRigDemo:
|
|
| 410 |
# Run prediction
|
| 411 |
trainer.predict(system, datamodule=data, ckpt_path=resume_from_checkpoint, return_predictions=False)
|
| 412 |
|
| 413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
|
| 415 |
def run_skin_inference_python(self, skeleton_file: str, output_file: str) -> str:
|
| 416 |
"""
|
| 417 |
Run skin inference using Python (replaces skin part of generate_skin.sh)
|
| 418 |
Returns path to skin FBX file
|
| 419 |
"""
|
| 420 |
-
import lightning as L
|
| 421 |
from box import Box
|
| 422 |
-
|
| 423 |
from src.data.datapath import Datapath
|
|
|
|
| 424 |
from src.data.transform import TransformConfig
|
|
|
|
| 425 |
from src.model.parse import get_model
|
| 426 |
from src.system.parse import get_system, get_writer
|
| 427 |
-
from src.inference.download import download
|
| 428 |
|
| 429 |
# Load task configuration
|
| 430 |
task_config_path = "configs/task/quick_inference_unirig_skin.yaml"
|
| 431 |
with open(task_config_path, 'r') as f:
|
| 432 |
task = Box(yaml.safe_load(f))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
|
| 434 |
-
#
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
if not os.path.exists(skeleton_npz):
|
| 439 |
-
raise RuntimeError(f"Skeleton NPZ file not found: {skeleton_npz}")
|
| 440 |
-
|
| 441 |
-
# Setup datapath
|
| 442 |
-
datapath = Datapath(files=[skeleton_npz], cls=None)
|
| 443 |
|
| 444 |
# Load configurations
|
| 445 |
data_config = Box(yaml.safe_load(open("configs/data/quick_inference.yaml", 'r')))
|
|
@@ -468,10 +382,10 @@ class UniRigDemo:
|
|
| 468 |
# Setup callbacks and writer
|
| 469 |
callbacks = []
|
| 470 |
writer_config = task.writer.copy()
|
| 471 |
-
writer_config['npz_dir'] =
|
| 472 |
-
writer_config['
|
| 473 |
-
writer_config['output_name'] = output_file
|
| 474 |
writer_config['user_mode'] = True
|
|
|
|
| 475 |
callbacks.append(get_writer(**writer_config, order_config=predict_transform_config.order_config))
|
| 476 |
|
| 477 |
# Get system
|
|
@@ -487,7 +401,19 @@ class UniRigDemo:
|
|
| 487 |
# Run prediction
|
| 488 |
trainer.predict(system, datamodule=data, ckpt_path=resume_from_checkpoint, return_predictions=False)
|
| 489 |
|
| 490 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
|
| 492 |
def merge_results_python(self, source_file: str, target_file: str, output_file: str) -> str:
|
| 493 |
"""
|
|
@@ -496,10 +422,27 @@ class UniRigDemo:
|
|
| 496 |
"""
|
| 497 |
from src.inference.merge import transfer
|
| 498 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
# Use the transfer function directly
|
| 500 |
-
transfer(source=source_file, target=target_file, output=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
|
| 502 |
-
|
|
|
|
|
|
|
|
|
|
| 503 |
|
| 504 |
|
| 505 |
def create_app():
|
|
@@ -528,148 +471,38 @@ def create_app():
|
|
| 528 |
<li><strong>Skinning Weights:</strong> Automatic vertex-to-bone weight assignment</li>
|
| 529 |
<li><strong>Universal Support:</strong> Works with humans, animals, and objects</li>
|
| 530 |
</ul>
|
| 531 |
-
<p><strong>Supported formats:</strong> .obj, .fbx, .glb
|
| 532 |
""")
|
| 533 |
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
with gr.Column(scale=1):
|
| 541 |
-
pipeline_input = gr.File(
|
| 542 |
-
label="Upload 3D Model",
|
| 543 |
-
file_types=[".obj", ".fbx", ".glb", ".gltf", ".vrm"],
|
| 544 |
-
type="filepath",
|
| 545 |
-
)
|
| 546 |
-
pipeline_seed = gr.Slider(
|
| 547 |
-
minimum=1,
|
| 548 |
-
maximum=99999,
|
| 549 |
-
value=12345,
|
| 550 |
-
step=1,
|
| 551 |
-
label="Random Seed (for reproducible results)"
|
| 552 |
-
)
|
| 553 |
-
pipeline_btn = gr.Button("π― Start Complete Pipeline", variant="primary", size="lg")
|
| 554 |
-
|
| 555 |
-
with gr.Column(scale=1):
|
| 556 |
-
pipeline_status = gr.Markdown("Ready to process your 3D model...")
|
| 557 |
-
pipeline_preview = gr.HTML("")
|
| 558 |
-
|
| 559 |
-
with gr.Row():
|
| 560 |
-
with gr.Column():
|
| 561 |
-
gr.HTML("<h4>π₯ Download Results</h4>")
|
| 562 |
-
pipeline_skeleton_out = gr.File(label="Skeleton (.fbx)", visible=False)
|
| 563 |
-
pipeline_skin_out = gr.File(label="Skinning Weights (.fbx)", visible=False)
|
| 564 |
-
pipeline_final_out = gr.File(label="Final Rigged Model (.glb)", visible=False)
|
| 565 |
-
|
| 566 |
-
pipeline_btn.click(
|
| 567 |
-
fn=demo_instance.complete_pipeline,
|
| 568 |
-
inputs=[pipeline_input, pipeline_seed],
|
| 569 |
-
outputs=[pipeline_status, pipeline_skeleton_out, pipeline_skin_out,
|
| 570 |
-
pipeline_final_out, pipeline_preview]
|
| 571 |
)
|
| 572 |
-
|
| 573 |
-
# Step-by-Step Tab
|
| 574 |
-
with gr.Tab("π§ Step-by-Step Process", elem_id="stepwise-tab"):
|
| 575 |
-
gr.HTML("<h3>Manual Step-by-Step Rigging Process</h3>")
|
| 576 |
-
gr.HTML("<p>Process your model step by step with full control over each stage.</p>")
|
| 577 |
-
|
| 578 |
-
# Step 1: Skeleton Generation
|
| 579 |
-
with gr.Group():
|
| 580 |
-
gr.HTML("<h4>Step 1: Skeleton Generation</h4>")
|
| 581 |
-
with gr.Row():
|
| 582 |
-
with gr.Column():
|
| 583 |
-
step1_input = gr.File(
|
| 584 |
-
label="Upload 3D Model",
|
| 585 |
-
file_types=[".obj", ".fbx", ".glb", ".gltf", ".vrm"],
|
| 586 |
-
type="filepath"
|
| 587 |
-
)
|
| 588 |
-
step1_seed = gr.Slider(
|
| 589 |
-
minimum=1,
|
| 590 |
-
maximum=99999,
|
| 591 |
-
value=12345,
|
| 592 |
-
step=1,
|
| 593 |
-
label="Random Seed"
|
| 594 |
-
)
|
| 595 |
-
step1_btn = gr.Button("Generate Skeleton", variant="secondary")
|
| 596 |
-
|
| 597 |
-
with gr.Column():
|
| 598 |
-
step1_status = gr.Markdown("Upload a model to start...")
|
| 599 |
-
step1_preview = gr.HTML("")
|
| 600 |
-
step1_output = gr.File(label="Skeleton File (.fbx)", visible=False)
|
| 601 |
-
|
| 602 |
-
# Step 2: Skinning Generation
|
| 603 |
-
with gr.Group():
|
| 604 |
-
gr.HTML("<h4>Step 2: Skinning Weight Generation</h4>")
|
| 605 |
-
with gr.Row():
|
| 606 |
-
with gr.Column():
|
| 607 |
-
step2_input = gr.File(
|
| 608 |
-
label="Skeleton File (from Step 1)",
|
| 609 |
-
file_types=[".fbx"],
|
| 610 |
-
type="filepath"
|
| 611 |
-
)
|
| 612 |
-
step2_btn = gr.Button("Generate Skinning Weights", variant="secondary")
|
| 613 |
-
|
| 614 |
-
with gr.Column():
|
| 615 |
-
step2_status = gr.Markdown("Complete Step 1 first...")
|
| 616 |
-
step2_preview = gr.HTML("")
|
| 617 |
-
step2_output = gr.File(label="Skinning File (.fbx)", visible=False)
|
| 618 |
-
|
| 619 |
-
# Step 3: Merge Results
|
| 620 |
-
with gr.Group():
|
| 621 |
-
gr.HTML("<h4>Step 3: Merge with Original Model</h4>")
|
| 622 |
-
with gr.Row():
|
| 623 |
-
with gr.Column():
|
| 624 |
-
step3_original = gr.File(
|
| 625 |
-
label="Original Model",
|
| 626 |
-
file_types=[".obj", ".fbx", ".glb", ".gltf", ".vrm"],
|
| 627 |
-
type="filepath"
|
| 628 |
-
)
|
| 629 |
-
step3_rigged = gr.File(
|
| 630 |
-
label="Rigged File (from Step 2)",
|
| 631 |
-
file_types=[".fbx"],
|
| 632 |
-
type="filepath"
|
| 633 |
-
)
|
| 634 |
-
step3_btn = gr.Button("Merge Results", variant="secondary")
|
| 635 |
-
|
| 636 |
-
with gr.Column():
|
| 637 |
-
step3_status = gr.Markdown("Complete previous steps first...")
|
| 638 |
-
step3_preview = gr.HTML("")
|
| 639 |
-
step3_output = gr.File(label="Final Rigged Model (.glb)", visible=False)
|
| 640 |
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
fn=lambda x: x,
|
| 663 |
-
inputs=[step1_output],
|
| 664 |
-
outputs=[step2_input]
|
| 665 |
-
)
|
| 666 |
-
|
| 667 |
-
# Auto-populate step 3 rigged input when step 2 completes
|
| 668 |
-
step2_output.change(
|
| 669 |
-
fn=lambda x: x,
|
| 670 |
-
inputs=[step2_output],
|
| 671 |
-
outputs=[step3_rigged]
|
| 672 |
-
)
|
| 673 |
|
| 674 |
# Footer
|
| 675 |
gr.HTML("""
|
|
@@ -680,9 +513,6 @@ def create_app():
|
|
| 680 |
π <a href="https://zjp-shadow.github.io/works/UniRig/" target="_blank">Project Page</a> |
|
| 681 |
π€ <a href="https://huggingface.co/VAST-AI/UniRig" target="_blank">Models</a>
|
| 682 |
</p>
|
| 683 |
-
<p style="color: #9ca3af; font-size: 0.9em;">
|
| 684 |
-
β‘ Powered by PyTorch & Gradio | π― GPU recommended for optimal performance
|
| 685 |
-
</p>
|
| 686 |
</div>
|
| 687 |
""")
|
| 688 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import shutil
|
| 2 |
import subprocess
|
| 3 |
+
import time
|
| 4 |
import traceback
|
| 5 |
from pathlib import Path
|
| 6 |
+
from typing import Tuple
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import lightning as L
|
| 10 |
import spaces
|
| 11 |
import torch
|
| 12 |
+
import yaml
|
| 13 |
|
|
|
|
| 14 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 15 |
|
| 16 |
+
|
| 17 |
# Get the PyTorch and CUDA versions
|
| 18 |
torch_version = torch.__version__.split("+")[0] # Strips any "+cuXXX" suffix
|
| 19 |
cuda_version = torch.version.cuda
|
| 20 |
+
spconv_version = "-cu121" if cuda_version else ""
|
| 21 |
|
| 22 |
# Format CUDA version to match the URL convention (e.g., "cu118" for CUDA 11.8)
|
| 23 |
if cuda_version:
|
|
|
|
| 25 |
else:
|
| 26 |
cuda_version = "cpu" # Fallback in case CUDA is not available
|
| 27 |
|
|
|
|
| 28 |
|
| 29 |
subprocess.run(f'pip install spconv{spconv_version}', shell=True)
|
| 30 |
subprocess.run(f'pip install torch_scatter torch_cluster -f https://data.pyg.org/whl/torch-{torch_version}+{cuda_version}.html --no-cache-dir', shell=True)
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
class UniRigDemo:
|
| 34 |
"""Main class for the UniRig Gradio demo application."""
|
| 35 |
|
| 36 |
def __init__(self):
|
| 37 |
+
# Create temp directory in current directory instead of system temp
|
| 38 |
+
base_dir = Path(__file__).parent
|
| 39 |
+
self.temp_dir = base_dir / "tmp"
|
| 40 |
+
self.temp_dir.mkdir(exist_ok=True)
|
| 41 |
|
| 42 |
# Supported file formats
|
| 43 |
+
self.supported_formats = ['.obj', '.fbx', '.glb']
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def validate_input_file(self, file_path: str) -> bool:
|
| 46 |
"""Validate if the input file format is supported."""
|
| 47 |
+
if not file_path or not Path(file_path).exists():
|
| 48 |
return False
|
| 49 |
|
| 50 |
file_ext = Path(file_path).suffix.lower()
|
| 51 |
return file_ext in self.supported_formats
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
@spaces.GPU()
|
| 54 |
def generate_skeleton(self, input_file: str, seed: int = 12345) -> Tuple[str, str, str]:
|
| 55 |
"""
|
|
|
|
| 67 |
return "Error: Invalid or unsupported file format. Supported: " + ", ".join(self.supported_formats), "", ""
|
| 68 |
|
| 69 |
# Create working directory
|
| 70 |
+
file_stem = Path(input_file).stem
|
| 71 |
+
input_model_dir = self.temp_dir / f"{file_stem}_{seed}"
|
| 72 |
+
input_model_dir.mkdir(exist_ok=True)
|
| 73 |
+
|
| 74 |
+
# Copy input file to working directory
|
| 75 |
+
input_file = Path(input_file)
|
| 76 |
+
shutil.copy2(input_file, input_model_dir / input_file.name)
|
| 77 |
+
input_file = input_model_dir / input_file.name
|
| 78 |
+
print(f"New input file path: {input_file}")
|
| 79 |
|
| 80 |
# Generate skeleton using Python (replaces bash script)
|
| 81 |
+
output_file = input_model_dir / f"{file_stem}_skeleton.fbx"
|
| 82 |
|
| 83 |
+
self.run_skeleton_inference_python(input_file, output_file, seed)
|
| 84 |
+
|
| 85 |
+
if not output_file.exists():
|
| 86 |
return "Error: Skeleton file was not generated", "", ""
|
| 87 |
|
| 88 |
+
print(f"Generated skeleton at: {output_file}")
|
| 89 |
+
return str(output_file)
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
def merge_results(self, original_file: str, rigged_file: str, output_file) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
"""
|
| 93 |
OPERATION 3: Merge the rigged skeleton/skin with the original model using Python functions.
|
| 94 |
|
|
|
|
| 99 |
Returns:
|
| 100 |
Tuple of (status_message, output_file_path, preview_info)
|
| 101 |
"""
|
| 102 |
+
if not original_file or not Path(original_file).exists():
|
| 103 |
return "Error: Original file not provided or doesn't exist", "", ""
|
| 104 |
|
| 105 |
+
if not rigged_file or not Path(rigged_file).exists():
|
| 106 |
return "Error: Rigged file not provided or doesn't exist", "", ""
|
| 107 |
|
| 108 |
# Create output file
|
| 109 |
work_dir = Path(rigged_file).parent
|
| 110 |
+
output_file = work_dir / f"{Path(original_file).stem}_rigged.glb"
|
| 111 |
|
| 112 |
# Run merge using Python function
|
| 113 |
try:
|
| 114 |
+
self.merge_results_python(rigged_file, original_file, str(output_file))
|
| 115 |
except Exception as e:
|
| 116 |
error_msg = f"Error: Merge failed: {str(e)}"
|
| 117 |
traceback.print_exc()
|
| 118 |
return error_msg, "", ""
|
| 119 |
|
| 120 |
+
# Validate that the output file exists and is a file (not a directory)
|
| 121 |
+
output_file_abs = output_file.resolve()
|
| 122 |
+
if not output_file_abs.exists():
|
| 123 |
return "Error: Merged file was not generated", "", ""
|
| 124 |
|
| 125 |
+
if not output_file_abs.is_file():
|
| 126 |
+
return f"Error: Output path is not a valid file: {output_file_abs}", "", ""
|
| 127 |
|
| 128 |
+
# Generate preview information
|
| 129 |
+
preview_info = self.generate_model_preview(str(output_file_abs))
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
return "β
Model rigging completed successfully!", str(output_file_abs), preview_info
|
| 132 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
def complete_pipeline(self, input_file: str, seed: int = 12345) -> Tuple[str, str, str, str, str]:
|
| 134 |
"""
|
| 135 |
Run the complete rigging pipeline: skeleton generation β skinning β merge.
|
|
|
|
| 141 |
Returns:
|
| 142 |
Tuple of status messages and file paths for each step
|
| 143 |
"""
|
| 144 |
+
# Validate input file
|
| 145 |
+
if not self.validate_input_file(input_file):
|
| 146 |
+
raise gr.Error(f"Error: Invalid or unsupported file format. Supported formats: {', '.join(self.supported_formats)}")
|
| 147 |
+
|
| 148 |
+
# Create working directory
|
| 149 |
+
file_stem = Path(input_file).stem
|
| 150 |
+
input_model_dir = self.temp_dir / f"{file_stem}_{seed}"
|
| 151 |
+
input_model_dir.mkdir(exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
+
# Copy input file to working directory
|
| 154 |
+
input_file = Path(input_file)
|
| 155 |
+
shutil.copy2(input_file, input_model_dir / input_file.name)
|
| 156 |
+
input_file = input_model_dir / input_file.name
|
| 157 |
+
print(f"New input file path: {input_file}")
|
| 158 |
+
|
| 159 |
+
# Step 1: Generate skeleton
|
| 160 |
+
output_skeleton_file = input_model_dir / f"{file_stem}_skeleton.fbx"
|
| 161 |
+
self.run_skeleton_inference_python(input_file, output_skeleton_file, seed)
|
| 162 |
|
| 163 |
+
# Step 2: Generate skinning
|
| 164 |
+
output_skin_file = input_model_dir / f"{file_stem}_skin.fbx"
|
| 165 |
+
self.run_skin_inference_python(output_skeleton_file, output_skin_file)
|
| 166 |
+
|
| 167 |
+
# Step 3: Merge results
|
| 168 |
+
final_file = input_model_dir / f"{file_stem}_rigged.glb"
|
| 169 |
+
self.merge_results_python(output_skin_file, input_file, final_file)
|
| 170 |
+
|
| 171 |
+
return str(final_file)
|
| 172 |
+
|
| 173 |
def extract_mesh_python(self, input_file: str, output_dir: str) -> str:
|
| 174 |
"""
|
| 175 |
Extract mesh data from 3D model using Python (replaces extract.sh)
|
| 176 |
Returns path to generated .npz file
|
| 177 |
"""
|
| 178 |
# Import required modules
|
| 179 |
+
from src.data.extract import get_files, extract_builtin
|
| 180 |
|
| 181 |
# Create extraction parameters
|
| 182 |
files = get_files(
|
| 183 |
data_name="raw_data.npz",
|
| 184 |
+
inputs=str(input_file),
|
| 185 |
input_dataset_dir=None,
|
| 186 |
output_dataset_dir=output_dir,
|
| 187 |
force_override=True,
|
| 188 |
warning=False,
|
| 189 |
)
|
| 190 |
|
| 191 |
+
if not files:
|
| 192 |
+
raise RuntimeError("No files to extract")
|
| 193 |
+
|
| 194 |
+
# Run the actual extraction
|
| 195 |
+
timestamp = str(int(time.time()))
|
| 196 |
+
extract_builtin(
|
| 197 |
+
output_folder=output_dir,
|
| 198 |
+
target_count=50000,
|
| 199 |
+
num_runs=1,
|
| 200 |
+
id=0,
|
| 201 |
+
time=timestamp,
|
| 202 |
+
files=files,
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
# Return the directory path where raw_data.npz was created
|
| 206 |
+
# The dataset expects to find raw_data.npz in this directory
|
| 207 |
+
expected_npz_dir = files[0][1] # This is the output directory
|
| 208 |
+
expected_npz_file = Path(expected_npz_dir) / "raw_data.npz"
|
| 209 |
|
| 210 |
+
if not expected_npz_file.exists():
|
| 211 |
+
raise RuntimeError(f"Extraction failed: {expected_npz_file} not found")
|
| 212 |
+
|
| 213 |
+
return expected_npz_dir # Return the directory containing raw_data.npz
|
| 214 |
|
| 215 |
def run_skeleton_inference_python(self, input_file: str, output_file: str, seed: int = 12345) -> str:
|
| 216 |
"""
|
| 217 |
Run skeleton inference using Python (replaces skeleton part of generate_skeleton.sh)
|
| 218 |
Returns path to skeleton FBX file
|
| 219 |
"""
|
|
|
|
| 220 |
from box import Box
|
| 221 |
+
|
| 222 |
from src.data.datapath import Datapath
|
| 223 |
+
from src.data.dataset import DatasetConfig, UniRigDatasetModule
|
| 224 |
from src.data.transform import TransformConfig
|
| 225 |
+
from src.inference.download import download
|
|
|
|
| 226 |
from src.model.parse import get_model
|
| 227 |
from src.system.parse import get_system, get_writer
|
| 228 |
+
from src.tokenizer.parse import get_tokenizer
|
| 229 |
+
from src.tokenizer.spec import TokenizerConfig
|
| 230 |
|
| 231 |
# Set random seed
|
| 232 |
L.seed_everything(seed, workers=True)
|
| 233 |
|
| 234 |
# Load task configuration
|
| 235 |
task_config_path = "configs/task/quick_inference_skeleton_articulationxl_ar_256.yaml"
|
| 236 |
+
if not Path(task_config_path).exists():
|
| 237 |
+
raise FileNotFoundError(f"Task configuration file not found: {task_config_path}")
|
| 238 |
+
|
| 239 |
+
# Load the task configuration
|
| 240 |
with open(task_config_path, 'r') as f:
|
| 241 |
task = Box(yaml.safe_load(f))
|
| 242 |
|
| 243 |
# Create temporary npz directory
|
| 244 |
+
npz_dir = Path(output_file).parent / "npz"
|
| 245 |
+
npz_dir.mkdir(exist_ok=True)
|
| 246 |
|
| 247 |
# Extract mesh data
|
| 248 |
+
npz_data_dir = self.extract_mesh_python(input_file, npz_dir)
|
| 249 |
|
| 250 |
+
# Setup datapath with the directory containing raw_data.npz
|
| 251 |
+
datapath = Datapath(files=[npz_data_dir], cls=None)
|
| 252 |
|
| 253 |
# Load configurations
|
| 254 |
data_config = Box(yaml.safe_load(open("configs/data/quick_inference.yaml", 'r')))
|
|
|
|
| 281 |
# Setup callbacks and writer
|
| 282 |
callbacks = []
|
| 283 |
writer_config = task.writer.copy()
|
| 284 |
+
writer_config['npz_dir'] = str(npz_dir)
|
| 285 |
+
writer_config['output_dir'] = str(Path(output_file).parent)
|
| 286 |
+
writer_config['output_name'] = Path(output_file).name
|
| 287 |
+
writer_config['user_mode'] = False # Set to False to enable NPZ export
|
| 288 |
+
print(f"Writer config: {writer_config}")
|
| 289 |
+
# But we want the FBX to go to our specified location when in user mode for FBX
|
| 290 |
callbacks.append(get_writer(**writer_config, order_config=predict_transform_config.order_config))
|
| 291 |
|
| 292 |
# Get system
|
|
|
|
| 302 |
# Run prediction
|
| 303 |
trainer.predict(system, datamodule=data, ckpt_path=resume_from_checkpoint, return_predictions=False)
|
| 304 |
|
| 305 |
+
# The actual output file will be in a subdirectory named after the input file
|
| 306 |
+
# Look for the generated skeleton.fbx file
|
| 307 |
+
input_name_stem = Path(input_file).stem
|
| 308 |
+
actual_output_dir = Path(output_file).parent / input_name_stem
|
| 309 |
+
actual_output_file = actual_output_dir / "skeleton.fbx"
|
| 310 |
+
|
| 311 |
+
if not actual_output_file.exists():
|
| 312 |
+
# Try alternative locations - look for any skeleton.fbx file in the output directory
|
| 313 |
+
alt_files = list(Path(output_file).parent.rglob("skeleton.fbx"))
|
| 314 |
+
if alt_files:
|
| 315 |
+
actual_output_file = alt_files[0]
|
| 316 |
+
print(f"Found skeleton at alternative location: {actual_output_file}")
|
| 317 |
+
else:
|
| 318 |
+
# List all files for debugging
|
| 319 |
+
all_files = list(Path(output_file).parent.rglob("*"))
|
| 320 |
+
print(f"Available files: {[str(f) for f in all_files]}")
|
| 321 |
+
raise RuntimeError(f"Skeleton FBX file not found. Expected at: {actual_output_file}")
|
| 322 |
+
|
| 323 |
+
# Copy to the expected output location
|
| 324 |
+
if actual_output_file != Path(output_file):
|
| 325 |
+
shutil.copy2(actual_output_file, output_file)
|
| 326 |
+
print(f"Copied skeleton from {actual_output_file} to {output_file}")
|
| 327 |
+
|
| 328 |
+
print(f"Generated skeleton at: {output_file}")
|
| 329 |
+
return str(output_file)
|
| 330 |
|
| 331 |
def run_skin_inference_python(self, skeleton_file: str, output_file: str) -> str:
|
| 332 |
"""
|
| 333 |
Run skin inference using Python (replaces skin part of generate_skin.sh)
|
| 334 |
Returns path to skin FBX file
|
| 335 |
"""
|
|
|
|
| 336 |
from box import Box
|
| 337 |
+
|
| 338 |
from src.data.datapath import Datapath
|
| 339 |
+
from src.data.dataset import DatasetConfig, UniRigDatasetModule
|
| 340 |
from src.data.transform import TransformConfig
|
| 341 |
+
from src.inference.download import download
|
| 342 |
from src.model.parse import get_model
|
| 343 |
from src.system.parse import get_system, get_writer
|
|
|
|
| 344 |
|
| 345 |
# Load task configuration
|
| 346 |
task_config_path = "configs/task/quick_inference_unirig_skin.yaml"
|
| 347 |
with open(task_config_path, 'r') as f:
|
| 348 |
task = Box(yaml.safe_load(f))
|
| 349 |
+
|
| 350 |
+
# Look for files matching predict_skeleton.npz pattern recursively
|
| 351 |
+
skeleton_work_dir = Path(skeleton_file).parent
|
| 352 |
+
all_npz_files = list(skeleton_work_dir.rglob("**/*.npz"))
|
| 353 |
|
| 354 |
+
# Setup datapath - need to pass the directory containing the NPZ file
|
| 355 |
+
skeleton_npz_dir = all_npz_files[0].parent
|
| 356 |
+
datapath = Datapath(files=[str(skeleton_npz_dir)], cls=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
# Load configurations
|
| 359 |
data_config = Box(yaml.safe_load(open("configs/data/quick_inference.yaml", 'r')))
|
|
|
|
| 382 |
# Setup callbacks and writer
|
| 383 |
callbacks = []
|
| 384 |
writer_config = task.writer.copy()
|
| 385 |
+
writer_config['npz_dir'] = str(skeleton_npz_dir)
|
| 386 |
+
writer_config['output_name'] = str(output_file)
|
|
|
|
| 387 |
writer_config['user_mode'] = True
|
| 388 |
+
writer_config['export_fbx'] = True # Enable FBX export
|
| 389 |
callbacks.append(get_writer(**writer_config, order_config=predict_transform_config.order_config))
|
| 390 |
|
| 391 |
# Get system
|
|
|
|
| 401 |
# Run prediction
|
| 402 |
trainer.predict(system, datamodule=data, ckpt_path=resume_from_checkpoint, return_predictions=False)
|
| 403 |
|
| 404 |
+
# The skin FBX file should be generated with the specified output name
|
| 405 |
+
# Since user_mode is True and export_fbx is True, it should create the file directly
|
| 406 |
+
if not Path(output_file).exists():
|
| 407 |
+
# Look for generated skin FBX files in the output directory
|
| 408 |
+
skin_files = list(Path(output_file).parent.rglob("*skin*.fbx"))
|
| 409 |
+
if skin_files:
|
| 410 |
+
actual_output_file = skin_files[0]
|
| 411 |
+
# Copy/move to the expected location
|
| 412 |
+
shutil.copy2(actual_output_file, output_file)
|
| 413 |
+
else:
|
| 414 |
+
raise RuntimeError(f"Skin FBX file not found. Expected at: {output_file}")
|
| 415 |
+
|
| 416 |
+
return str(output_file)
|
| 417 |
|
| 418 |
def merge_results_python(self, source_file: str, target_file: str, output_file: str) -> str:
|
| 419 |
"""
|
|
|
|
| 422 |
"""
|
| 423 |
from src.inference.merge import transfer
|
| 424 |
|
| 425 |
+
# Validate input paths
|
| 426 |
+
if not Path(source_file).exists():
|
| 427 |
+
raise ValueError(f"Source file does not exist: {source_file}")
|
| 428 |
+
if not Path(target_file).exists():
|
| 429 |
+
raise ValueError(f"Target file does not exist: {target_file}")
|
| 430 |
+
|
| 431 |
+
# Ensure output directory exists
|
| 432 |
+
output_path = Path(output_file)
|
| 433 |
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 434 |
+
|
| 435 |
# Use the transfer function directly
|
| 436 |
+
transfer(source=str(source_file), target=str(target_file), output=str(output_path), add_root=False)
|
| 437 |
+
|
| 438 |
+
# Validate that the output file was created and is a valid file
|
| 439 |
+
if not output_path.exists():
|
| 440 |
+
raise RuntimeError(f"Merge failed: Output file not created at {output_path}")
|
| 441 |
|
| 442 |
+
if not output_path.is_file():
|
| 443 |
+
raise RuntimeError(f"Merge failed: Output path is not a valid file: {output_path}")
|
| 444 |
+
|
| 445 |
+
return str(output_path.resolve())
|
| 446 |
|
| 447 |
|
| 448 |
def create_app():
|
|
|
|
| 471 |
<li><strong>Skinning Weights:</strong> Automatic vertex-to-bone weight assignment</li>
|
| 472 |
<li><strong>Universal Support:</strong> Works with humans, animals, and objects</li>
|
| 473 |
</ul>
|
| 474 |
+
<p><strong>Supported formats:</strong> .obj, .fbx, .glb</p>
|
| 475 |
""")
|
| 476 |
|
| 477 |
+
with gr.Row(equal_height=True):
|
| 478 |
+
with gr.Column(scale=1):
|
| 479 |
+
input_3d_model = gr.File(
|
| 480 |
+
label="Upload 3D Model",
|
| 481 |
+
file_types=[".obj", ".fbx", ".glb"],
|
| 482 |
+
type="filepath",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 483 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
|
| 485 |
+
with gr.Row(equal_height=True):
|
| 486 |
+
seed = gr.Number(
|
| 487 |
+
value=12345,
|
| 488 |
+
label="Random Seed (for reproducible results)",
|
| 489 |
+
scale=4,
|
| 490 |
+
)
|
| 491 |
+
random_btn = gr.Button("π Random Seed", variant="secondary", scale=1)
|
| 492 |
+
pipeline_btn = gr.Button("π― Start Complete Pipeline", variant="primary", size="lg")
|
| 493 |
+
|
| 494 |
+
pipeline_skeleton_out = gr.File(label="Final Rigged Model")
|
| 495 |
+
|
| 496 |
+
random_btn.click(
|
| 497 |
+
fn=lambda: int(torch.randint(0, 100000, (1,)).item()),
|
| 498 |
+
outputs=seed
|
| 499 |
+
)
|
| 500 |
+
|
| 501 |
+
pipeline_btn.click(
|
| 502 |
+
fn=demo_instance.complete_pipeline,
|
| 503 |
+
inputs=[input_3d_model, seed],
|
| 504 |
+
outputs=[pipeline_skeleton_out]
|
| 505 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
|
| 507 |
# Footer
|
| 508 |
gr.HTML("""
|
|
|
|
| 513 |
π <a href="https://zjp-shadow.github.io/works/UniRig/" target="_blank">Project Page</a> |
|
| 514 |
π€ <a href="https://huggingface.co/VAST-AI/UniRig" target="_blank">Models</a>
|
| 515 |
</p>
|
|
|
|
|
|
|
|
|
|
| 516 |
</div>
|
| 517 |
""")
|
| 518 |
|