Maya API in Python - MItMeshPolygon.getIndex()


Argh, i cannot get this method to work!

polygonIter = maya.OpenMaya.MItMeshPolygon(myObject)
faceIndex = 0

scriptUtil = maya.OpenMaya.MScriptUtil()
dummyIndex = scriptUtil.asIntPtr()

polygonIter.setIndex(faceIndex, dummyIndex)

And the error:

polygonIter.setIndex(faceIndex, dummyIndex)

File “D:\buildforge\Maya_2011_Win64_Build\build\wrk\optim\runTime\Python\Lib\site-packages\maya\”, line 6367, in setIndex

TypeError: in method ‘MItMeshPolygon_setIndex’, argument 2 of type ‘int’

The parameter docs say this:
MStatus MItMeshPolygon::setIndex(int index, int & prevIndex)

[in] index The index of desired face to access.
[in] prevIndex The index of the current face in the iteration

Why prevIndex is needed is beyond me, and why its an int & is also baffling…

Any ideas? Cheers!


Your code executes for me pretty much as written (I subbed in my own MObject, obviously).

That makes me think that your faceIndex is getting messed with, somehow. Either you haven’t supplied us with all the code or there’s some background script running that may have changed its value.


Cheers for testing this Bronwen.
Well, the only other code of relevance really is turning my polygonal object into an MObject. Is your MObject a component or something like that?

I found a workaround, which essentially just uses an iterator to loop through all the object’s faces, and ignoring those indices which i don’t care about.


I’ve had to do some setIndex of my own not too long ago, and had to deal with similar issues trying to wrap all that MScriptUtil nonsense. While I can’t really comment as to why your code is not working, here’s what ended up working for me, hopefully it helps you sorting out your problem.
Only real difference I can see is the MItMeshPolygon constructor we are using.

comp = api.MObject()

area = api.MScriptUtil()
areaPtr = area.asDoublePtr()
dummy = api.MScriptUtil()

currentFace = api.MItMeshPolygon( self.dagPath, comp )
currentFace.setIndex(self.faceID, dummy.asIntPtr())
currentFace.getArea(areaPtr,api.MSpace.kWorld )

self.dagPath & self.faceID are variables created somewhere else matching a specific object and a face ID in this object.


polygonIter = om.MItMeshPolygon(meshObj)
util = om.MScriptUtil()
dummyIndexPtr = util.asIntPtr()
faceIdx = 0
polygonIter.setIndex(faceIdx, dummyIndexPtr )

The dummy index should be the current state of the iterator.