You can register one or more functions to be called whenever the current 3ds Max animation time is changed, such as when the user drags the time slider or plays the animation. The following methods let you register and unregister these callbacks:
registerTimeCallback <fn>
unRegisterTimeCallback <fn> | undefined )
You can register as many functions as you like. Each one is individually called whenever the time changes. The functions you register must take no arguments. They can access the updated current time through the MAXScript system variable currentTime
.
EXAMPLE:
fn time_p = print currentTime
registerTimeCallback time_p
In the above example, the registered function causes the current time to be printed in the Listener window whenever the user moves the time slider or plays the animation.
You can also enable or disable time change callbacks globally.
<boolean> timeCallbacksEnabled()
Returns whether time change callbacks are enabled. Available in 3ds Max 2021.1 Update and higher.
<boolean> enableTimeCallbacks()
<boolean> disableTimeCallbacks()
Enables or disables time change callbacks. These functions return the current time change callbacks enabled state. So, for example, calling disableTimeCallbacks()
when callbacks are already disabled will return false.
Available in 3ds Max 2021.1 Update and higher.
showregisteredTimeCallbacks [to:stream] [asArray:<boolean>]
Returns a list of registered time change callbacks, listing the function name, source file (if applicable) and line number in the source file where the function is defined. If the optional to argument is specified, the output is sent to that output stream. If the optional asArray argument is specified, the return value is formatted as an array of arrays, where for each registered callback function the first element is the callback function name, the second element is the source file name and line number where the callback function is defined (or an empty string if the function was not defined in a file), and the third element is the actual function value which can be used to de-register the callback.
Special Considerations
If a runtime error occurs in a callback function while it is being executed, an error message is displayed and that callback function is permanently disabled.
The time callback is not called during rendering, even if multiple frames are rendered.
The registered function is executed in a special context and not in the context of the code that created it. This means that the function cannot contain references to local variables in outer code nestings surrounding the function definition since those variables are on an execution stack that does not exist at the time the function is called. An important exception to this is utility and rollout panel locals, such as local functions, rollout variables and nested rollouts. You can refer to them in change handler code inside rollout code as they are associated directly with their rollout or utility object.
Note that it is the function value that is being registered, not the function name or global variable. This means that redefining the same-named function after registering it does not change the callback. You either need to unregister, re-define the function and then register it again, or make the registered function an intermediary which calls another function,
EXAMPLE:
fn time_cb = print currentTime
fn tcb = time_cb()
registerTimeCallback tcb
In this case, the registered callback function, tcb
, calls the real call back, time_cb
, (by referencing the global variable it is defined in), meaning you can redefine time_cb()
as often as you need and the callback will always invoke the latest definition. This is a useful technique to employ while you are developing and debugging a callback.