Created
January 12, 2017 20:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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!") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a life saver! Thanks so much!