Using control z with pyside 2 in Maya


#1

I’m somewhat new to creating interfaces using pyside2 so I hope this question isn’t to rookie, but I’ve noticed that when you click control z using a pyQT GUI it will take you back one step in the codes operation per click vs going back entirely to the point prior to when you clicked the button. What do I need to do in order to have control z take the user back to the point before they used the script?


#2

I’m guessing you’re looking for cmds.undoInfo
https://help.autodesk.com/cloudhelp/2017/CHS/Maya-Tech-Docs/CommandsPython/undoInfo.html

As it says in the docs, it can be dangerous. You must close open chunks, and you can’t close chunks if there are none open. A good way of ensuring that always happens is to make an undo context manager like this:

from contextlib import contextmanager
from maya import cmds

@contextmanager
def undoContext():
    cmds.undoInfo(openChunk=True)
    try:
        yield
    finally:
        cmds.undoInfo(closeChunk=True)

Then, you can use the python with keyword like so:

with undoContext():
    # Put all your code in here, and it will make sure that
    # undo chunks are safely opened and closed

#3

Thank you! this worked perfectly.