Skip to content

Instantly share code, notes, and snippets.

@justinfx
Created January 12, 2017 20:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinfx/91cc21a79a182a6d028ab9943381a781 to your computer and use it in GitHub Desktop.
Save justinfx/91cc21a79a182a6d028ab9943381a781 to your computer and use it in GitHub Desktop.
A decorator to wrap Qt slots, in Maya, so that exceptions are reported to Script Editor
"""
A decorator to wrap Qt slots, in Maya, so that exceptions are reported to Script Editor
Ref: https://groups.google.com/d/topic/python_inside_maya/xv7DCiocDZA/discussion
justinisrael@gmail.com
"""
#...
import sys
import traceback
import maya.cmds as cmds
def errorsToScriptEditor(fn):
def wrapped(*args, **kwargs):
try:
return fn(*args, **kwargs)
except:
tb = traceback.extract_tb(sys.exc_traceback)
tb = tb[1:] # pop this current wrapper from info
lines = traceback.format_exception_only(sys.exc_type, sys.exc_value)
lines.extend(traceback.format_list(tb))
err = "Exception occurred in Qt slot:\n%s" % ''.join(lines)
cmds.warning(err, noContext=True)
return wrapped
class Xxx(QWidget):
#...
@errorsToScriptEditor
def raisesError(self):
raise NameError("I failed!")
@michaeldaw
Copy link

Excellent idea. Thanks for posting this.

@kleinheinz
Copy link

This is a life saver! Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment