FBMenu stops working after an exception was raised

When code associated with a custom menu item raises an exception, the menu item can no longer be executed for that session of MotionBuilder.

Steps:

  1. Load and execute the script below
  2. From the New Menu, select ‘SubMenu 1’
  3. Observe in the Python window that an exception is displayed in the output
  4. From the New Menu, select ‘SubMenu 1’
  5. Observe that nothing is output.
from pyfbsdk import *
def eventMenu(control, event):
raise Exception('kill this menu item')
menuMgr = FBMenuManager()
menuMgr.InsertFirst(None, "New Menu")
newMenu = menuMgr.GetMenu("New Menu")
newMenu.InsertLast("SubMenu 1", 11)
newMenu.InsertLast("SubMenu 2", 12)
# Registers event handler.
newMenu.OnMenuActivate.Add(eventMenu)

The problem here is caused by a change in the way MotionBuilder 2014 handles exceptions. If there is an exception in a Python FBEvent Callback, Mobu will automatically unregister this callback to prevent seeing repetitive tracebacks.

There is a message on the MoBu console when Mobu auto removes those ‘bad’ callbacks, something like:
Exception (boost::python::error_already_set) caught, remove Callback!
If you cannot see the console screen on Windows, add the “-console” flag to Mobu’s shortcut.

You can restore your unregistered callback by registering it again as a workaround, but I would recommend to put a try/except block around your code to avoid the exception to propagate to MoBu and get your callback removed. This is anyway a good practice to handle errors that way in your code.


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading