A lot of the Maya API classes are not iterable, whereas there is no
reason they shouldn't.
It's easy to fix by adding them __len__ and __iter__ methods when
necessary. That's what the datatypes module in pymel does for MVector,
MMatrix and related :
import pymel.api as _api
# patch some Maya api classes that miss __iter__ to make them iterable /
convertible to list
def _patchMVector() :
def __len__(self):
""" Number of components in the Maya api Vector, ie 3 """
return 3
type.__setattr__(_api.MVector, '__len__', __len__)
def __iter__(self):
""" Iterates on all components of a Maya api Vector """
for i in xrange(len(self)) :
yield _api.MVector.__getitem__(self, i)
type.__setattr__(_api.MVector, '__iter__', __iter__)
def _patchMMatrix() :
def __len__(self):
""" Number of rows in the Maya api Matrix, ie 4.
Not to be confused with the number of components (16) given
by the size method """
return 4
type.__setattr__(_api.MMatrix, '__len__', __len__)
def __iter__(self):
""" Iterates on all 4 rows of a Maya api Matrix """
for r in xrange(4) :
yield
Array([_api.MScriptUtil.getDoubleArrayItem(_api.MMatrix.__getitem__(self,
r), c) for c in xrange(4)])
type.__setattr__(_api.MMatrix, '__iter__', __iter__)
When I last checked, seemed about all Maya API array types need that
checking and possibly patching (has the added advantage of being able to
hide MScriptUtil calls too)
Olivier
chadrik wrote:
> iterating through iterables is crazy talk!
>
> (pass it on: http://code.google.com/p/pymel/wiki/PythonAPI)
>
> -chad
>
>
> On Oct 1, 2009, at 4:26 PM, Paul Molodowitch wrote:
>
>
>> Some array types are iterable - MDoubleArray, for instance. And,
>> really, since they've implemented __getitem__, all they really need is
>> __len__ - which would just be a straight wrap of length() - and it
>> would work beautifully...
>>
>> I guess I should just resign myself to always writing c-code in python
>> when it comes to the API... but every so often, I try to do something
>> pythonic, and it works, and I get all excited, and start trying other
>> crazy things!
>>
>> - Paul
>>
>> On Thu, Oct 1, 2009 at 3:41 PM, Chad Vernon <[email protected]>
>> wrote:
>>
>>> You can't iterate API array types like normal Python iterables.
>>> You have to do:
>>>
>>> for dag in range(dagArray.length()):
>>> # blah
>>>
>>> On Thu, Oct 1, 2009 at 3:38 PM, barnabas79 <[email protected]>
>>> wrote:
>>>
>>>> The subject says it all...
>>>>
>>>> import maya.OpenMaya as om
>>>> sel = om.MSelectionList()
>>>> sel.append('persp')
>>>> sel.add('persp')
>>>> dag = om.MDagPath()
>>>> comp = om.MObject()
>>>> sel.getDagPath(0, dag, comp)
>>>> dagArray = om.MDagPathArray()
>>>> dagArray.append(dag)
>>>> dagArray.append(dag)
>>>>
>>>> numElements = 0
>>>> for dag in dagArray:
>>>> numElements += 1
>>>> if numElements > 10:
>>>> print "Oh noes!"
>>>> break
>>>>
>>>>
>>>
>
>
> >
>
>
--
Olivier Renouard
--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/python_inside_maya
-~----------~----~----~----~------~----~------~--~---