2010
06.25

Autodesk passes all exceptions thrown in the command engine when in GUI mode to maya.utils._guiExceptHook, rather than doing it the way that Python does it natively through sys.excepthook. Here’s a simple workaround if you want to modify that behavior at run-time.

You can just overload the function of the imported module like so:

import sys
import maya.cmds as cmds

utils = sys.modules['maya.utils']
def excepthook(tb_type, exc_object, tb, detail=2):
	print '='*40
	print utils.formatGuiException(tb_type, exc_object, tb, detail)
	cmds.ScriptEditor()
	print '='*40
	
	import pdb
	pdb.post_mortem(tb)
	return utils.formatGuiException(tb_type, exc_object, tb, detail)

utils._guiExceptHook = excepthook

Or, if you want to get the normal behavior back, you can do this:

import sys
import maya.cmds as cmds

utils = sys.modules['maya.utils']
def excepthook(tb_type, exc_object, tb, detail=2):
	if sys.excepthook != sys.__excepthook__:
		sys.excepthook(tb_type, exc_object, tb)
	return utils.formatGuiException(tb_type, exc_object, tb, detail)

utils._guiExceptHook = excepthook

Now, when an exception is fired, it will first run your custom excepthook (if modified, if not it will just skip it).

Note: your function must return a string, and that string will be passed by the Maya command engine to MGlobal.displayError()

You must be logged in to post a comment.