Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -6,170 +6,353 @@ import tempfile
|
|
6 |
import shutil
|
7 |
import subprocess
|
8 |
import spaces
|
9 |
-
from typing import Any, Dict, Union
|
10 |
|
11 |
# --- Configuration ---
|
12 |
# Path to the cloned UniRig repository directory within the Space
|
13 |
-
|
|
|
14 |
|
15 |
-
#
|
16 |
-
|
|
|
|
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
# Path to the setup script
|
21 |
SETUP_SCRIPT = os.path.join(os.path.dirname(__file__), "setup_blender.sh")
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
else:
|
28 |
-
print("Blender
|
29 |
|
30 |
-
# Verify Blender Python path
|
31 |
-
if os.path.exists(
|
32 |
-
print(f"Blender Python
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
else:
|
36 |
-
print("bpy
|
37 |
else:
|
38 |
-
print(f"Blender Python
|
|
|
39 |
|
|
|
40 |
if not os.path.isdir(UNIRIG_REPO_DIR):
|
41 |
-
print(f"ERROR: UniRig repository not found at {UNIRIG_REPO_DIR}.
|
42 |
-
#
|
|
|
|
|
|
|
43 |
|
|
|
44 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
45 |
-
print(f"
|
46 |
if DEVICE.type == 'cuda':
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
49 |
else:
|
50 |
-
print("Warning: CUDA not available or not detected by PyTorch.
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
def patch_asset_py():
|
53 |
-
"""Temporary patch to fix type hinting error in asset.py"""
|
|
|
54 |
asset_py_path = os.path.join(UNIRIG_REPO_DIR, "src", "data", "asset.py")
|
55 |
try:
|
|
|
|
|
|
|
|
|
|
|
56 |
with open(asset_py_path, "r") as f:
|
57 |
content = f.read()
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
60 |
print("Patch already applied to asset.py")
|
61 |
return
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
with open(asset_py_path, "w") as f:
|
68 |
f.write(content)
|
69 |
print("Successfully patched asset.py")
|
|
|
70 |
except Exception as e:
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
process_env = os.environ.copy()
|
82 |
-
|
83 |
-
# Determine the path to the 'src' directory within UniRig, where the 'unirig' package resides.
|
84 |
unirig_src_dir = os.path.join(UNIRIG_REPO_DIR, "src")
|
85 |
|
86 |
-
#
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
try:
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
print(f"{step_name} STDOUT:\n{result.stdout}")
|
97 |
if result.stderr:
|
98 |
-
|
|
|
|
|
99 |
except subprocess.CalledProcessError as e:
|
100 |
-
|
|
|
101 |
print(f"Command: {' '.join(e.cmd)}")
|
102 |
print(f"Return code: {e.returncode}")
|
103 |
-
print(f"
|
104 |
-
print(f"
|
105 |
-
|
106 |
-
|
|
|
|
|
|
|
107 |
except FileNotFoundError:
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
110 |
except Exception as e_general:
|
|
|
111 |
print(f"An unexpected Python exception occurred in run_unirig_command for {step_name}: {e_general}")
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
@spaces.GPU
|
115 |
def rig_glb_mesh_multistep(input_glb_file_obj):
|
116 |
-
"""
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
|
|
122 |
if input_glb_file_obj is None:
|
123 |
raise gr.Error("No input file provided. Please upload a .glb mesh.")
|
124 |
|
125 |
input_glb_path = input_glb_file_obj
|
126 |
print(f"Input GLB path received: {input_glb_path}")
|
127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
processing_temp_dir = tempfile.mkdtemp(prefix="unirig_processing_")
|
129 |
print(f"Using temporary processing directory: {processing_temp_dir}")
|
130 |
|
131 |
try:
|
|
|
132 |
base_name = os.path.splitext(os.path.basename(input_glb_path))[0]
|
|
|
133 |
abs_skeleton_output_path = os.path.join(processing_temp_dir, f"{base_name}_skeleton.fbx")
|
134 |
abs_skin_output_path = os.path.join(processing_temp_dir, f"{base_name}_skin.fbx")
|
135 |
abs_final_rigged_glb_path = os.path.join(processing_temp_dir, f"{base_name}_rigged_final.glb")
|
136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
# Step 1: Skeleton Prediction
|
138 |
-
print("Step 1: Predicting Skeleton...")
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
if not os.path.exists(abs_skeleton_output_path):
|
142 |
-
raise gr.Error("Skeleton prediction failed
|
|
|
143 |
|
144 |
# Step 2: Skinning Weight Prediction
|
145 |
-
print("Step 2: Predicting Skinning Weights...")
|
146 |
-
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
if not os.path.exists(abs_skin_output_path):
|
149 |
-
raise gr.Error("Skinning prediction failed
|
|
|
150 |
|
151 |
# Step 3: Merge Skeleton/Skin with Original Mesh
|
152 |
-
print("Step 3: Merging Results...")
|
153 |
-
|
154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
if not os.path.exists(abs_final_rigged_glb_path):
|
156 |
-
raise gr.Error("Merging process failed
|
|
|
157 |
|
|
|
|
|
158 |
return abs_final_rigged_glb_path
|
159 |
|
160 |
-
except gr.Error:
|
|
|
|
|
161 |
if os.path.exists(processing_temp_dir):
|
162 |
shutil.rmtree(processing_temp_dir)
|
163 |
print(f"Cleaned up temporary directory: {processing_temp_dir}")
|
164 |
-
raise
|
|
|
165 |
except Exception as e:
|
|
|
166 |
print(f"An unexpected error occurred in rig_glb_mesh_multistep: {e}")
|
|
|
|
|
|
|
167 |
if os.path.exists(processing_temp_dir):
|
168 |
shutil.rmtree(processing_temp_dir)
|
169 |
print(f"Cleaned up temporary directory: {processing_temp_dir}")
|
|
|
170 |
raise gr.Error(f"An unexpected error occurred during processing: {str(e)[:500]}")
|
171 |
|
172 |
-
|
|
|
173 |
theme = gr.themes.Soft(
|
174 |
primary_hue=gr.themes.colors.sky,
|
175 |
secondary_hue=gr.themes.colors.blue,
|
@@ -177,24 +360,49 @@ theme = gr.themes.Soft(
|
|
177 |
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
178 |
)
|
179 |
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
|
|
197 |
if __name__ == "__main__":
|
198 |
-
|
199 |
-
|
200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
import shutil
|
7 |
import subprocess
|
8 |
import spaces
|
9 |
+
from typing import Any, Dict, Union, List
|
10 |
|
11 |
# --- Configuration ---
|
12 |
# Path to the cloned UniRig repository directory within the Space
|
13 |
+
# Ensure this path is correct relative to app.py
|
14 |
+
UNIRIG_REPO_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "UniRig")) # Get absolute path
|
15 |
|
16 |
+
# Absolute path to the Blender installation provided in the Space environment
|
17 |
+
BLENDER_INSTALL_DIR = "/opt/blender-4.2.0-linux-x64"
|
18 |
+
BLENDER_PYTHON_VERSION_DIR = "4.2" # Corresponds to Blender 4.2.x
|
19 |
+
BLENDER_PYTHON_VERSION = "python3.11" # Blender 4.2 uses Python 3.11
|
20 |
|
21 |
+
# Construct paths dynamically based on the above
|
22 |
+
BLENDER_PYTHON_DIR = os.path.join(BLENDER_INSTALL_DIR, BLENDER_PYTHON_VERSION_DIR, "python")
|
23 |
+
BLENDER_PYTHON_EXEC = os.path.join(BLENDER_PYTHON_DIR, "bin", BLENDER_PYTHON_VERSION) # Still needed for PYTHONPATH
|
24 |
+
BLENDER_PYTHON_LIB_PATH = os.path.join(BLENDER_PYTHON_DIR, "lib", BLENDER_PYTHON_VERSION)
|
25 |
+
BLENDER_PYTHON_SITE_PACKAGES = os.path.join(BLENDER_PYTHON_LIB_PATH, "site-packages")
|
26 |
|
27 |
+
# Path to the setup script (executed only if Blender isn't found initially)
|
28 |
SETUP_SCRIPT = os.path.join(os.path.dirname(__file__), "setup_blender.sh")
|
29 |
|
30 |
+
# --- Initial Checks ---
|
31 |
+
print("--- Environment Checks ---")
|
32 |
+
|
33 |
+
# Check if Blender Python executable exists (needed for environment setup)
|
34 |
+
# Run setup script if Blender isn't found (assuming setup script handles installation)
|
35 |
+
if not os.path.exists(BLENDER_PYTHON_EXEC):
|
36 |
+
print(f"Blender Python executable not found at {BLENDER_PYTHON_EXEC}. Running setup script...")
|
37 |
+
if os.path.exists(SETUP_SCRIPT):
|
38 |
+
try:
|
39 |
+
# Run the setup script using bash
|
40 |
+
setup_result = subprocess.run(["bash", SETUP_SCRIPT], check=True, capture_output=True, text=True)
|
41 |
+
print("Setup script executed successfully.")
|
42 |
+
print(f"Setup STDOUT:\n{setup_result.stdout}")
|
43 |
+
if setup_result.stderr:
|
44 |
+
print(f"Setup STDERR:\n{setup_result.stderr}")
|
45 |
+
# Re-check if the executable exists after running the script
|
46 |
+
if not os.path.exists(BLENDER_PYTHON_EXEC):
|
47 |
+
raise RuntimeError(f"Setup script ran but Blender Python still not found at {BLENDER_PYTHON_EXEC}.")
|
48 |
+
except subprocess.CalledProcessError as e:
|
49 |
+
print(f"ERROR running setup script: {SETUP_SCRIPT}")
|
50 |
+
print(f"Return Code: {e.returncode}")
|
51 |
+
print(f"Stdout: {e.stdout}")
|
52 |
+
print(f"Stderr: {e.stderr}")
|
53 |
+
# Raise a more informative error for Gradio if setup fails
|
54 |
+
raise gr.Error(f"Failed to execute setup script. Check logs. Stderr: {e.stderr[-500:]}")
|
55 |
+
except Exception as e:
|
56 |
+
raise gr.Error(f"Unexpected error running setup script '{SETUP_SCRIPT}': {e}")
|
57 |
+
else:
|
58 |
+
# If setup script is missing, raise a Gradio error
|
59 |
+
raise gr.Error(f"Blender Python not found and setup script missing: {SETUP_SCRIPT}")
|
60 |
else:
|
61 |
+
print(f"Blender Python executable found: {BLENDER_PYTHON_EXEC}")
|
62 |
|
63 |
+
# Verify Blender Python site-packages path and bpy module presence
|
64 |
+
if os.path.exists(BLENDER_PYTHON_SITE_PACKAGES):
|
65 |
+
print(f"Blender Python site-packages found at: {BLENDER_PYTHON_SITE_PACKAGES}")
|
66 |
+
# Check for 'bpy' directory which contains __init__.py and modules
|
67 |
+
bpy_module_path = os.path.join(BLENDER_PYTHON_SITE_PACKAGES, "bpy")
|
68 |
+
if os.path.isdir(bpy_module_path) and os.path.exists(os.path.join(bpy_module_path, "__init__.py")):
|
69 |
+
print("Blender Python 'bpy' module directory found.")
|
70 |
+
# Fallback check for bpy.so if directory structure is different
|
71 |
+
elif os.path.exists(os.path.join(BLENDER_PYTHON_SITE_PACKAGES, "bpy.so")):
|
72 |
+
print("Blender Python 'bpy.so' found.")
|
73 |
else:
|
74 |
+
print("WARNING: Blender Python 'bpy' module not found in site-packages. Check Blender installation or paths.")
|
75 |
else:
|
76 |
+
print(f"WARNING: Blender Python site-packages not found at {BLENDER_PYTHON_SITE_PACKAGES}. Check paths.")
|
77 |
+
|
78 |
|
79 |
+
# Check for UniRig repository
|
80 |
if not os.path.isdir(UNIRIG_REPO_DIR):
|
81 |
+
print(f"ERROR: UniRig repository not found at {UNIRIG_REPO_DIR}.")
|
82 |
+
# Raise Gradio error if critical component is missing
|
83 |
+
raise gr.Error(f"UniRig repository missing at: {UNIRIG_REPO_DIR}. Ensure it's cloned correctly.")
|
84 |
+
else:
|
85 |
+
print(f"UniRig repository found at: {UNIRIG_REPO_DIR}")
|
86 |
|
87 |
+
# Check PyTorch and CUDA
|
88 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
89 |
+
print(f"Gradio environment using device: {DEVICE}")
|
90 |
if DEVICE.type == 'cuda':
|
91 |
+
try:
|
92 |
+
print(f"Gradio CUDA Device Name: {torch.cuda.get_device_name(0)}")
|
93 |
+
print(f"Gradio PyTorch CUDA Version: {torch.version.cuda}")
|
94 |
+
except Exception as e:
|
95 |
+
print(f"Could not get CUDA device details: {e}")
|
96 |
else:
|
97 |
+
print("Warning: Gradio environment CUDA not available or not detected by PyTorch.")
|
98 |
+
print("UniRig subprocess will attempt to use GPU via Blender's Python environment (invoked by .sh scripts).")
|
99 |
+
|
100 |
+
print("--- End Environment Checks ---")
|
101 |
+
|
102 |
+
# --- Helper Functions ---
|
103 |
|
104 |
def patch_asset_py():
|
105 |
+
"""Temporary patch to fix type hinting error in UniRig's asset.py"""
|
106 |
+
# This patch might still be needed if the .sh scripts call Python code that uses asset.py
|
107 |
asset_py_path = os.path.join(UNIRIG_REPO_DIR, "src", "data", "asset.py")
|
108 |
try:
|
109 |
+
# Check if file exists before trying to open
|
110 |
+
if not os.path.exists(asset_py_path):
|
111 |
+
print(f"Warning: asset.py not found at {asset_py_path}, skipping patch.")
|
112 |
+
return # Don't raise error, maybe it's not needed
|
113 |
+
|
114 |
with open(asset_py_path, "r") as f:
|
115 |
content = f.read()
|
116 |
+
|
117 |
+
problematic_line = "meta: Union[Dict[str, ...], None]=None"
|
118 |
+
corrected_line = "meta: Union[Dict[str, Any], None]=None"
|
119 |
+
typing_import = "from typing import Any"
|
120 |
+
|
121 |
+
if corrected_line in content:
|
122 |
print("Patch already applied to asset.py")
|
123 |
return
|
124 |
+
if problematic_line not in content:
|
125 |
+
print("Problematic line not found in asset.py, patch might be unnecessary.")
|
126 |
+
return
|
127 |
+
|
128 |
+
print("Applying patch to asset.py...")
|
129 |
+
content = content.replace(problematic_line, corrected_line)
|
130 |
+
if typing_import not in content:
|
131 |
+
if "from typing import" in content:
|
132 |
+
content = content.replace("from typing import", f"{typing_import}\nfrom typing import", 1)
|
133 |
+
else:
|
134 |
+
content = f"{typing_import}\n{content}"
|
135 |
+
|
136 |
with open(asset_py_path, "w") as f:
|
137 |
f.write(content)
|
138 |
print("Successfully patched asset.py")
|
139 |
+
|
140 |
except Exception as e:
|
141 |
+
# Log error but don't necessarily stop the app, maybe patch isn't critical
|
142 |
+
print(f"ERROR: Failed to patch asset.py: {e}. Proceeding cautiously.")
|
143 |
+
# raise gr.Error(f"Failed to apply necessary patch to UniRig code: {e}") # Optional: make it fatal
|
144 |
+
|
145 |
+
@spaces.GPU
|
146 |
+
def run_unirig_command(script_path: str, args: List[str], step_name: str):
|
147 |
+
"""
|
148 |
+
Runs a specific UniRig SHELL script (.sh) using bash in a subprocess,
|
149 |
+
ensuring the correct environment (PYTHONPATH, etc.) is set.
|
150 |
+
|
151 |
+
Args:
|
152 |
+
script_path: Absolute path to the .sh script to execute.
|
153 |
+
args: A list of command-line arguments for the script.
|
154 |
+
step_name: Name of the step for logging.
|
155 |
+
"""
|
156 |
+
# Command is now bash + script path + arguments
|
157 |
+
cmd = ["bash", script_path] + args
|
158 |
+
|
159 |
+
print(f"\n--- Running UniRig Step: {step_name} ---")
|
160 |
+
# Use subprocess.list2cmdline for a more accurate representation if needed,
|
161 |
+
# but simple join is often fine for logging.
|
162 |
+
print(f"Command: {' '.join(cmd)}")
|
163 |
+
|
164 |
+
# Prepare the environment for the subprocess (shell script)
|
165 |
+
# The shell script will likely invoke python and needs the correct env
|
166 |
process_env = os.environ.copy()
|
|
|
|
|
167 |
unirig_src_dir = os.path.join(UNIRIG_REPO_DIR, "src")
|
168 |
|
169 |
+
# Crucial: Set PYTHONPATH so the python invoked by the shell script finds Blender's
|
170 |
+
# site-packages and the UniRig source code.
|
171 |
+
pythonpath_parts = [
|
172 |
+
BLENDER_PYTHON_SITE_PACKAGES,
|
173 |
+
unirig_src_dir,
|
174 |
+
UNIRIG_REPO_DIR # Include base repo dir as well
|
175 |
+
]
|
176 |
+
process_env["PYTHONPATH"] = os.pathsep.join(filter(None, pythonpath_parts))
|
177 |
+
print(f"Subprocess PYTHONPATH: {process_env['PYTHONPATH']}")
|
178 |
+
|
179 |
+
# Add Blender's library path and potentially the python bin path to PATH
|
180 |
+
# This helps the shell find the correct python if needed, and shared libs
|
181 |
+
blender_lib_path = os.path.join(BLENDER_PYTHON_DIR, "lib")
|
182 |
+
blender_bin_path = os.path.join(BLENDER_PYTHON_DIR, "bin")
|
183 |
+
process_env["LD_LIBRARY_PATH"] = f"{blender_lib_path}{os.pathsep}{process_env.get('LD_LIBRARY_PATH', '')}"
|
184 |
+
process_env["PATH"] = f"{blender_bin_path}{os.pathsep}{process_env.get('PATH', '')}" # Add blender python bin to PATH
|
185 |
+
print(f"Subprocess LD_LIBRARY_PATH: {process_env['LD_LIBRARY_PATH']}")
|
186 |
+
print(f"Subprocess PATH: {process_env['PATH']}")
|
187 |
+
|
188 |
|
189 |
try:
|
190 |
+
# Execute the shell script.
|
191 |
+
# cwd=UNIRIG_REPO_DIR ensures the script runs from the repo's root,
|
192 |
+
# which is often necessary for finding relative paths within the script.
|
193 |
+
result = subprocess.run(
|
194 |
+
cmd,
|
195 |
+
cwd=UNIRIG_REPO_DIR,
|
196 |
+
capture_output=True,
|
197 |
+
text=True,
|
198 |
+
check=True, # Raises CalledProcessError on non-zero exit codes
|
199 |
+
env=process_env
|
200 |
+
)
|
201 |
print(f"{step_name} STDOUT:\n{result.stdout}")
|
202 |
if result.stderr:
|
203 |
+
# Treat stderr as potential warnings unless check=True failed
|
204 |
+
print(f"{step_name} STDERR (Warnings/Info):\n{result.stderr}")
|
205 |
+
|
206 |
except subprocess.CalledProcessError as e:
|
207 |
+
# Error occurred within the shell script (non-zero exit code)
|
208 |
+
print(f"ERROR during {step_name}: Subprocess failed!")
|
209 |
print(f"Command: {' '.join(e.cmd)}")
|
210 |
print(f"Return code: {e.returncode}")
|
211 |
+
print(f"--- {step_name} STDOUT ---:\n{e.stdout}")
|
212 |
+
print(f"--- {step_name} STDERR ---:\n{e.stderr}")
|
213 |
+
# Provide a concise error message to Gradio
|
214 |
+
error_summary = e.stderr.strip().splitlines()
|
215 |
+
last_lines = "\n".join(error_summary[-5:]) if error_summary else "No stderr output."
|
216 |
+
raise gr.Error(f"Error in UniRig '{step_name}'. Check logs. Last error lines:\n{last_lines}")
|
217 |
+
|
218 |
except FileNotFoundError:
|
219 |
+
# This error means 'bash' or the script_path wasn't found
|
220 |
+
print(f"ERROR: Could not find executable 'bash' or script '{script_path}' for {step_name}.")
|
221 |
+
print(f"Attempted command: {' '.join(cmd)}")
|
222 |
+
raise gr.Error(f"Setup error for UniRig '{step_name}'. 'bash' or script '{os.path.basename(script_path)}' not found.")
|
223 |
+
|
224 |
except Exception as e_general:
|
225 |
+
# Catch any other unexpected Python errors during subprocess handling
|
226 |
print(f"An unexpected Python exception occurred in run_unirig_command for {step_name}: {e_general}")
|
227 |
+
import traceback
|
228 |
+
traceback.print_exc()
|
229 |
+
raise gr.Error(f"Unexpected Python error during '{step_name}' execution: {str(e_general)[:500]}")
|
230 |
+
|
231 |
+
print(f"--- Finished UniRig Step: {step_name} ---")
|
232 |
+
|
233 |
|
234 |
@spaces.GPU
|
235 |
def rig_glb_mesh_multistep(input_glb_file_obj):
|
236 |
+
"""
|
237 |
+
Main Gradio function to rig a GLB mesh using UniRig's multi-step process.
|
238 |
+
Orchestrates calls to run_unirig_command for each step, executing .sh scripts.
|
239 |
+
"""
|
240 |
+
try:
|
241 |
+
patch_asset_py() # Attempt patch, might still be relevant
|
242 |
+
except gr.Error as e:
|
243 |
+
# If patching is critical and fails, raise the error
|
244 |
+
# raise e
|
245 |
+
print(f"Ignoring patch error: {e}") # Or just log it
|
246 |
+
except Exception as e:
|
247 |
+
print(f"Ignoring unexpected patch error: {e}")
|
248 |
+
# raise gr.Error(f"Failed to prepare UniRig environment: {e}")
|
249 |
|
250 |
+
# --- Input Validation ---
|
251 |
if input_glb_file_obj is None:
|
252 |
raise gr.Error("No input file provided. Please upload a .glb mesh.")
|
253 |
|
254 |
input_glb_path = input_glb_file_obj
|
255 |
print(f"Input GLB path received: {input_glb_path}")
|
256 |
|
257 |
+
if not os.path.exists(input_glb_path):
|
258 |
+
raise gr.Error(f"Input file path does not exist: {input_glb_path}")
|
259 |
+
if not input_glb_path.lower().endswith(".glb"):
|
260 |
+
raise gr.Error("Invalid file type. Please upload a .glb file.")
|
261 |
+
|
262 |
+
# --- Setup Temporary Directory ---
|
263 |
processing_temp_dir = tempfile.mkdtemp(prefix="unirig_processing_")
|
264 |
print(f"Using temporary processing directory: {processing_temp_dir}")
|
265 |
|
266 |
try:
|
267 |
+
# --- Define File Paths ---
|
268 |
base_name = os.path.splitext(os.path.basename(input_glb_path))[0]
|
269 |
+
abs_input_glb_path = os.path.abspath(input_glb_path)
|
270 |
abs_skeleton_output_path = os.path.join(processing_temp_dir, f"{base_name}_skeleton.fbx")
|
271 |
abs_skin_output_path = os.path.join(processing_temp_dir, f"{base_name}_skin.fbx")
|
272 |
abs_final_rigged_glb_path = os.path.join(processing_temp_dir, f"{base_name}_rigged_final.glb")
|
273 |
|
274 |
+
# --- Define Absolute Paths to UniRig SHELL Scripts ---
|
275 |
+
# Construct absolute paths based on UNIRIG_REPO_DIR, using .sh extension
|
276 |
+
skeleton_script_path = os.path.join(UNIRIG_REPO_DIR, "launch/inference/generate_skeleton.sh")
|
277 |
+
skin_script_path = os.path.join(UNIRIG_REPO_DIR, "launch/inference/generate_skin.sh")
|
278 |
+
merge_script_path = os.path.join(UNIRIG_REPO_DIR, "launch/inference/merge.sh")
|
279 |
+
|
280 |
+
# --- Execute UniRig Steps ---
|
281 |
+
|
282 |
# Step 1: Skeleton Prediction
|
283 |
+
print("\nStarting Step 1: Predicting Skeleton...")
|
284 |
+
# Arguments for the generate_skeleton.sh script
|
285 |
+
skeleton_args = [
|
286 |
+
"--input", abs_input_glb_path,
|
287 |
+
"--output", abs_skeleton_output_path
|
288 |
+
# Add any other arguments the shell script expects
|
289 |
+
]
|
290 |
+
# Check if script exists before running
|
291 |
+
if not os.path.exists(skeleton_script_path):
|
292 |
+
raise gr.Error(f"Skeleton script not found at: {skeleton_script_path}")
|
293 |
+
run_unirig_command(skeleton_script_path, skeleton_args, "Skeleton Prediction")
|
294 |
if not os.path.exists(abs_skeleton_output_path):
|
295 |
+
raise gr.Error("Skeleton prediction failed. Output file not created. Check logs.")
|
296 |
+
print("Step 1: Skeleton Prediction completed.")
|
297 |
|
298 |
# Step 2: Skinning Weight Prediction
|
299 |
+
print("\nStarting Step 2: Predicting Skinning Weights...")
|
300 |
+
# Arguments for the generate_skin.sh script
|
301 |
+
skin_args = [
|
302 |
+
"--input", abs_skeleton_output_path, # Input is the skeleton from step 1
|
303 |
+
"--source", abs_input_glb_path, # Source mesh
|
304 |
+
"--output", abs_skin_output_path
|
305 |
+
# Add any other arguments the shell script expects
|
306 |
+
]
|
307 |
+
if not os.path.exists(skin_script_path):
|
308 |
+
raise gr.Error(f"Skinning script not found at: {skin_script_path}")
|
309 |
+
run_unirig_command(skin_script_path, skin_args, "Skinning Prediction")
|
310 |
if not os.path.exists(abs_skin_output_path):
|
311 |
+
raise gr.Error("Skinning prediction failed. Output file not created. Check logs.")
|
312 |
+
print("Step 2: Skinning Prediction completed.")
|
313 |
|
314 |
# Step 3: Merge Skeleton/Skin with Original Mesh
|
315 |
+
print("\nStarting Step 3: Merging Results...")
|
316 |
+
# Arguments for the merge.sh script
|
317 |
+
merge_args = [
|
318 |
+
"--source", abs_skin_output_path, # Input is the skinned result from step 2
|
319 |
+
"--target", abs_input_glb_path, # Target is the original mesh
|
320 |
+
"--output", abs_final_rigged_glb_path
|
321 |
+
# Add any other arguments the shell script expects
|
322 |
+
]
|
323 |
+
if not os.path.exists(merge_script_path):
|
324 |
+
raise gr.Error(f"Merging script not found at: {merge_script_path}")
|
325 |
+
run_unirig_command(merge_script_path, merge_args, "Merging")
|
326 |
if not os.path.exists(abs_final_rigged_glb_path):
|
327 |
+
raise gr.Error("Merging process failed. Final rigged GLB file not created. Check logs.")
|
328 |
+
print("Step 3: Merging completed.")
|
329 |
|
330 |
+
# --- Return Result ---
|
331 |
+
print(f"Successfully generated rigged model: {abs_final_rigged_glb_path}")
|
332 |
return abs_final_rigged_glb_path
|
333 |
|
334 |
+
except gr.Error as e:
|
335 |
+
# If a gr.Error was raised, clean up and re-raise
|
336 |
+
print(f"Gradio Error occurred: {e}")
|
337 |
if os.path.exists(processing_temp_dir):
|
338 |
shutil.rmtree(processing_temp_dir)
|
339 |
print(f"Cleaned up temporary directory: {processing_temp_dir}")
|
340 |
+
raise e
|
341 |
+
|
342 |
except Exception as e:
|
343 |
+
# Catch any other unexpected errors
|
344 |
print(f"An unexpected error occurred in rig_glb_mesh_multistep: {e}")
|
345 |
+
import traceback
|
346 |
+
traceback.print_exc()
|
347 |
+
# Clean up temporary files before raising the error
|
348 |
if os.path.exists(processing_temp_dir):
|
349 |
shutil.rmtree(processing_temp_dir)
|
350 |
print(f"Cleaned up temporary directory: {processing_temp_dir}")
|
351 |
+
# Raise a generic Gradio error
|
352 |
raise gr.Error(f"An unexpected error occurred during processing: {str(e)[:500]}")
|
353 |
|
354 |
+
|
355 |
+
# --- Gradio Interface Definition ---
|
356 |
theme = gr.themes.Soft(
|
357 |
primary_hue=gr.themes.colors.sky,
|
358 |
secondary_hue=gr.themes.colors.blue,
|
|
|
360 |
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
361 |
)
|
362 |
|
363 |
+
# Check UniRig repo existence again before building the interface
|
364 |
+
if not os.path.isdir(UNIRIG_REPO_DIR):
|
365 |
+
startup_error_message = (
|
366 |
+
f"CRITICAL STARTUP ERROR: UniRig repository not found at {UNIRIG_REPO_DIR}. "
|
367 |
+
"The application cannot start. Please ensure the repository is cloned correctly."
|
368 |
+
)
|
369 |
+
print(startup_error_message)
|
370 |
+
# Display an error message if Gradio tries to build the UI without UniRig
|
371 |
+
with gr.Blocks(theme=theme) as iface:
|
372 |
+
gr.Markdown(f"# Application Error\n\n{startup_error_message}")
|
373 |
+
else:
|
374 |
+
# Build the normal interface if UniRig is found
|
375 |
+
iface = gr.Interface(
|
376 |
+
fn=rig_glb_mesh_multistep,
|
377 |
+
inputs=gr.File(
|
378 |
+
label="Upload .glb Mesh File",
|
379 |
+
type="filepath", # Provides the path to the uploaded file
|
380 |
+
file_types=[".glb"] # Restrict file types
|
381 |
+
),
|
382 |
+
outputs=gr.Model3D(
|
383 |
+
label="Rigged 3D Model (.glb)",
|
384 |
+
clear_color=[0.8, 0.8, 0.8, 1.0] # Background color for the viewer
|
385 |
+
),
|
386 |
+
title=f"UniRig Auto-Rigger (Blender {BLENDER_PYTHON_VERSION_DIR} / Python {BLENDER_PYTHON_VERSION})",
|
387 |
+
description=(
|
388 |
+
"Upload a 3D mesh in `.glb` format. This application uses UniRig via Blender (invoked through shell scripts) to automatically generate a skeleton and skinning weights.\n"
|
389 |
+
f"* Running main app on Python {sys.version.split()[0]}, UniRig steps use Blender's Python {BLENDER_PYTHON_VERSION} via .sh scripts.\n"
|
390 |
+
f"* Utilizing device: **{DEVICE.type.upper()}** (via ZeroGPU if available).\n"
|
391 |
+
f"* UniRig Source: https://github.com/VAST-AI-Research/UniRig"
|
392 |
+
),
|
393 |
+
cache_examples=False, # Disable caching if results depend heavily on GPU state or are large
|
394 |
+
theme=theme,
|
395 |
+
allow_flagging='never' # Disable flagging unless needed
|
396 |
+
)
|
397 |
|
398 |
+
# --- Launch the Application ---
|
399 |
if __name__ == "__main__":
|
400 |
+
# Ensure the interface object exists before launching
|
401 |
+
if 'iface' in locals():
|
402 |
+
print("Launching Gradio interface...")
|
403 |
+
# Consider adding share=True for public link if needed, or server_name="0.0.0.0"
|
404 |
+
iface.launch()
|
405 |
+
else:
|
406 |
+
# This case should only happen if the UniRig repo check failed above
|
407 |
+
print("ERROR: Gradio interface not created due to startup errors.")
|
408 |
+
|