Daankular commited on
Commit
60f5fd7
·
1 Parent(s): c1af5fa

Fix pymeshlab patch: always write file after import removal

Browse files

Previous patch only wrote the file if the exact function-body string matched.
If the match failed, the import removal was discarded and pymeshlab import
stayed in the file. Fix: always write after stripping the import; attempt
exact-string function replacement first, regex fallback second.

Files changed (1) hide show
  1. app.py +31 -16
app.py CHANGED
@@ -476,10 +476,20 @@ def load_triposg():
476
  if _it_path.exists():
477
  _it_text = _it_path.read_text()
478
  if "pymeshlab_replaced_v1" not in _it_text:
479
- # Remove the pymeshlab import
480
  _it_text = _it_text.replace("import pymeshlab\n", "")
481
- # Replace the three pymeshlab helper functions + simplify_mesh with trimesh version
482
- _old_simplify = (
 
 
 
 
 
 
 
 
 
 
483
  "def mesh_to_pymesh(vertices, faces):\n"
484
  " mesh = pymeshlab.Mesh(vertex_matrix=vertices, face_matrix=faces)\n"
485
  " ms = pymeshlab.MeshSet()\n"
@@ -502,20 +512,25 @@ def load_triposg():
502
  " else:\n"
503
  " return mesh\n"
504
  )
505
- _new_simplify = (
506
- "# pymeshlab_replaced_v1: replaced with trimesh (no py3.13 wheels for pymeshlab)\n"
507
- "def simplify_mesh(mesh: trimesh.Trimesh, n_faces):\n"
508
- " if mesh.faces.shape[0] > n_faces:\n"
509
- " mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, process=True)\n"
510
- " mesh = mesh.simplify_quadric_decimation(n_faces)\n"
511
- " return mesh\n"
512
- )
513
- if _old_simplify in _it_text:
514
- _it_text = _it_text.replace(_old_simplify, _new_simplify)
515
- _it_path.write_text(_it_text)
516
- print("[load_triposg] Patched inference_triposg.py: pymeshlab → trimesh simplify")
517
  else:
518
- print("[load_triposg] WARNING: could not patch inference_triposg.py (pattern not found)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
519
 
520
  weights_path = snapshot_download("VAST-AI/TripoSG")
521
 
 
476
  if _it_path.exists():
477
  _it_text = _it_path.read_text()
478
  if "pymeshlab_replaced_v1" not in _it_text:
479
+ # Step 1: always strip the top-level pymeshlab import
480
  _it_text = _it_text.replace("import pymeshlab\n", "")
481
+
482
+ # Step 2: try to replace the pymeshlab helper functions with trimesh equivalent
483
+ _new_simplify = (
484
+ "# pymeshlab_replaced_v1: replaced with trimesh (no py3.13 wheels for pymeshlab)\n"
485
+ "def simplify_mesh(mesh: trimesh.Trimesh, n_faces):\n"
486
+ " if mesh.faces.shape[0] > n_faces:\n"
487
+ " mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=mesh.faces, process=True)\n"
488
+ " mesh = mesh.simplify_quadric_decimation(n_faces)\n"
489
+ " return mesh\n"
490
+ )
491
+ # Try the exact upstream string first
492
+ _old_exact = (
493
  "def mesh_to_pymesh(vertices, faces):\n"
494
  " mesh = pymeshlab.Mesh(vertex_matrix=vertices, face_matrix=faces)\n"
495
  " ms = pymeshlab.MeshSet()\n"
 
512
  " else:\n"
513
  " return mesh\n"
514
  )
515
+ if _old_exact in _it_text:
516
+ _it_text = _it_text.replace(_old_exact, _new_simplify)
517
+ print("[load_triposg] Patched inference_triposg.py: pymeshlab → trimesh (exact match)")
 
 
 
 
 
 
 
 
 
518
  else:
519
+ # Fallback: regex handles whitespace/indent variants
520
+ import re as _re
521
+ _it_text = _re.sub(
522
+ r"def mesh_to_pymesh\(.*?\n.*?\n.*?\n.*?\n.*?\n\n\ndef pymesh_to_trimesh\(.*?\n.*?\n.*?\n.*?\n\n\ndef simplify_mesh\([^)]*\):[^\n]*\n(?: [^\n]*\n)*",
523
+ _new_simplify,
524
+ _it_text,
525
+ flags=_re.DOTALL,
526
+ )
527
+ print("[load_triposg] Patched inference_triposg.py: pymeshlab → trimesh (regex fallback)")
528
+
529
+ # Step 3: always write — import removal alone fixes the crash even if
530
+ # function replacement didn't match (simplify_mesh just won't exist,
531
+ # which is a NameError only if called, not at import time)
532
+ _it_path.write_text(_it_text)
533
+ print("[load_triposg] inference_triposg.py written.")
534
 
535
  weights_path = snapshot_download("VAST-AI/TripoSG")
536