Undo in Maya (Mel / Python Execution)

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?

cmds.undoInfo(openChunk=True)

#CODE CODE CODE

cmds.undoInfo(closeChunk=True)

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).

I use it extensively, works fine in Maya 2012.

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:

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

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

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

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.

1 Like

heh rob, turning maya into max i see!