Registering Python Callbacks

Some MAXScript callback registration functions can take a Python function as the callback argument. These include registerTimeCallback(), registerRedrawViewsCallback(), and nodeEventCallback(). Others take a string rather than a function as an argument, and thus cannot take a Python function. These functions include DialogMonitorOPS.RegisterNofication() and callbacks.addScript().

One work-around is to assign the Python function to a variable in pymxs.runtime. Here is a simple example to illustrate the idea:

import pymxs
rt = pymxs.runtime

def myCallback():
    print "Callback fired!"

# connect to the MXS runtime
rt.pyCallback = myCallback

# register ourselves as a callback
# note that the pyCallback() is in fact a maxscript call
rt.callbacks.addScript(rt.Name('nodeCreated'), 'pyCallback()')