Clean up a mesh using trimesh.
(mesh)
| 47 | |
| 48 | |
| 49 | def clean_mesh(mesh): |
| 50 | """Clean up a mesh using trimesh.""" |
| 51 | print(f" Original: {len(mesh.vertices)} vertices, {len(mesh.faces)} faces") |
| 52 | |
| 53 | mesh.merge_vertices(merge_tex=False, merge_norm=False) |
| 54 | mesh.remove_unreferenced_vertices() |
| 55 | try: |
| 56 | import networkx # noqa: F401 - trimesh's fix_normals requires it |
| 57 | mesh.fix_normals() |
| 58 | except ImportError: |
| 59 | print(" Warning: networkx not installed, skipping fix_normals (GLB may have lighting issues)") |
| 60 | |
| 61 | # Rotate -90 degrees around X for GLTF Y-up -> Unreal Z-up |
| 62 | rotation_matrix = trimesh.transformations.rotation_matrix(-np.radians(90), [1, 0, 0]) |
| 63 | mesh.apply_transform(rotation_matrix) |
| 64 | |
| 65 | print(f" Cleaned: {len(mesh.vertices)} vertices, {len(mesh.faces)} faces") |
| 66 | return mesh |
| 67 | |
| 68 | |
| 69 | def convert_mesh(input_path: Path, output_path: Path) -> bool: |