You'll need to clarify a bit. You can use valueAtTime or look for nearest, previous, or next keyframe. What exactly are you trying to do?
Basically I have text that reveals itself line by line. This is controlled by an animator control offset.
I need this offset value to be changed over time based on the count of previous keyframes on another layer,
Do you need to delay the reveal so it happens after a keyframe? If all you need to do is count keyframes than that is pretty easy. nearestKey(time).index will give you the number of the keyframe nearest the current time. If you need to make the switch exactly on a keyframe or a specific number of frames before or after then it is going to require a bunch more language.
Let's say your keyframes were set on Null 1's position property. The expression to count keyframes would look like this:
You would set up your text animator to use Index and Words. The new number would appear as soon as the half way point between keyframes is passed. It will start with 1.
I appreciate your response to the original poster, the code works great! You stated a little more language was needed to action the count ON each key frame; would you be able to share that with me? I own a kill counter youtube channel and currently edit each text box seperately, and as you can imagine, this takes an absolute age to edit one video with up to 3000 kills.
So I also have anohter question; am i able to use keyframes to trigger a sound FX too?
Very many thanks in advance!
You can retrieve the time of the nearest keyframe or the number of the nearest keyframe by starting with this simple expression:
ref = thisComp.layer("Reference Layer"); keyTime = ref.position.nearestKey(time).time; keyIndex = ref.position.nearestKey(time).index;
In this case, I'm looking at the Position property of a layer named "Reference Layer" and returning the keyframe time and the keyframe number.
You can then use if/else statements, accumulators, or other methods to make changes to other properties.
You cannot cue audio tracks using keyframes but you could time remap an audio layer, extend it. set a couple of keyframes at the start and end of a sound effect, then use an if statement to start playing the sound when a new keyframe is reached. This would be the same kind of thing you would use to drive time remapped mouth shapes to animate a character.
I don't have time to figure out all of the code for you but if you are good with Expressions and do a little research, you should be able to make something happen when you get to a keyframe on another property.
Rick, thank you very much for this; I will get to work on it and see what I can make of it! I appreciate your time answering 🙂
This should give you the time of the previous keyframe for any property.
ref = thisComp.layer("Reference Layer").position; keyTime = ref.nearestKey(time); if (keyTime.time <= time) keyNumber = keyTime.index; else keyNumber = keyTime.index - 1; lastKeyTime = ref.key(keyNumber).time;
It might give you an idea of how to proceed. You could use the keyNumber of the last keyframe or the lastKeyTime to drive other things. Maybe it will point you in the right direction. I find it useful to apply expressions like this to a text layer's Source Text property so I can see exactly what I'm getting.