|
Hi,
Using a simple iterator class in python I would like to use a for next loop. I really hate using a while loop, as eventually I will forget to type “stuff.next()” in the while
part and maya will implode forever.
This code prints out the items that are currently selected.
import maya.OpenMaya as OM
#Assign all selected objects to a selection list list = OM.MSelectionList()
OM.MGlobal_getActiveSelectionList(list)
stuff = OM.MItSelectionList(list)
while not stuff.isDone():
dpath= OM.MDagPath()
stuff.getDagPath(dpath)
print dpath.partialPathName()
stuff.next()
Under the python documentation it seems that I should be able to loop through
iterators like this..
for item in iter(stuff):
This returns the error
# TypeError: ‘MItSelectionList’ object is not callable #
Is there any general way to use iterator API functions in python without while loops?
|
|
|
|
It’s not a python iterator, so the best you can do is
wrap it in one:
def mIter(mit):
while not mit.isDone():
yield mit
mit.next()
list = OM.MSelectionList()
OM.MGlobal_getActiveSelectionList(list)
for item in mIter(OM.MItSelectionList(list)):
dpath= OM.MDagPath()
item.getDagPath(dpath)
print dpath.partialPathName()
|
|
|
|
Thanks for your response. That reduces potentially lots of “while loops” to one.
My problem formed because I looked in the iterator class for a function that returns the number of elements in the list. Originally I was using MItMeshPolygon and it had a
count() function so I had a for loop. But then I found the most used Iterator function MItSelectionList
had no function that returned the number of members.
BUT I found that MSelectionList DOES!!!
import maya.OpenMaya as OM
#Assign all selected objects to a selection list list = OM.MSelectionList()
OM.MGlobal_getActiveSelectionList(list)
stuff = OM.MItSelectionList(list)
for i in range(0,list.length()):
dpath= OM.MDagPath()
stuff.getDagPath(dpath)
print dpath.partialPathName()
stuff.next()
There still is the possibility of putting the .next() on the wrong space. But no infinite loops. It would just repeat the same functions and stop.
YEAH!!!
|
|
|