fix(sim): Fix .obj mesh compose issue. (#46)

This commit is contained in:
Xinjie 2025-10-02 16:31:59 +08:00 committed by GitHub
parent 6a750a37be
commit 0fb691bd22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 11 deletions

View File

@ -45,16 +45,23 @@ def decompose_convex_coacd(
mesh = coacd.Mesh(mesh.vertices, mesh.faces) mesh = coacd.Mesh(mesh.vertices, mesh.faces)
result = coacd.run_coacd(mesh, **params) result = coacd.run_coacd(mesh, **params)
combined = sum([trimesh.Trimesh(*m) for m in result])
meshes = []
for v, f in result:
meshes.append(trimesh.Trimesh(v, f))
# Compute collision_scale because convex decomposition usually makes the mesh larger. # Compute collision_scale because convex decomposition usually makes the mesh larger.
if auto_scale: if auto_scale:
convex_mesh_shape = np.ptp(combined.vertices, axis=0) all_mesh = sum([trimesh.Trimesh(*m) for m in result])
convex_mesh_shape = np.ptp(all_mesh.vertices, axis=0)
visual_mesh_shape = np.ptp(mesh.vertices, axis=0) visual_mesh_shape = np.ptp(mesh.vertices, axis=0)
rescale = visual_mesh_shape / convex_mesh_shape scale_factor *= visual_mesh_shape / convex_mesh_shape
combined.vertices *= rescale
combined = trimesh.Scene()
for mesh_part in meshes:
mesh_part.vertices *= scale_factor
combined.add_geometry(mesh_part)
combined.vertices *= scale_factor
combined.export(outfile) combined.export(outfile)

View File

@ -282,16 +282,12 @@ class URDFGenerator(object):
d_params = dict( d_params = dict(
threshold=0.05, max_convex_hull=100, verbose=False threshold=0.05, max_convex_hull=100, verbose=False
) )
filename = f"{os.path.splitext(obj_name)[0]}_collision.ply" filename = f"{os.path.splitext(obj_name)[0]}_collision.obj"
output_path = os.path.join(mesh_folder, filename) output_path = os.path.join(mesh_folder, filename)
decompose_convex_mesh( decompose_convex_mesh(
mesh_output_path, output_path, **d_params mesh_output_path, output_path, **d_params
) )
obj_filename = filename.replace(".ply", ".obj") collision_mesh = f"{self.output_mesh_dir}/{filename}"
trimesh.load(output_path).export(
f"{mesh_folder}/{obj_filename}"
)
collision_mesh = f"{self.output_mesh_dir}/{obj_filename}"
except Exception as e: except Exception as e:
logger.warning( logger.warning(
f"Convex decomposition failed for {output_path}, {e}." f"Convex decomposition failed for {output_path}, {e}."