Maya API in Python - MItMeshPolygon.getIndex()


#1

Argh, i cannot get this method to work!
Code:


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

scriptUtil = maya.OpenMaya.MScriptUtil()
scriptUtil.createFromInt(0)
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\OpenMaya.py”, 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!


#2

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.


#3

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.


#4

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()
area.createFromDouble(0.0)
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.


#5

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

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