execute a function in each loop


#1

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 :sad: , i know im doing something wrong, plz help


#2

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?


#3

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…


#4

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.