Copy link to clipboard
Copied
I've got a composition with a few layers of precomps. I enabled Time Remapping on each and added this script:
loopOut(loop="cycle",numKeyframes=0)
Works great. I'd love to be able to limit the number of times the animation loops, though. Is that possible?
Thanks much!
Copy link to clipboard
Copied
Try this instead of loopOut():
nLoops = 10;
if (numKeys > 1){
loopDur = key(numKeys).time - key(1).time;
n = Math.floor((time - key(1).time)/loopDur);
if (n < nLoops){
t = (time - key(1).time)%loopDur;
valueAtTime(key(1).time + t);
}else{
valueAtTime(key(numKeys).time);
}
}else
value;
Copy link to clipboard
Copied
Dan! That's brilliant. Thanks SO much! Works like a charm.
Copy link to clipboard
Copied
Dan's solution is elegant, but I think this one is more useful.
If the first and last frames of the nested comp are identical so you can have an identical loop, and you went to the last Time Remapping keyframe, moved back one frame, set a new TR keyframe, then deleted the last one, so you have a perfect seamless loop try this:
Time remapping works internally on frame numbers. If you convert time to frame numbers, you can multiply the frame number of the Time Remapping keyframe by the number of loops you want. Combine that with an if statement, and you get this.
fr = time / thisComp.frameDuration;
t = key(2).time / thisComp.frameDuration;
nL = 8
if (fr < t * nL)
loopOut();
else
0
That expression will loop the animation eight times and then stop. You can also slide the last TR keyframe left or right in the timeline to change the speed of the loop without modifying the expression.
I attached a sample comp.
It also works with loopOut("ping-pong").