jkorstad commited on
Commit
f58274e
·
verified ·
1 Parent(s): 2b04489

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -16
app.py CHANGED
@@ -151,14 +151,15 @@ def patch_asset_py():
151
  def run_unirig_command(script_path: str, args: List[str], step_name: str):
152
  """
153
  Runs a specific UniRig SHELL script (.sh) using bash in a subprocess,
154
- ensuring the correct environment (PYTHONPATH, etc.) is set.
155
 
156
  Args:
157
  script_path: Absolute path to the .sh script to execute.
158
  args: A list of command-line arguments for the script.
159
  step_name: Name of the step for logging.
160
  """
161
- cmd = ["bash", script_path] + args
 
162
 
163
  print(f"\n--- Running UniRig Step: {step_name} ---")
164
  print(f"Command: {' '.join(cmd)}")
@@ -167,7 +168,7 @@ def run_unirig_command(script_path: str, args: List[str], step_name: str):
167
  process_env = os.environ.copy()
168
  unirig_src_dir = os.path.join(UNIRIG_REPO_DIR, "src")
169
 
170
- # 1. Set PYTHONPATH: Blender’s site-packages + UniRig source
171
  pythonpath_parts = [
172
  BLENDER_PYTHON_SITE_PACKAGES,
173
  unirig_src_dir,
@@ -176,22 +177,27 @@ def run_unirig_command(script_path: str, args: List[str], step_name: str):
176
  process_env["PYTHONPATH"] = os.pathsep.join(filter(None, pythonpath_parts))
177
  print(f"Subprocess PYTHONPATH: {process_env['PYTHONPATH']}")
178
 
179
- # 2. Set LD_LIBRARY_PATH: Include Blender’s Python library directory
180
  blender_lib_path = os.path.join(BLENDER_PYTHON_DIR, "lib")
181
  existing_ld_path = process_env.get('LD_LIBRARY_PATH', '')
182
  process_env["LD_LIBRARY_PATH"] = f"{blender_lib_path}{os.pathsep}{existing_ld_path}" if existing_ld_path else blender_lib_path
183
  print(f"Subprocess LD_LIBRARY_PATH: {process_env['LD_LIBRARY_PATH']}")
184
 
185
- # 3. Set PATH with a temporary 'python' symlink to Blender’s Python
186
- temp_dir = tempfile.mkdtemp()
187
- try:
188
- python_link = os.path.join(temp_dir, "python")
189
- os.symlink(BLENDER_PYTHON_EXEC, python_link) # Links 'python' to 'python3.11'
190
- existing_path = process_env.get('PATH', '')
191
- process_env["PATH"] = f"{temp_dir}{os.pathsep}{BLENDER_PYTHON_BIN_DIR}{os.pathsep}{existing_path}"
192
- print(f"Subprocess PATH: {process_env['PATH']}")
 
 
 
 
193
 
194
- # Execute the shell script
 
195
  result = subprocess.run(
196
  cmd,
197
  cwd=UNIRIG_REPO_DIR,
@@ -229,9 +235,6 @@ def run_unirig_command(script_path: str, args: List[str], step_name: str):
229
  traceback.print_exc()
230
  raise gr.Error(f"Unexpected Python error during '{step_name}' execution: {str(e_general)[:500]}")
231
 
232
- finally:
233
- shutil.rmtree(temp_dir) # Clean up the temporary directory
234
-
235
  print(f"--- Finished UniRig Step: {step_name} ---")
236
 
237
 
@@ -273,6 +276,19 @@ def rig_glb_mesh_multistep(input_glb_file_obj):
273
  print(f"Failed to import torch in Blender's Python: {e.stderr}")
274
  raise gr.Error("PyTorch import failed in Blender's Python environment. Check installation.")
275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  # --- Setup Temporary Directory ---
277
  processing_temp_dir = tempfile.mkdtemp(prefix="unirig_processing_")
278
  print(f"Using temporary processing directory: {processing_temp_dir}")
 
151
  def run_unirig_command(script_path: str, args: List[str], step_name: str):
152
  """
153
  Runs a specific UniRig SHELL script (.sh) using bash in a subprocess,
154
+ ensuring the correct environment (PYTHONPATH, etc.) is set within Blender's context.
155
 
156
  Args:
157
  script_path: Absolute path to the .sh script to execute.
158
  args: A list of command-line arguments for the script.
159
  step_name: Name of the step for logging.
160
  """
161
+ # Use xvfb-run to enable headless execution of Blender
162
+ cmd = ["xvfb-run", "-a", "bash", script_path] + args
163
 
164
  print(f"\n--- Running UniRig Step: {step_name} ---")
165
  print(f"Command: {' '.join(cmd)}")
 
168
  process_env = os.environ.copy()
169
  unirig_src_dir = os.path.join(UNIRIG_REPO_DIR, "src")
170
 
171
+ # Set PYTHONPATH: Blender’s site-packages + UniRig source
172
  pythonpath_parts = [
173
  BLENDER_PYTHON_SITE_PACKAGES,
174
  unirig_src_dir,
 
177
  process_env["PYTHONPATH"] = os.pathsep.join(filter(None, pythonpath_parts))
178
  print(f"Subprocess PYTHONPATH: {process_env['PYTHONPATH']}")
179
 
180
+ # Set LD_LIBRARY_PATH: Include Blender’s Python library directory
181
  blender_lib_path = os.path.join(BLENDER_PYTHON_DIR, "lib")
182
  existing_ld_path = process_env.get('LD_LIBRARY_PATH', '')
183
  process_env["LD_LIBRARY_PATH"] = f"{blender_lib_path}{os.pathsep}{existing_ld_path}" if existing_ld_path else blender_lib_path
184
  print(f"Subprocess LD_LIBRARY_PATH: {process_env['LD_LIBRARY_PATH']}")
185
 
186
+ # Set PATH to prioritize Blender's Python bin directory
187
+ existing_path = process_env.get('PATH', '')
188
+ process_env["PATH"] = f"{BLENDER_PYTHON_BIN_DIR}{os.pathsep}{existing_path}"
189
+ print(f"Subprocess PATH: {process_env['PATH']}")
190
+
191
+ # Debug: Check which Python is used
192
+ python_bin = subprocess.run(["which", "python"], env=process_env, capture_output=True, text=True)
193
+ print(f"Python binary used: {python_bin.stdout.strip()}")
194
+
195
+ # Debug: Check Blender version
196
+ blender_check = subprocess.run([os.path.join(BLENDER_INSTALL_DIR, "blender"), "--version"], capture_output=True, text=True)
197
+ print(f"Blender version: {blender_check.stdout.strip()}")
198
 
199
+ try:
200
+ # Execute the shell script with xvfb-run
201
  result = subprocess.run(
202
  cmd,
203
  cwd=UNIRIG_REPO_DIR,
 
235
  traceback.print_exc()
236
  raise gr.Error(f"Unexpected Python error during '{step_name}' execution: {str(e_general)[:500]}")
237
 
 
 
 
238
  print(f"--- Finished UniRig Step: {step_name} ---")
239
 
240
 
 
276
  print(f"Failed to import torch in Blender's Python: {e.stderr}")
277
  raise gr.Error("PyTorch import failed in Blender's Python environment. Check installation.")
278
 
279
+ # Test Blender execution
280
+ try:
281
+ blender_test = subprocess.run(
282
+ [os.path.join(BLENDER_INSTALL_DIR, "blender"), "--version"],
283
+ capture_output=True,
284
+ text=True,
285
+ check=True
286
+ )
287
+ print(f"Blender version: {blender_test.stdout.strip()}")
288
+ except subprocess.CalledProcessError as e:
289
+ print(f"Failed to run Blender: {e.stderr}")
290
+ raise gr.Error("Blender is not accessible. Check installation.")
291
+
292
  # --- Setup Temporary Directory ---
293
  processing_temp_dir = tempfile.mkdtemp(prefix="unirig_processing_")
294
  print(f"Using temporary processing directory: {processing_temp_dir}")