Skip to content

Instantly share code, notes, and snippets.

@SEVEZ
Created August 29, 2015 11:23
Show Gist options
  • Save SEVEZ/84030959a239ebe7f0c5 to your computer and use it in GitHub Desktop.
Save SEVEZ/84030959a239ebe7f0c5 to your computer and use it in GitHub Desktop.
Sets the vertex normals of a face selection, averaging normals shared by multiple selected faces.
# ---------------------------------------------------------------------------------------
# - pompVertNormFaceAvg
#
# - Sets the vertex normals of a face selection,
# averaging normals shared by multiple selected faces.
#
#
# - by Pontus Karlsson 2013 (www.pomperi.com)
# ---------------------------------------------------------------------------------------
import maya.cmds as cmds
faces = cmds.filterExpand(sm=34, ex=True)
verts = cmds.ls((cmds.polyListComponentConversion(faces, ff=True, tv=True)), flatten=True)
normals = [[], [], []]
for v in verts:
conFaces = cmds.ls(cmds.polyListComponentConversion(v, fv=True, tf=True), flatten=True)
shaFaces = list(set(conFaces).intersection(set(faces)))
faceNorm = cmds.polyInfo(shaFaces, fn=True)
for normal in faceNorm:
label, vertex, x, y, z = normal.split()
normals[0].append(float(x))
normals[1].append(float(y))
normals[2].append(float(z))
x_avg = (sum(normals[0]) / len(shaFaces))
y_avg = (sum(normals[1]) / len(shaFaces))
z_avg = (sum(normals[2]) / len(shaFaces))
cmds.select(v)
cmds.polyNormalPerVertex(xyz = (x_avg, y_avg, z_avg))
normals[:] = [[], [], []]
cmds.select(cl=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment