Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Expression count previous keyframes

Engaged ,
Jan 17, 2018 Jan 17, 2018

I need to have the an offset value count how many keyframes on a null layer previous to the current time.

Suggestions?

3.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 17, 2018 Jan 17, 2018

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:

thisComp.

...
Translate
Community Expert ,
Jan 17, 2018 Jan 17, 2018

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 17, 2018 Jan 17, 2018

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,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 17, 2018 Jan 17, 2018

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:

thisComp.layer("Null 1").transform.position.nearestKey(time).index

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 12, 2020 Oct 12, 2020

Rick,

 

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 12, 2020 Oct 12, 2020

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.

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 12, 2020 Oct 12, 2020

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 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 12, 2020 Oct 12, 2020
LATEST

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. 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines