Some MAXScript callback registration functions can take a Python function as the callback argument. These include registerTimeCallback(), registerRedrawViewsCallback(), nodeEventCallback(), and callbacks.addScript() (as of 3ds Max 2021). The exception is DialogMonitorOPS.RegisterNofication(), which takes a string argument, and thus cannot take a Python function.
Note: The Notification Codes topic lists available event codes for both MaxPlus and pymxs, and is useful for migrating scripts from MaxPlus to pymxs.
For example, you can use the general callback system to create a callback that fires when nodes are created:
import pymxs rt = pymxs.runtime # remove any previous callbacks: rt.callbacks.removeScripts(id=rt.name('MyCallbacks')) # function to print the notification param, which in the case of NodeCreated, is the node def myCallback(): print ('Callback fired!') notification = rt.callbacks.notificationParam() print (notification) rt.callbacks.addScript(rt.Name('nodeCreated'), myCallback, id=rt.Name('MyCallbacks')) # Will print something like this: # Callback fired! # $Sphere:Sphere005 @ [0.000000,0.000000,0.000000] print(rt.callbacks.show(id=rt.Name('MyCallbacks'), asArray=True)) # Prints something like: # #(#(#nodeCreated, #myCallbacks, false, false, "<function myCallback at 0x0000021020609828>()"))
We need to use the asArray=True parameter for callbacks.show(), because normally this function tries to output to stdout (the listener), which is disabled in Python for this call. You could also use the to:stringStream parameter to capture the output.
Note: Even though the id parameter is optional for addScript(), it is a best practice to always provide one, to make it easier to find and remove callbacks later. In this example we can use it to "clean up" before running the script.