javaloopsafter-effects

Automatic countdown time between markers in After Effects Expressions


Purpose: Countdown between markers (Med between first and second, between second and third...).

Everything works between the first markers, but when I add a loop it either hangs and crashes, or the counting stays relative to the first two points, going into negative numbers.

How to properly make a loop in After Effects Expressions?

for (k = 1; k < thisLayer.marker.numKeys;) { //numKeys total number of markers
  key1 = thisLayer.marker.key(k).time;
  key2 = thisLayer.marker.key(k + 1).time;
  back_countdown_time = (key2 - key1) - (time - key1); // remaining time between two markers
  if (back_countdown_time < 0) k++; //check to switch to the next marker segment
}

Visualizing an example

1


Solution

  • The problem was the presence of the cycle itself. After reading the documentation, I found a way to solve the problem with "nearestKey(time)"

    n_key = thisLayer.marker.nearestKey(time);
    if (time >= n_key.time) i = n_key.index;
    else i = n_key.index - 1
    key1 = thisLayer.marker.key(i).time;
    key2 = thisLayer.marker.key(i + 1).time;
    back_countdown_time = Math.ceil((key2 - key1) - (time - key1));