Skip to main content
Participating Frequently
August 18, 2022
Answered

Possible to Set a Limit to loopOut Cycles?

  • August 18, 2022
  • 2 replies
  • 9995 views

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!

Correct answer ceznov

Hi!

Dan's expression is wonderful but I am wondering is it possible to add an option to it? I want to be able to set the range of the loopOut so it only affects the kyframes within that range. Let's say I have an object and it does this:

1) At the beginning of the comp it changes its position couple of times (no loop needed)

2) In the middle of the comp it jumps and I want to loop this jump 5 times using Dan's expression

3) At the end of the comp the object changes it position couple of times (no loop needed).

 

So as you can see I want the loopOut to just affect the middle part of the composition and not affect anything else. Can it be done? Thanks!

2 replies

Community Expert
August 19, 2022

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"). 

ceznov
ceznovCorrect answer
Inspiring
September 2, 2023

Hi!

Dan's expression is wonderful but I am wondering is it possible to add an option to it? I want to be able to set the range of the loopOut so it only affects the kyframes within that range. Let's say I have an object and it does this:

1) At the beginning of the comp it changes its position couple of times (no loop needed)

2) In the middle of the comp it jumps and I want to loop this jump 5 times using Dan's expression

3) At the end of the comp the object changes it position couple of times (no loop needed).

 

So as you can see I want the loopOut to just affect the middle part of the composition and not affect anything else. Can it be done? Thanks!

Dan Ebberts
Community Expert
September 2, 2023

Something like this maybe:

nLoops = 5;
loopKey1 = 5;
loopKey2 = 8;

t = time;
if (numKeys >= loopKey2){
  t1 = key(loopKey1).time;
  t2 = key(loopKey2).time;
  loopDur = t2 - t1;
  if (time > t1){
    if (time < t1 + nLoops*loopDur){
      t = t1 + (time - t1)%loopDur;
    }else{
      t = t2 + time - (t1 + nLoops*loopDur);
    } 
  }
}
valueAtTime(t)
Dan Ebberts
Community Expert
August 18, 2022

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;
Participating Frequently
August 18, 2022

Dan! That's brilliant. Thanks SO much! Works like a charm.