Jump to content

Vertex normal script for Maya


Recommended Posts

Hi everyone!

I just wanted to share a script I've done for Maya to quickly be able to set the vertex normals based on the face normals. I know there are a few ways to do this within native maya, as well as a few scripts, but I haven't been able to find one which averages the normals for verts shared by multiple selected faces. This can be quite annoying when setting up curved or round surfaces.


For 3ds max there's a great script called GetVertNormalFromFace which does this. What mine does is essentially the same but in Maya. Just make a face selection of the faces you want to set the vert normals for and run the script.


To run the script, just paste into the script editor as python and drag it to a shelf, or set it up as a hotkey

Here's the script:

# ---------------------------------------------------------------------------------------

# - 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()




    x_avg = (sum(normals[0]) / len(shaFaces))

    y_avg = (sum(normals[1]) / len(shaFaces))

    z_avg = (sum(normals[2]) / len(shaFaces))


    cmds.polyNormalPerVertex(xyz = (x_avg, y_avg, z_avg))

    normals[:] = [[], [], []]


I hope someone finds it useful :)

Edited by Pomperi
Link to comment
Share on other sites

Good boy Pontus. Look at you with all your fancy technical know-how.

I use this script all the time in 3ds max. If ever I make the dirty leap to Maya, I'll be sure to give this an install!

Link to comment
Share on other sites

Hehe, thanks guys :)


kikette: Glad you like it!


I'm currently trying to re-write the script in OpenMaya to make it a bit faster. Also trying to come up with a good way to maintain hard edges, as it's quite cumbersome whenever you want to keep hard edges intact when setting up the vertex normals for a mesh. I think the best way workflow-wise is to set up hard and soft edges before manipulating the vert normals (meaning you would simply soften the whole mesh if you don't want any hard edges) then run the script and wherever there's a split, the vert normal will be set for each face individually.


If anyone have any suggestions, that would be awesome!

Link to comment
Share on other sites

beautiful!! I have a script like this at work I use all the time :).

Maintaining hard edges is a problem, I always end up having to cut up my model into separate objects. There must be a better way..


damn, now I'm inspired to make my own version of this 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • MapCore on Discord

  • Our picks

    • Post in Yanzl's Source Emporium
      Two new asset releases!

      First up is Basalt. Sadly I can't release all of the assets as some of the nature stuff is from Megascans and can't be redistributed. Also only includes the assets I've made.


      Second one is assets I've made for Pitstop. A small release that includes some road and concrete trim textures, a bunch of sponsors and some skybox buildings that might be useful.


      As always, free for non-commercial use with attribution. 
  • Create New...