Notifications
Clear all

[Closed] execute a function in each loop

hi.

How can I execute a function every time the animation repeats when it is loop mode?

I wrote something like this:

at time 0 function()

but it only execute once 🙁 , i know im doing something wrong, plz help

2 Replies

This is the only way I know of:

(
global FrameZeroFunction, AnimRangStartFunction 
 
fn FrameZeroFunction = 
(if (currentTime==0f) do (print "Frame 0"))
 
fn AnimRangStartFunction = 
(if (currentTime==animationRange.start) do (print "Animation Range Start"))
 
registerTimeCallback FrameZeroFunction
registerTimeCallback AnimRangStartFunction
)

The code above shows how to do this using either a fixed time (Frame 0) or the current start time set in the Time Configuration dialog.

There are two potential problems with the Time Change Callback mechanism:
1) It appears to only be called on frames where the viewports are redrawn, so it doesn’t get called on every loop…only about 2/3 of the time.
2) It is frequently called twice for a single time change through the specified time (somewhat like the spinner/colorpicker event handlers).

I don’t know of any method in MaxScript to directly/specifically detect the start of a playback loop…anyone else have a better approach to this?

Here is another approach that will definitely execute once per playback loop:

(
global LastTime = animationRange.end
global PlaybackLoopStartFunction
fn PlaybackLoopStartFunction = 
(
if (currentTime<LastTime) do (print ("New Loop at Frame "+(currentTime as string)))
LastTime = currentTime
)
registerTimeCallback PlaybackLoopStartFunction
)

Here is some of the output to the Listener:

"New Loop at Frame 0.075f"
"New Loop at Frame 0.15625f"
"New Loop at Frame 0f"
"New Loop at Frame 0.075f"
"New Loop at Frame 0.15625f"
"New Loop at Frame 0f"
"New Loop at Frame 0.075f"
"New Loop at Frame 0.15625f"
"New Loop at Frame 0f"

As you can see, the Time Change Callback mechanism does not always get called on whole frame numbers. It also gets called if you manually scroll backward, so you’ll want to put in another safeguard for that…