General Event Callback Mechanism

MAXScript allows you to register callback scripts for all of the notification events supported by 3ds max, such as pre/post scene file open, new, reset, scene file save, pre/post render, selection change, etc. You can specify any number of callback scripts per notification event. Callback scripts can be bundled into ID'd sets, and can be deleted individually or all callback scripts in and ID'd set can be deleted. Callback scripts can be specified as persistent so that they are saved and loaded with the currently open file.

The callbacks are maintained by the following set of functions:

callbacks.addScript <callback_type_name> \ ( <script_string> | <script_stringstream> | \ fileName:<filename_string> ) \ [id:<name>] [ persistent:<boolean> ]

This method is used to register a new callback script. Requires as the first argument a name that specifies which type of notify event this script is associated with. The list of valid callback_type_name values is listed below.

The script is supplied either as a direct String or StringStream value containing the text of the script to run, or as a fileName: keyword argument, in which case the named file is loaded and run whenever the event notification callback occurs. You can specify either a direct string or a fileName:, but not both.

The optional id: parameter lets you tag one or a group of callbacks with a unique name so that you can remove them all as a group without interfering with other callbacks, perhaps registered by other scripted tools.

The optional persistent: parameter lets you control whether the script is saved permanently in the currently open scene file or is a global callback script that remains in place no matter what file opening and closing is performed. A true value for the parameter specifies that the script should be stored in the current file and loaded and registered for callback whenever that file is opened again. Persistent callback scripts are always removed when a new file is loaded or a reset is performed so that persistent scripts in one file don't accidentally get copied to a later file. The default for this parameter is false, indicating the script is a global script and is not persistent.

For example:

callbacks.addScript #preRender "setUpRenderGeom()" \ id:#jbwRender

registers a new callback script that will be called just before a render is performed. The script invokes a function (that has presumably already been set up). An unique ID has been assigned to the script for later selective removal.

callbacks.removeScripts [ <callback_type_name> ] [ id:<name> ]

This method is used to unregister and remove one or more callback scripts. Specifying just a callback event type name removes all the callback scripts for that event. Specifying just an id:<name> removes all callback scripts in all events with that ID. Specifying both limits the ID-based removal to the specified event type.

callbacks.show ()

This method lists out the current callback scripts in the Listener window.

callbacks.broadcastCallback <callback_type_name>

This method provides a way for you to simulate one of the events and have all the callback scripts for it executed. Within 3ds max, the #preRenderFrame and #preRenderFrame callbacks can only be called by the renderer. These callbacks can not be called by using this method.

The following is a list of valid callback event type names:

#unitsChange

Sent if the user changes the unit setting.

#timeunitsChange

Sent if the user changes the time format setting.

#viewportChange

Sent if the user changes the viewport layout.

#spacemodeChange

Sent if the user changes the reference coordinate system.

#modPanelSelChanged

Sent whenever the Modify panel opens on a new selection, either because the Modify panel was just selected or because a new selection is made in the scene. The notify occurs at a point just prior to panel redraw but after the selection has been established, so that callback functions can modify the panel state without it being overwritten.

System Notifications

#systemPreReset

Sent before 3ds max is reset.

#systemPostReset

Sent after 3ds max is reset.

#postSystemStartup

Sent when the software goes live.

#pluginLoaded

Sent whenever a plug-in is loaded.

#systemPreNew

Sent just before 3ds max is reset.

#systemPostNew

Sent just after 3ds max is reset.

#preSystemShutdown

Sent just before the software enters the shutdown process.

#postSystemShutdown

Sent just before the software finishes the shutdown process.

#colorChanged

Sent when the system is updating it's custom colors.

File Notifications

#filePreOpen

Sent before a new file is opened.

#filePostOpen

Sent after a new file is opened successfully.

#filePreMerge

Sent before a file is merged.

#filePostMerge

Sent after a file is merged successfully.

#filePreSave

Sent before a file is saved.

#filePostSave

Sent after a file is saved.

Renderer Notifications

#preRender

Sent before rendering is started. This notification is sent out before the renderer creates the render instance objects, which means that you can create nodes and other objects as a response to this event. When rendering multiple frames, this event is sent only once at the beginning of the rendering phase, and not on a per-frame basis. In the #preRender callback, you can't change any of the render parameters (height, width, aliasing, etc) and effect the current render sessions. Those parameters have already been set up internally in 3ds max, and so changes to them won't occur until the next render session occurs.

#postRender

Sent after rendering has finished. This notification is sent out after the renderer destroys the render instance objects, which means that you can create nodes and other objects as a response to this event. When rendering multiple frames, this event is sent only once at the end of the rendering phase, and not on a per-frame basis.

#preRenderEval

Sent just before the renderer starts evaluating objects.

#preRenderFrame

Sent just before each frame is rendered by the renderer. This notification is sent out after the renderer has taken a snapshot of the scene geometry. At the time of this call the scene cannot be modified. The renderer has already called GetRenderMesh() on all the object instances, and the materials and lights are already updated. If you don't modify anything that is rendered, then it is okay to use this callback. The current frame being rendered is set into the MAXScript currentTime context and system global, so any references to other scene object properties will automatically be resolved at the currently rendering frame's time. This notification is not sent out when the renderer is called to update the Material Editor sample spheres, only during output rendering.

#postRenderFrame

Sent just after each frame is rendered by the renderer. The current frame being rendered is set into the MAXScript currentTime context and system global, so any references to other scene object properties will automatically be resolved at the currently rendering frame's time. This notification is not sent out when the renderer is called to update the Material Editor sample spheres, only during output rendering.

#renderParamsChanged

Sent whenever the common renderer parameters have changed.

Import/Export Notifications

#preImport

Sent before a file is imported.

#postImport

Sent after a file is imported successfully.

#importFailed

Sent if import fails.

#preExport

Sent before a file is exported.

#postExport

Sent after a file is exported successfully.

#exportFailed

Sent if export fails.

Node Related Notifications

#nodeCreated

Sent when a node is created.

#nodeRenamed

Sent if a scene node is renamed.

#nodeHide

Sent when a node is hidden.

#nodeUnhide

Sent when a node is unhidden.

#nodeFreeze

Sent when a node is frozen.

#nodeUnfreeze

Sent when a node is unfrozen.

#nodeLinked

Sent when a new parent-child link is made.

#nodeUnlinked

Sent when a parent-child link is broken.

#nodePreMaterial

Sent just before a node gets a new material

#nodePostMaterial

Sent just after a node gets a new material

#sceneNodeAdded

Sent just after a node is added to the scene.

#selectedNodesPreDelete

Sent just before selected nodes are deleted.

#selectedNodesPostDelete

Sent just after selected nodes are deleted.

Material Library Notifications

#matLibPreOpen

Sent just before loading a material library.

#matLibPostOpen

Sent just after loading a material library.

#matLibPreSave

Sent just before saving a material library.

#matLibPostSave

Sent just after saving a material library.

#matLibPreMerge

Sent just before merging a material library.

#matLibPostMerge

Sent just after merging a material library.

Asset Browser Notifications

#assetBrowserPreNavigate

Sent whenever a URL is loaded into the Asset Browser.

VIZ-Only Notifications

#heightMenuChanged

Sent when a user operated the height menu.

#fileLinkPreBind

Sent just before a file link bind.

#fileLinkPostBind

Sent just after a file link bind.

#fileLinkPreDetach

Sent just before a file link detach.

#fileLinkPostDetach

Sent just after a file link detach.

#fileLinkPreReload

Sent just before a file link reload.

#fileLinkPostReload

Sent just after a file link reload.

#fileLinkPreAttach

Sent just before a file link attach.

#fileLinkPostAttach

Sent just after a file link attach.

Other Notifications

#animateon

Sent when the Animate button is turned on.

#animateOff

Sent when the Animate button is turned off.

#mainWindowEnabled

Sent when main window gets enabled.

#selectionSetChanged

Sent after the selection set has changed.

#bitmapChanged

Sent after a bitmap is reloaded.