I started playing with Qt after reading about it in the GUI thread and I got it up and running great, launching commands, etc.
I ran into one thing that I was wondering if it’s a common issue.
If I have a script that is a series of command (say create a bunch of locators) , if I run it through PyQT the undo queue gets filled with each operation separately, as opposed to the normal behavior of “grouping” all of those actions under the script call.
Is it something I’m doing wrong while launching pyQT or is it a known issue?
When I have come across this problem before I have just wrapped my script in an undoInfo wrapper.
maya.cmds.undoInfo(openChunk=True)
try:
# call your script here
finally:
maya.cmds.undoInfo(closeChunk=True)
The use of the try, finally is important.
If for whatever reason you script gets an error and you don’t call closeChunk Maya’s undo stack will break and make things very unstable.
A little while back I started a decorators library such as showing wait cursors and echoing the method being called.
This is the make any function undo-able decorator.
def d_undoable(f):
"""
A decorator that will make commands undoable in maya
"""
def func(*args, **kwargs):
cmds.undoInfo(openChunk=True)
functionReturn = None
try:
functionReturn = f(*args, **kwargs)
except:
print sys.exc_info()[1]
finally:
cmds.undoInfo(closeChunk=True)
return functionReturn
func.__name__ = f.__name__
func.__doc__ = f.__doc__
return func
Not a great example but you get the idea.
@d_undoable
def someFunction():
for i in range(10):
print "blah"
[QUOTE=snolan;29255]A little while back I started a decorators library such as showing wait cursors and echoing the method being called.
This is the make any function undo-able decorator.
def d_undoable(f):
"""
A decorator that will make commands undoable in maya
"""
def func(*args, **kwargs):
cmds.undoInfo(openChunk=True)
functionReturn = None
try:
functionReturn = f(*args, **kwargs)
except:
print sys.exc_info()[1]
finally:
cmds.undoInfo(closeChunk=True)
return functionReturn
func.__name__ = f.__name__
func.__doc__ = f.__doc__
return func
Not a great example but you get the idea.
@d_undoable
def someFunction():
for i in range(10):
print "blah"
use functools.wraps, it handles all of the remapping of names, docs, etc…
Also, don’t use bare except blocks, this will block things like SystemExit, which you really don’t ever want to block. If you need to catch Maya’s exceptions, use RuntimeError. Which is still really broad, but well it is the best we’ve got, unless you’re using PyMel.
import functools
def d_undoable(f):
"""
A decorator that will make commands undoable in maya
"""
@functools.wraps(f)
def func(*args, **kwargs):
cmds.undoInfo(openChunk=True)
functionReturn = None
try:
functionReturn = f(*args, **kwargs)
except RuntimeError as e:
print e.message
finally:
cmds.undoInfo(closeChunk=True)
return functionReturn
return func