Undo in Maya (Mel / Python Execution)


#1

If you are doing something like looping through all verts and altering skinning data, what’s the best way to wrap that in one single undo?

A friend told me to build a huge eval string, I think there must be a better way than that, anyone?


#2
cmds.undoInfo(openChunk=True)

#CODE CODE CODE

cmds.undoInfo(closeChunk=True)

#3

Is the undo-chunk system working properly in Maya now? Because I have experienced some problems with it in the past (jumped from Maya 2009 to 2012 so might have missed some fixes along the way).


#4

I use it extensively, works fine in Maya 2012.


#5

Glad to hear that! Have been relying on roll-back functions, and even though that works great, being able to undo is really useful :slight_smile:


#6

Though be carefull mixing undo and renderLayer switching can produce weird results.


#7

I would strongly recomend wrapping your code in a try, finally block when using undo chunks.

cmds.undoInfo(openChunk=True)
try:
    #CODE CODE CODE
finally:
    cmds.undoInfo(closeChunk=True)

Keir


#8

++ to Keir. It’s not just a good idea, it’s the law – if you leave a dangling undoInfo chunk, Bad Things will happen.


#9

Even better, make an ‘UndoContext’ context manager class (enter and exit methods), like:

class UndoContext(object):
    def __enter__(self):
        cmds.undoInfo(openChunk=True)
    def __exit__(self, *exc_info):
        cmds.undoInfo(closeChunk=True)

with UndoContext():
    ... your code here....

And then you can also pass in all sorts of things into the initializer of the class to correspond to undoInfo parameters. Wrap the shitty maya.cmds into something pythonic, one function at a time.


#10

heh rob, turning maya into max i see!