import maya.OpenMaya as om

selection=om.MSelectionList()
om.MGlobal.getActiveSelectionList(selection)

dagpath=om.MDagPath()
selection.getDagPath(0, dagpath)

faces=om.MItMeshPolygon(dagpath)
vertex=om.MItMeshVertex(dagpath)
meshfn=om.MFnMesh(dagpath)

normalvectors=om.MVectorArray()

previndex=om.MScriptUtil()
previndex.createFromInt(0)
previndexPtr=previndex.asIntPtr()

area=om.MScriptUtil()
area.createFromDouble(0.0)
areaPtr=area.asDoublePtr()

vertexperpoly = om.MIntArray()
vertexlist = om.MIntArray()

skipvertex = []

while not faces.isDone():
    vertexperpoly.append(faces.polygonVertexCount())
    faces.next()
meshfn.getVertices(vertexperpoly,vertexlist)
faces.reset()

for j in xrange(faces.count()):
    faces.setIndex(j, previndexPtr)
    
    n = om.MVector()
    faces.getNormal(n, om.MSpace.kWorld)

    for i in xrange(faces.polygonVertexCount()):
        facepoints=om.MPointArray()
        faces.getPoints(facepoints, om.MSpace.kWorld)
        v1 = None
        v2 = None
        if i == 0:
            v1=facepoints[i+1]-facepoints[i]
            v2=facepoints[i+2]-facepoints[i]
        else:
            v1=facepoints[i-1]-facepoints[i]
            v2=facepoints[i+2]-facepoints[i]
        angle = v1.angle(v2)
        currentvertex=faces.vertexIndex(i)
        if currentvertex in skipvertex:
            continue
        vertex.setIndex(currentvertex, previndexPtr)
        
        hoodfaces=om.MIntArray()
        vertex.getConnectedFaces(hoodfaces)
        
        for f in hoodfaces:
            if f == j:
                continue
            faces.setIndex(f, previndexPtr)
            currentnormal = om.MVector()
            faces.getArea(areaPtr, om.MSpace.kWorld)
            faces.getNormal(currentnormal, om.MSpace.kWorld)
            
            scalar = (area.getDouble(areaPtr)) * angle
            n += currentnormal * scalar
        n.normalize() #normalize vertex normal
        normalvectors.append(n) #append to normal vector array
        skipvertex.append(currentvertex)
meshfn.setVertexNormals(normalvectors,vertexlist,om.MSpace.kWorld)

I get this :
// Error: (kInvalidParameter): No element at given index 

# Traceback (most recent call last):

# File "<maya console>", line 52, in <module>

# File 
"S:\Maya_2017_DI\build\Release\runTime\Python\Lib\site-packages\maya\OpenMaya.py",
 
line 8714, in vertexIndex

# RuntimeError: (kInvalidParameter): No element at given index //


the MItMeshPolygon.vertexIndex fails , but it shouldn't :'(


my code is based on a trick with face relative index range (0 - (face vert 
count-1))


and this method would've allow me to get globel space index to set it for 
vertex iterator (MItMeshVertex) right after

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/856158c9-063f-4bc3-83b8-346a3b9a153c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to