Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Last active January 20, 2023 15:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRoy/a7d08ae250b10b4f9c1ebaed717944f0 to your computer and use it in GitHub Desktop.
Save BigRoy/a7d08ae250b10b4f9c1ebaed717944f0 to your computer and use it in GitHub Desktop.
In maya get all zero or near zero face vertex normals of a mesh using Python
import maya.api.OpenMaya as om
from maya import cmds
def get_dag_path(name):
"""Return MDagPath from object name"""
sel = om.MSelectionList()
sel.add(name)
return sel.getDagPath(0)
def get_mesh_zero_normals(mesh, tolerance=1e-10):
"""Return mesh face vertices of which the normal is zero.
Args:
mesh (str): Name of mesh node.
tolerance (float): Tolerance for zero comparison.
Returns:
list: Individual .vtxFace components which have zero
normal vectors
"""
dag = get_dag_path(mesh)
it = om.MItMeshFaceVertex(dag)
result = om.MSelectionList()
while not it.isDone():
normal = it.getNormal()
if normal.isEquivalent(om.MVector.kZeroVector, tolerance):
# Zero or near zero normal detected
result.add((dag, it.currentItem()))
it.next()
return list(result.getSelectionStrings())
if __name__ == "__main__":
# Example with all normals zero
transform, construction_history = cmds.polyCube()
mesh = cmds.listRelatives(transform, type="mesh", fullPath=True)[0]
cmds.polyNormalPerVertex(mesh, normalXYZ=[0, 0, 0])
zero_normal_components = get_mesh_zero_normals(mesh)
print(zero_normal_components)
# Example with a single face vertex normal zero
transform, construction_history = cmds.polyCube()
mesh = cmds.listRelatives(transform, type="mesh", fullPath=True)[0]
cmds.polyNormalPerVertex(mesh + ".vtxFace[0][0]", normalXYZ=[0, 0, 0])
zero_normal_components = get_mesh_zero_normals(mesh)
print(zero_normal_components)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment