Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MeshObjectUtility): convert quads to triangles #1127

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(MeshObjectUtility): convert quads to triangles
  • Loading branch information
MartinSmeyer committed Jul 8, 2024
commit df2d9acef563f9540f21eb405cc51d93e534ebcf
47 changes: 25 additions & 22 deletions blenderproc/python/types/MeshObjectUtility.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,28 +506,31 @@ def add_geometry_nodes(self):
modifier = self.blender_obj.modifiers[-1]
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)
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()

# 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])
# 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
Loading