(Python) How to reorient mesh normal to point outwards #4821
-
Hello, I have a series of mesh objects that I am trying to union in Python but some of them have inconsistent normals directions like the mesh pictured below. I want to reorient the normals so that they all point outwards, that is, so that they all appear blue. Can someone please explain how I can achieve this? By the way, I noticed in Meshlib v3.0.6.229 that such disorientations can now be detected and rectified but I cannot work out from the documentation how to do it. ![]() Many thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello! This case is kind of complex because inconsistently oriented parts are joined together and sometimes forms self-intersections and overlaps. You can try code like this from meshlib import mrmeshpy as mm
mesh = mm.loadMesh(__file__+"/../18.stl")
# find disoriented faces
disorientedFaces = mm.findDisorientedFaces(mesh)
# extract them into separate mesh and flip
separateDisorientedPart = mm.Mesh()
separateDisorientedPart.addMeshPart(mm.MeshPart(mesh,disorientedFaces))
separateDisorientedPart.topology.flipOrientation()
# delete disoriented faces
mesh.deleteFaces(disorientedFaces)
# join with fliped disorieted faces (they will be joined but not connected)
mesh.addMesh(separateDisorientedPart)
# rebuild mesh with corrected disorientation to connect all together and fix issues
rSettings = mm.RebuildMeshSettings()
rSettings.voxelSize = mm.suggestVoxelSize(mesh,1e8)
mesh = mm.rebuildMesh(mesh,rSettings)
# optionnaly fix small components
mesh.deleteFaces(mesh.topology.getValidFaces() - mm.MeshComponents.getLargeByAreaComponents(mesh,mesh.area()*0.002,None))
mm.saveMesh(mesh,__file__+"/../res_mesh.stl") |
Beta Was this translation helpful? Give feedback.
Hello!
This case is kind of complex because inconsistently oriented parts are joined together and sometimes forms self-intersections and overlaps.
You can try code like this