Last active
January 24, 2023 21:08
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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