undo after running finction from pyside problem

225 views
Skip to first unread message

ynedelin

unread,
Dec 19, 2014, 2:21:32 PM12/19/14
to python_in...@googlegroups.com

Hey Guys
I was wondering if anyone have seen this behavior.

I have this small function 

""" test undo with QT """
import maya.cmds as mc
def testLoop():
    sel = mc.ls(sl=1)
    for node in sel:
        mc.move(1,1,1, node, relative=1)


If I run this on a selection of nodes from script editor I can undo the whole function with a single undo

but

if I run this from a pyside button I have to undo every 'move' command so if there are 10 selected objects I have to run undo 10 times.

What am I doing wrong?

Thanks a lot.
here is my UI script


from PySide import QtCore, QtGui, QtUiTools
from shiboken import wrapInstance
import maya.OpenMayaUI as omui
import pysideuic
import maya.cmds as mc
import os, sys, inspect

import testUndo as T


filePath = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
uiFile = os.path.join( filePath , "testUndo.ui")

main_window_ptr = omui.MQtUtil.mainWindow()
_parentWindow = wrapInstance(long(main_window_ptr), QtGui.QWidget)

class UiLoader(QtUiTools.QUiLoader):
    def __init__(self, baseinstance):
        QtUiTools.QUiLoader.__init__(self, baseinstance)
        self.baseinstance = baseinstance
    def createWidget(self, class_name, parent=None, name=''):
        if parent is None and self.baseinstance:
            return self.baseinstance
        else:
            widget = QtUiTools.QUiLoader.createWidget(self, class_name, parent, name)
            if self.baseinstance:
                setattr(self.baseinstance, name, widget)
            return widget

def loadUi(uifile, baseinstance=None):
    loader = UiLoader(baseinstance)
    widget = loader.load(uifile)
    QtCore.QMetaObject.connectSlotsByName(widget)
    return widget

class TestUndoWindow(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self, parent=_parentWindow)
        loadUi(uiFile, self)

    def establishConnections(self): 
        self.testUndo_btn.clicked.connect(self.testUndo)

    def testUndo(self):
        T.testLoop()

Justin Israel

unread,
Dec 19, 2014, 3:03:04 PM12/19/14
to python_in...@googlegroups.com

I don’t remember the exact circumstances when Maya decides to do it for you or not, but you could explicitly define an undo chunk:

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

--
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/4b20c166-e487-4b7c-b7d3-0eb754494cbf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ynedelin

unread,
Dec 19, 2014, 3:46:01 PM12/19/14
to python_in...@googlegroups.com
Thanks Justin,
I tried adding this to the function that runs in the pyside button and I got the same result, undo was still only recording the move commands one at a time.

yury

ynedelin

unread,
Dec 19, 2014, 5:50:14 PM12/19/14
to python_in...@googlegroups.com
Hey Justin,
after playing around with chunk undo I got it to work. I do not like this bit in the docs  "Use with CAUTION!! " but it seem to work now,
Thanks
Yury

Justin Israel

unread,
Dec 19, 2014, 6:17:42 PM12/19/14
to python_in...@googlegroups.com

The use with caution bit is about making sure you always close the chunk. That's why I wrap it in a try.. finally. It ensures that even if the code inside crashes, the chunk will get closed. You could wrap it into a fancier context using the contextlib module. That would let you do something like

with undoChunk():
   ...


--
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.
Reply all
Reply to author
Forward
0 new messages