ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Reference to previous key frame

FAQ   •   VIEW ALL
 Reference to previous key frame on Sep 20, 2016 at 5:39:53 pmLast Edited By Pablo Levin on Sep 20, 2016 at 5:41:54 pm

Hi all! I am needing to make reference to the previous keyframe. So then I can use something like .valueAtTime(prevKeyframe.time). I'm trying differents things since two days, but I canĀ“t figure it out.

 Re: Reference to previous key frameon Sep 20, 2016 at 5:51:39 pm

Depending on what you're trying to do, this might work:

``` n = 0; if (numKeys > 0){ n = nearestKey(time).index; if (key(n).time > time){ n--; } } if (n > 0) t = key(n).time else t = 0; ```

Dan

 Re: Reference to previous key frameon Sep 20, 2016 at 8:40:59 pm

Yes Dan! You are a genious!!

Thanks again!!!

 Re: Reference to previous key frameon Sep 20, 2016 at 8:41:40 pm

*genius

 Re: Reference to previous key frameon Jan 12, 2019 at 9:50:56 am

Hi guys,

I wrote this one for "previousKey".

So you can easily choose ".value" / ".time" / ".index" at the end.

previousKey :
function F(a){if(a<=time)return 0 else return 1 }key(Math.max(Math.min(nearestKey(time).index-F(nearestKey(time).time),numKeys),1)).value
function F(a){if(a<=time)return 0 else return 1 }key(Math.max(Math.min(nearestKey(time).index-F(nearestKey(time).time),numKeys),1)).time
function F(a){if(a<=time)return 0 else return 1 }key(Math.max(Math.min(nearestKey(time).index-F(nearestKey(time).time),numKeys),1)).index

if needed, this is for nextKey :
function F(a){if(a<=time)return 0 else return 1 }key(Math.max(Math.min(nearestKey(time).index+1-F(nearestKey(time).time),numKeys),1)).value
function F(a){if(a<=time)return 0 else return 1 }key(Math.max(Math.min(nearestKey(time).index+1-F(nearestKey(time).time),numKeys),1)).time
function F(a){if(a<=time)return 0 else return 1 }key(Math.max(Math.min(nearestKey(time).index+1-F(nearestKey(time).time),numKeys),1)).index

Explainations:
function F(a) will test if nearestKey is before or after current time
Math.max/Math.min will exclude some errors

works for me in CS6
works with arrays

`function F(a){if(a&lt;=time)return 0 else return 1 }key(Math.max(Math.min(nearestKey(time).index-F(nearestKey(time).time),numKeys),1)).value`