Man this is crazy bug! I tried it at first and it didn't work. Then I added the multiple calls as you did and it worked. I tried moving it around a bit and found that I could get it to work if I add another setNormal() at the end. This is on a linux 64 bit maya 8.5 sp1. I might try
setFaceVertexNormals if it will produce the results you are looking for. Ive also noticed in the past certain times working in the python maya api that if I did'nt wrap my code into a function or class the effect would not happen in maya until I manually "del()" the objects. I think a flaw in some of the swig maya api objects is they don't fully run until destruction. This did'nt work in this case but I have run into it a bit in the past.
Matt
import maya.cmds as cmds
import maya.mel as mel
import maya.OpenMaya as api
# make the sphere and get the MFn
mesh, h = cmds.polySphere( sa=10, sh=10 )
mel.eval( 'setPolygonDisplaySettings("vNormal")' )
mesh = cmds.listRelatives( mesh, s=1)[0]
sel = api.MSelectionList()
sel.add( mesh )
dag = api.MDagPath()
sel.getDagPath(0,dag)
meshFn = api.MFnMesh( dag )
# invert the vertex normals
normalArray = api.MFloatVectorArray()
newNormalArray = api.MFloatVectorArray()
meshFn.getNormals( normalArray, api.MSpace.kWorld )
for i in range( normalArray.length() ):
normal = normalArray[i]
newNormalArray.append( normal * -1 )
# set the new, inverted vertex normals
meshFn.setNormals( newNormalArray, api.MSpace.kWorld )
checkNormalArray = api.MFloatVectorArray()
meshFn.getNormals( checkNormalArray, api.MSpace.kWorld )
#Adding this here made it work with out multiple calls on my machine
#meshFn.setNormals( newNormalArray, api.MSpace.kWorld )
def angularDiff( arrayOne, arrayTwo ):
sumAngles = 0.0
for i in range( arrayOne.length() ):
newNormal = arrayOne[i]
checkNormal = arrayTwo[i]
sumAngles += abs( newNormal.angle( checkNormal ) )
print sumAngles
angularDiff( newNormalArray, checkNormalArray)
# 227.588285092
meshFn.setNormals( newNormalArray, api.MSpace.kWorld )
meshFn.getNormals( checkNormalArray, api.MSpace.kWorld )
angularDiff( newNormalArray, checkNormalArray)
#11.7161702135
meshFn.setNormals( newNormalArray, api.MSpace.kWorld )
meshFn.getNormals( checkNormalArray, api.MSpace.kWorld )
angularDiff( newNormalArray, checkNormalArray)
#5.86650015175
596.902620792
0.0
0.0