api: functions sets vs. iterators

124 views
Skip to first unread message

kevco...@gmail.com

unread,
Sep 21, 2014, 3:13:24 PM9/21/14
to python_in...@googlegroups.com
Hey guys,

I'm probably a little in over my head, but I'm starting to get familiar with the api, and I'm really not grasping the difference between function sets and iterators. They seem to be used in much the same way. could someone please give me some basic examples that help describe their differences, and how to know when you need one vs the other.

Thanks
Kev

Justin Israel

unread,
Sep 21, 2014, 3:32:34 PM9/21/14
to python_in...@googlegroups.com

Hey Kev,

The official docs describe the API structure in detail, but in a nutshell...

The basic object is an opaque handle, like an identifier, that can't do much of anything other than represent itself in memory.
Then you have function sets which are decoupled from the object handles and group together functions for specific types or purposes. Such as working with a handle for a mesh, or a curve. You attach an object to the function set, and then you can operate on it.
Iterators do just that one specific thing. They are type specific ways to loop over something that is a container for something else. Such as the selections in a selection list, or verts, or CVs.

Keep in mind that the python API (v1) is a very direct binding to its C++ counterpart. So it would be natural for the concepts in that api to seem a bit strange to a pure python programmer.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/edac5634-3c96-481c-9892-03ee52767adf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kevco...@gmail.com

unread,
Sep 22, 2014, 12:07:15 PM9/22/14
to python_in...@googlegroups.com
Hey Justin, thx for replying.

I think it's mainly just the iterators that I'm not really clear on. The docs seem to hit on the function sets, but I'm not really finding anything that explains iterators in detail, maybe Im missing it?

so would you use a function set to access points of a polymesh, then use an iterator to iterate through each point? is there a common direct useage between the function sets & iterators? or is it just case by case?

anyhow. I'm working with the 2.0 python api right now, since I just noticed yesterday that quite a few more classes were added with sp4.

With the code below, which would be the proper way to write it? I feel like the first way is best. what does the iterator really contribute? speed? am I even using the iter correctly? ;)

#=======================================
import maya.api.OpenMaya as om2


selList = om2.MSelectionList( om2.MGlobal.getActiveSelectionList() )
dagList = om2.MSelectionList()
for i in range( selList.length() ):
dagList.add( selList.getDagPath(i) )
print dagList

#=======

selList = om2.MSelectionList( om2.MGlobal.getActiveSelectionList() )
selIter = om2.MItSelectionList( selList )
dagList = om2.MSelectionList()
while not selIter.isDone():
dagList.add( selIter.getDagPath() )
selIter.next()
print dagList

#=======================================

Thanks!
Kev

Paul Molodowitch

unread,
Sep 22, 2014, 1:34:57 PM9/22/14
to python_inside_maya
Ok... well, I can now see why you're confused about the need for MIt* types, if your primary point of reference is MSelectionList vs. MItSelectionList.  For most MIt*s, there either isn't another class that allows you to iterate through all objects of that type (ie, MItDependencyGraph), or it provides many methods / ways of querying information that isn't available through other function sets (ie, MItMesh*). 

 The line is a bit blurred with MSelectionList / MItSelectionList, I feel like.

It's made even more confusing because of two outstanding bugs / limitations with MSelectionList / MItSelectionList, that frequently mean you need to use BOTH. From the docs, http://help.autodesk.com/view/MAYAUL/2015/ENU/?guid=__files_Appendices_Appendix_E_API_and_Devkit_limitations_htm (which ALSO seem to have a bug - I have to browse through the contents tree, "Maya Developer Help" > "Appendices" > "Appendix D: API and Devkit limitations", in order to get the page to show up - maybe because the index says "D" but they page title is "E"?):

Selecting multiple types of object components

When multiple types of object components are selected, e.g. edges and vertices, an MSelectionList that has been assigned the active selection list will contain only the final type of component selected. For example if edges were selected and then vertices were shift-selected the MSelectionList representing the active selection list will contain one MObject representing the vertex components.

Workaround

MItSelectionList list will differentiate between the different components selected and can be used to identify all of the various components selected.

MItSelectionList::getDagPath()

MItSelectionList::getDagPath() will always return the original dagPath of an instanced node. For example, if two shape nodes are selected and their dagPaths are:

pCube1|pCubeShape1
pCube2
pCube2|pCubeShape1

getDagPath() will return:

pCube1|pCubeShape1
pCube1
pCube1|pCubeShape1

for each object in the list.

Workaround

MSelectionList::getDagPath() will return the correct dagPath for each object in the list.


So - the summary is, if you only want the dag paths, you'd best use MSelectionList... but if you want components, it's better to use MItSelectionList... and if you want BOTH, then you have to use both.  (Lame, I know!)



Kev

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages