Skip to content

Commit

Permalink
Merge pull request #1127 from DLR-RM/iss_1126_convert_quads_to_triang…
Browse files Browse the repository at this point in the history
…les_in_trimesh_conversion

feat(MeshObjectUtility): convert quads to triangles
  • Loading branch information
cornerfarmer authored Jul 8, 2024
2 parents 0152cd9 + 8e9ef02 commit 610aa3f
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions blenderproc/python/types/MeshObjectUtility.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,27 +507,32 @@ def add_geometry_nodes(self):
return modifier.node_group

def mesh_as_trimesh(self) -> Trimesh:
""" Returns a trimesh.Trimesh instance of the MeshObject.
:return: The object as trimesh.Trimesh.
"""
# get mesh data
mesh = self.get_mesh()

# get vertices
verts = np.array([[v.co[0], v.co[1], v.co[2]] for v in mesh.vertices])

# check if faces are pure tris or quads
if not all(len(f.vertices[:]) == len(mesh.polygons[0].vertices[:]) for f in mesh.polygons):
raise Exception(f"The mesh {self.get_name()} must have pure triangular or pure quad faces")

# re-scale the vertices since scale operations doesn't apply to the mesh data
verts *= self.blender_obj.scale

# get faces
faces = np.array([f.vertices[:] for f in mesh.polygons if len(f.vertices[:]) in [3, 4]])

return Trimesh(vertices=verts, faces=faces)
""" Returns a trimesh.Trimesh instance of the MeshObject.
:return: The object as trimesh.Trimesh.
"""

# get mesh data
mesh = self.get_mesh()

# check if faces are pure tris or quads and triangulate quads if this is not the case
if not all(len(f.vertices[:]) == len(mesh.polygons[0].vertices[:]) for f in mesh.polygons):
# Triangulate quads
self.select()
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.quads_convert_to_tris(quad_method='FIXED', ngon_method='BEAUTY')
bpy.ops.object.mode_set(mode='OBJECT')
self.deselect()

# get vertices
verts = np.array([[v.co[0], v.co[1], v.co[2]] for v in mesh.vertices])
# re-scale the vertices since scale operations doesn't apply to the mesh data
verts *= self.blender_obj.scale
# get faces
faces = np.array([f.vertices[:] for f in mesh.polygons if len(f.vertices[:]) in [3, 4]])

return Trimesh(vertices=verts, faces=faces)

def create_from_blender_mesh(blender_mesh: bpy.types.Mesh, object_name: str = None) -> "MeshObject":
""" Creates a new Mesh object using the given blender mesh.
Expand Down

0 comments on commit 610aa3f

Please sign in to comment.