How to select with API and undo?

68 views
Skip to first unread message

Michael Boon

unread,
Apr 9, 2018, 12:32:12 AM4/9/18
to Python Programming for Autodesk Maya
Hi,

If I change the selection using the API, for example:
import maya.api.OpenMaya as om
# meshFn is a MFnMesh attached to an object in the scene
# faceIds is a MIntArray
compFn = om.MFnSingleIndexedComponent()
compObj
= compFn.create(om.MFn.kMeshPolygonComponent)
compFn
.addElements(faceIds)
sList
= om.MSelectionList()
sList
.add((meshFn.getPath(), compObj))
om.MGlobal.setActiveSelectionList(sList)

How do I make that play nicely with undo? Currently it doesn't affect the undo stack at all, so undoing undoes whatever happened before it, and redoing doesn't redo it, so the selection ends up wrong.

MDGModifier doesn't seem to be relevant to selection.

Thanks,

Boon

Michael Boon

unread,
Apr 9, 2018, 1:12:03 AM4/9/18
to Python Programming for Autodesk Maya
It looks like the appropriate C++ function is MGlobal::selectCommand, but that isn't available in Python.

Michael Boon

unread,
Apr 9, 2018, 2:48:44 AM4/9/18
to Python Programming for Autodesk Maya
So far, my best idea is to use PyMel:
import pymel.core as pm
pmMesh
= pm.PyNode(meshFn.fullPathName())
pmFaces
= pm.MeshFace(pmMesh, faceIds)
pm
.select(pmFaces)

justin hidair

unread,
Apr 9, 2018, 12:52:45 PM4/9/18
to Python Programming for Autodesk Maya
how about dat ? It requires a command tho .

import maya.api.OpenMaya as om


''' following the declaration of a class for a command '''

def doIt(self):

    compFn
= om.MFnSingleIndexedComponent()
    compObj
= compFn.create(om.MFn.kMeshPolygonComponent)
    compFn
.addElements(faceIds)
    sList
= om.MSelectionList()
    sList
.add((meshFn.getPath(), compObj))
    om
.MGlobal.setActiveSelectionList(sList)

def undoIt(self):
   
#add cached previous selection
   
if not len(previous_cached):
       
#
        sList
= om.MSelectionList()
       
        om
.MGlobal.setActiveSelectionList(sList)
   
else:
       
#

        compFn
= om.MFnSingleIndexedComponent()
        compObj
= compFn.create(om.MFn.kMeshPolygonComponent)

        compFn
.addElements(previous_cached)

Michael Boon

unread,
Apr 9, 2018, 7:52:43 PM4/9/18
to Python Programming for Autodesk Maya
Yeah I was worried that might be the only way to do it without PyMel or cmds. I imagine there are cases where the PyMel solution would be slow enough to warrant writing a plugin command like that.

Marcus Ottosson

unread,
Apr 9, 2018, 8:28:54 PM4/9/18
to python_in...@googlegroups.com

Hey Michael,

There was a thread recently about the API and undo.

My takeaway is that you’re exactly right; there is few (if any) ways of integrating with Maya’s undo other than implementing a command plug-in. In the thread, I shared a project to simplify doing that, but at the end of the day a command plug-in is what it is.

My approach is to use the API for (1) performance sensitive tasks that include (2) queries and (3) bulk operations. Queries with cmds or PyMEL can take an order of magnitude longer, and obviously don’t need undoing. Bulk operations on the other hand can require undo, but may sometimes get away with not bothering with it. Such as building a rig, where it may be ok to either delete the resulting hierarchy yourself, or start anew if it’s happening on a headless Maya or the like.

For anything interactive, like scene selection, cmds or PyMEL may suffice, and for times it doesn’t then the apiundo project has filled the remaining gaps on my end.


--
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_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/571f4266-faf2-4990-9cd2-c58384bd9646%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages