PySide2 undo/redo inside Maya

32 views
Skip to first unread message

Pacifique Nzitonda

unread,
Sep 23, 2022, 5:59:06 AM9/23/22
to Python Programming for Autodesk Maya

I have a question about PySide2 undo/redo in Maya. I read a discussions about using undoInfo Maya command but what if my GUI has to interact with many methods? How can I create an undo/redo for all of them at once instead of doing:

def method1():
    cmds.undoInfo(openChunk=True)
    # my code
    #
    cmds.undoInfo(closeChunk=True)

# or even
def method2():
    try
        cmds.undoInfo(openChunk=True)
        # my code
        #
    except:
        pass
    finally:
        cmds.undoInfo(closeChunk=True)
       
def method3():
    cmds.undoInfo(openChunk=True)
    # my code
    #
    cmds.undoInfo(closeChunk=True)
   
   
I want this:

cmds.undoInfo(openChunk=True)
def method1()
def method2()
def method3()
cmds.undoInfo(closeChunk=True)

Knowing that each method is connected to a button on the GUI

Marcus Ottosson

unread,
Sep 23, 2022, 6:24:16 AM9/23/22
to python_in...@googlegroups.com

You’ve got the right idea, but I’m not sure what you expect is what your users would expect? Am I understanding it correctly that a user would:

  1. Open your UI
  2. Click a button
  3. Think for a moment..
  4. Click another button
  5. Check their phone..
  6. Click a third button
  7. Close the UI

And then when they hit Ctrl + Z it would undo each of these 3 button presses? If it was me, I would expect 3 clicks to require 3 undos.

If you really did want to undo each of the three items, then you’re on a bit of thin ice but not impossibly thin. You can:

  1. On window open, open that undo chunk
  2. Let the user click buttons
  3. On window close, close the undo chunk

That way, the next undo would undo all of it. The reason it’s thin ice is because the undo chunk is global to Maya and would include anything they do outside of it also. So the next undo may take other things with it that falls outside of your control.

Generally, undo has a temperament and should be dealt with cautiously. It can easily break other undo’s in the queue and often bring Maya down with it. So if you can, I would recommend you do what Maya does naturally and avoid undoInfo.


--
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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/15e4aa30-72d1-4122-b1ee-d40b9c63d068n%40googlegroups.com.

Pacifique Nzitonda

unread,
Sep 23, 2022, 11:29:11 AM9/23/22
to Python Programming for Autodesk Maya
Thanks  Marcus, 
After reading your answer, I'll stick to what I have now: for every method  I open and close an undoInfo chunk.

I'm writing a renamer tool:
RENAMER.JPG

My hope was to find a clean way of handling the undo/redo for each button operation.

Justin Israel

unread,
Sep 23, 2022, 3:06:19 PM9/23/22
to python_in...@googlegroups.com


On Sat, 24 Sept 2022, 3:29 am Pacifique Nzitonda, <nzitonda...@gmail.com> wrote:
Thanks  Marcus, 
After reading your answer, I'll stick to what I have now: for every method  I open and close an undoInfo chunk.

I'm writing a renamer tool:
RENAMER.JPG

My hope was to find a clean way of handling the undo/redo for each button operation.

It sounds like what you want is your try/finally example around each button callback so that esch button click generates its own undo. If you plan to sometimes combine methods for different buttons, then you could use the undo only in special wrappers for the button callback only. 

def buttonCallback1():
    openchunk
    method 1 
    method 2
    closechunk

def buttonCallback2():
    openchunk
    method 2
    method 3
    closechunk

Pacifique Nzitonda

unread,
Sep 23, 2022, 3:51:07 PM9/23/22
to Python Programming for Autodesk Maya
Thanks Justin and Marcus for "hammering" on my idea of opening/closing chunk for each method. 
By the way, thanks for having created a group like this where answers come fast :-)

Reply all
Reply to author
Forward
0 new messages