Undo/Redo

582 views
Skip to first unread message

Brandon Harris

unread,
Oct 15, 2009, 5:20:58 PM10/15/09
to python_inside_maya
OK. So I have finished a small procedure for averaging weights on
components and I have come to a point where I would like to set it up
for undoing and redoing. The only information I have to describe how
it's done is with Complete Maya Programming, but in it, all of it is
written in C++ and it's written as a compiled plugin. So my question
is, what changes about that with this all being in Python and run as a
script rather than a plugin?
Would I need to convert my procedure into an object and have the
undoIt and redoIt methods, or do I need to build the whole thing up as
MPxCommand and run it as a plugin? Any information would be great!

thanks!

Brandon L. Harris

Adam Mechtley

unread,
Oct 15, 2009, 5:34:09 PM10/15/09
to python_in...@googlegroups.com
If you are using OpenMaya API in your script (i.e. not pymel interfaces to the API) then yes, you must do this as an MPxCommand with undoIt() and redoIt() and load it as a plugin.

br...@meljunky.com

unread,
Oct 15, 2009, 5:34:47 PM10/15/09
to python_in...@googlegroups.com
For the most part when writing a plug-in you have to specifically tell Maya how to undo something. For example given a node named "mySphere" is renamed to "yourSphere" (passed as an argument). Then the undo function will rename "yourSphere" back to "mySphere". When you undo, Maya is in the exact state as the doIt/redoIt, so you can assume what you did can be reversed using the same varaibles that are stored globally to the MPxCommand.

The MGlobal class has a method to execute MEL and Python command that 'can' undo. I just used it to create a skinCluster since its easier the create a skinCluster using MEL or Python then the Maya API.

You don't have to compile Python plug-ins for them to run. Check out the examples in the dev kit, in the scripted folder.

-brian

Adam Mechtley

unread,
Oct 15, 2009, 5:38:30 PM10/15/09
to python_in...@googlegroups.com
Alternatively, the MDGModifier class (explained in Complete Maya Programming) allows you to alter the DG without you having to manually implement undo functionality.

Brandon Harris

unread,
Oct 15, 2009, 5:46:33 PM10/15/09
to python_inside_maya
ok, so I suppose since the undo in my case would simply take the
oldWeights and reapply them back it shouldn't be too difficult. Since
I'm in python, would I be able to just setup the command and simply
import it ,or is there a certain way of getting it to a usable state?
(put it in the Plugins folder and load it?

Adam Mechtley

unread,
Oct 15, 2009, 5:49:11 PM10/15/09
to python_in...@googlegroups.com
put the script in a folder in your plug-ins path and load it from the plug-in manager

Brandon Harris

unread,
Oct 15, 2009, 5:54:53 PM10/15/09
to python_inside_maya
good deal! thanks very much for all your help and quick responses!!

Brandon L. Harris

br...@meljunky.com

unread,
Oct 15, 2009, 5:58:30 PM10/15/09
to python_in...@googlegroups.com
If your using the MFnSkinCluster class, it can capture the old values in a MDoubleArray as they are changed in the doIt/redoIt so you can use them in the undo. If speed performance is not an issue then the skinPercent command will work. But, you have to capture the old results first. I used the MFnSkinCluster and changed the weights for one vert at once rather then looping through the joints and the difference was worth it.

Adam was responding so fast I could not get my answers out in time.

-brian

chadrik

unread,
Oct 15, 2009, 7:22:17 PM10/15/09
to python_in...@googlegroups.com
hi all,

pymel.api.plugins provides classes for creating dynamic plugins
outside of the context of a python module: no need to save out a file
and put it on MAYA_PLUG_IN_PATH, you can create new mel commands on
the fly, from a regular python module or from within the script
editor. for an example of how to use it check out
pymel.tools.py2mel.WrapperCommand, which generates mel commands from
python functions and classes. coming up next: a similar tool for
dynamically making new node types.

here's the super simple example:

from pymel.api.plugins import Command
class testCmd(Command):
def doIt(self, args):
print "doIt..."

# create the command
testCmd.register()

# use the command (this is not the same as the class: notice that it's
in the maya.cmds module
import maya.cmds as cmds
cmds.testCmd()

# get rid of hte command
testCmd.deregister()


keep in mind that this is an experimental feature: it may move
locations or change slightly, but it's there for the brave to try out.


-chad

Brandon Harris

unread,
Oct 16, 2009, 10:43:39 AM10/16/09
to python_inside_maya
yep yep. I was thinking about that last night. I know that I catch the
values when I use the setWeights module. I suppose I can just have it
reapply those values when the undo is used. As for the information
about Pymel, That is definitely something I wish I could get into, but
at the moment I'm not in a pipeline that is planning to use pymel
anytime soon so I must work with what I have. thanks!
Reply all
Reply to author
Forward
0 new messages