<p>Yep, just do</p>



<pre><code>var p = &lt;pick whip the property with the keyframes here&gt;;
var k = p.nearestKey(time);
if (k.time &lt;= time){
    k.index; //key is in the past
} else {
    k.index-1; //nearest key is in the future, return the previous key
}
</code></pre>

<p>On the first line you have to reference the property with the keyframes. The easiest way is with the pick whip (the curly thing in the expressions buttons). Drag it to the property you want to reference.</p>

<p><a href="https://i.sstatic.net/dfF7U.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/dfF7U.png" alt="enter image description here"></a></p>

<p>So the first line should look something like this:</p>

<pre><code>var p = thisComp.layer("Pale Gray-Magenta Solid 2").effect("Slider Control")("Slider");
</code></pre>

<p>or</p>

<pre><code>var p = transform.position;
</code></pre>

<p>Don't forget the semi-colon at the end.</p>

CC BY-SA 4.0
2019-09-23T07:53:16.757Z
2019-09-23T07:53:16.757Z
stib
<p>I'm working on a video comparing old and new games, and I'm scrubbing through some footage frame by frame trying to count button inputs in DOOM over a period of time. I was wondering if there was a way to increment a number based on the amount of keyframes present. So ideally it would automatically go up by one for each keyframe placed on the timeline, without having to manually increase the value on a per keyframe basis. Is there an easy way to go about this or will I have to manually set the value per keyframe?</p>

<p>I've never really used expressions before so this is new territory for me, and I don't know where to start.</p>

<p>Thank you.</p>

<keyframes>
Increment a number, based on number of keyframes in After Effects?
TacticalTruth
# Increment a number, based on number of keyframes in After Effects?

(Asked by: username_0 on 2019-09-21)

I'm working on a video comparing old and new games, and I'm scrubbing through some footage frame by frame trying to count button inputs in DOOM over a period of time. I was wondering if there was a way to increment a number based on the amount of keyframes present. So ideally it would automatically go up by one for each keyframe placed on the timeline, without having to manually increase the value on a per keyframe basis. Is there an easy way to go about this or will I have to manually set the value per keyframe?

I've never really used expressions before so this is new territory for me, and I don't know where to start.

Thank you.

**Tags:** keyframes


---

## 1 answers

### Answer by username_1 on 2019-09-23. Score: 1

Yep, just do
var p = ;
var k = p.nearestKey(time);
if (k.time <= time){
    k.index; //key is in the past
} else {
    k.index-1; //nearest key is in the future, return the previous key
}

On the first line you have to reference the property with the keyframes. The easiest way is with the pick whip (the curly thing in the expressions buttons). Drag it to the property you want to reference.

So the first line should look something like this:
var p = thisComp.layer("Pale Gray-Magenta Solid 2").effect("Slider Control")("Slider");

or
var p = transform.position;

Don't forget the semi-colon at the end.


---

