Skip to content

Instantly share code, notes, and snippets.

@bohdon
Last active January 24, 2023 21:08
Show Gist options
  • Save bohdon/8f55552165cc1902457cfee5fb04a7f8 to your computer and use it in GitHub Desktop.
Save bohdon/8f55552165cc1902457cfee5fb04a7f8 to your computer and use it in GitHub Desktop.
Unlock normals on a mesh in Maya, but detect and preserve soft edges.
import pymel.core as pm
def is_soft_edge(edge):
vtx_faces = pm.polyListComponentConversion(edge, fromEdge=True, toVertexFace=True)
expanded_vtx_faces = pm.filterExpand(vtx_faces, expand=True, selectionMask=70)
vtx_faces_by_vtx = dict()
for vtx_face in expanded_vtx_faces:
vtx = pm.polyListComponentConversion(vtx_face, fromVertexFace=True, toVertex=True)
vtx_str = str(vtx)
if vtx_str not in vtx_faces_by_vtx:
vtx_faces_by_vtx[vtx_str] = []
vtx_faces_by_vtx[vtx_str].append(vtx_face)
for vtx, vtx_faces in vtx_faces_by_vtx.items():
first_normal = None
for vtx_face in vtx_faces:
normal = pm.polyNormalPerVertex(vtx_face, query=True, normalXYZ=True)
if first_normal is None:
first_normal = normal
elif normal != first_normal:
return False
return True
def unlock_normals_keep_soft_edges(shape):
soft_edges = [edge for edge in shape.edges if is_soft_edge(edge)]
pm.polyNormalPerVertex(shape, unFreezeNormal=True)
pm.polySoftEdge(soft_edges, angle=180)
# run unlock for all selected nodes
for s in pm.selected():
unlock_normals_keep_soft_edges(s.getShape())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment