简体   繁体   中英

How do you use an Iterator without that has no __iter__ method?

While trying some basic programming in Maya I came across the "MitMeshPolygon" method. Which was described as: "This class is the iterator for polygonal surfaces (meshes)."

( https://help.autodesk.com/view/MAYAUL/2016/ENU/?guid=__py_ref_class_open_maya_1_1_m_it_mesh_polygon_html )

This seemed to be what I needed, so I looked into iterators more and from what I can tell, iterators should themselves be iterable, and have a iter method. However, when trying to iterate over it python told me it wasn't iterable.

        iterable = OpenMaya.MItMeshPolygon(dagPaths[0])            
        while not iterable.isDone():
            print(iterable)
            print(dir(iterable))

<maya.OpenMaya.MItMeshPolygon; proxy of <Swig Object of type 'MItMeshPolygon *' at 0x0000021A9159D630> >

[' class ', ' delattr ', ' dict ', ' dir ', ' doc ', ' eq ', ' format ', ' ge ', ' getattribute ', ' gt ', ' hash ', ' init ', ' init_subclass ', ' le ', ' lt ', ' module ', ' ne ', ' new ', ' next ', ' reduce ', ' reduce_ex ', ' repr ', ' setattr ', ' sizeof ', ' str ', ' subclasshook ', ' swig_destroy ', ' weakref ', 'center', 'className', 'count', 'currentItem', 'geomChanged', 'getArea', 'getAxisAtUV', 'getColor', 'getColorIndex', 'getColorIndices', 'getColors', 'getConnectedEdges', 'getConnectedFaces', 'getConnectedVertices', 'getEdges', 'getNormal', 'getNormals', 'getPointAtUV', 'getPoints', 'getTriangle', 'getTriangles', 'getUV', 'getUVArea', 'getUVAtPoint', 'getUVIndex', 'getUVSetNames', 'getUVs', 'getVertices', 'hasColor', 'hasUVs', 'hasValidTriangulation', 'index', 'isConnectedToEdge', 'isConnectedToFace', 'isConnectedToVertex', 'isConvex', 'isDone', 'isHoled', 'isLamina', 'isPlanar', 'isStarlike', 'isUVReversed', 'nex t', 'normalIndex', 'numColors', 'numConnectedEdges', 'numConnectedFaces', 'numTriangles', 'onBoundary', 'point', 'polygon', 'polygonVertexCount', 'reset', 'setIndex', 'setPoint', 'setPoints', 'setUV', 'setUVs', 'tangentIndex', 'this', 'thisown', 'updateSurface', 'vertexIndex', 'zeroArea', 'zeroUVArea']

I want to iterate through all the polygons and "getArea", but right now I have no idea how to do it, everything I read tells me iterators should be iterable, but the program tells me otherwise. What am I not understanding here? How do I use these methods to get specific information concerning the object indicated by the DagPath?

Your iterable has a __next__ method. You may be able to do something like the following:

iterator = OpenMaya.MItMeshPolygon(dagPaths[0])            
try:
    while True:
        item = next(iterator)
        ...
except StopIteration:
    pass

Note that I have renamed the variable to iterator , since that appears to be the slightly more correct term. An iterable should indeed have an __iter__ method, and calling iter(iterable) turns it into an iterator that does the actual iteration, and has a __next__ method.

In fact, iterator may actually be a generator, yielding values on the go (using next() ), and you wouldn't be able to access randomly located values (with, for example, indexing), only one after the other. Then you only need a __next()__ method. But without seeing the actual code for MitMeshPolygon , that is a guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM