Remember 3December
Find out where to celebrate your 3D CG art...
  • 1/3
You are here:Forum Home / Autodesk® Maya® / Python / Python iteration with API?
  RSS 2.0 ATOM  

Python iteration with API?
Rate this thread
 
36301
 
Subscribe to this thread
avatar
  • Total Posts: 6
  • Joined: 2008-04-09 04:32:12

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():
      
   
dpathOM.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?



Replies: 0
avatar
  • cgrebeld
  • Posted: 06 November 2009 08:40 PM
  • Total Posts: 3
  • Joined: 2009-03-11 22:11:26

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)):
    
dpathOM.MDagPath()      
    item
.getDagPath(dpath)
    
print dpath.partialPathName()


Replies: 0
avatar
  • Total Posts: 6
  • Joined: 2008-04-09 04:32:12

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()):
      
   
dpathOM.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!!!



Replies: 0




   
  Settings Choose Theme color: