Spaces:
Sleeping
Sleeping
| import trimesh | |
| import numpy as np | |
| import os | |
| def to_double_sided_gray_obj(mesh_path, out_path, gray=180): | |
| print(f"Processing: {mesh_path}") | |
| if not os.path.exists(mesh_path): | |
| print(f"File not found: {mesh_path}") | |
| return | |
| # Load with process=True to cleanup | |
| m = trimesh.load(mesh_path, process=True, force='mesh') | |
| # Handle Scene | |
| if isinstance(m, trimesh.Scene): | |
| if len(m.geometry) == 0: | |
| print("Empty scene") | |
| return | |
| m = trimesh.util.concatenate(tuple(m.geometry.values())) | |
| # 1. Fix normals on original | |
| m.fix_normals() | |
| # 2. Duplicate and flip for backfaces | |
| m_back = m.copy() | |
| m_back.faces = np.fliplr(m_back.faces) | |
| m_back.fix_normals() | |
| # 3. Combine | |
| m_combined = trimesh.util.concatenate([m, m_back]) | |
| # 4. Color | |
| vc = np.tile(np.array([gray, gray, gray, 255], dtype=np.uint8), (len(m_combined.vertices), 1)) | |
| m_combined.visual = trimesh.visual.ColorVisuals(mesh=m_combined, vertex_colors=vc) | |
| # 5. Export | |
| m_combined.export(out_path) | |
| print(f"Saved double-sided mesh to: {out_path}") | |
| return m_combined | |
| # Test on the reported problematic mesh | |
| mesh_path = "./ours_meshes/treehouse_rmapple.glb" | |
| out_path = "./debug_treehouse_double_sided.glb" | |
| # Run the fix | |
| mesh = to_double_sided_gray_obj(mesh_path, out_path) | |
| # If running in a notebook, this might show the viewer: | |
| if mesh: | |
| try: | |
| mesh.show() | |
| except: | |
| pass | |