# Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Highest Keyframe Value

• # Highest Keyframe Value

4 Members · 15 Posts
• ### Kevin Snyder

January 3, 2022 at 9:23 pm

I’m using the expression below to get the highest keyframe value of an animated property. The only problem is that properties that are more than one value, like position, have to be separated, which doesn’t work for scale. Is there a way to get the highest value without separating the dimensions and specify which value it’s measuring?

For example, find the highest x position keyframe value whether the dimensions are separated or not.

```a = thisComp.layer("Shape Layer 1").transform.xPosition;
max = a.value;
for (i = 1; i <= a.numKeys; i++){
max = Math.max(max,a.key(i).value);
}
max```
• ### Levi Borland

January 4, 2022 at 1:56 am

This for has a algorithmic efficiency of 2^n. In other words it completes two tasks each iteration (n standing for the total number of repeated iterations)Those two tasks are checking the current value of the next index, and then comparing it to the current max value. It then needs to repeat this process for the entire list of values. As you mentioned, this would not scale well at all. This becomes further complicated as positional data is usually stored as a 2 value (X and Y) or 3 value array (3D Z Position, X, and Y). This requires the additional use of the variable a in your code. But there is an easy way to reference to a specific dimension value, and that is by it’s index.

transform.Position.value[0] = X Value

transform.Position.value[1] = Y Value

transform.Position.value[2] = Z Value (If you are working with 3D Layers)

_______________________________________

try this….might work and might not….I am typing it out here and not in an IDE or AE Expression Field.

———————————————>

max = thisComp.layer(“Shape Layer 1”).transform.Position.value[0];

for (i = 1; i<=thisComp.layer(“Shape Layer 1”).transform.Position.numkeys; i++) {

max = Math.max(max, max.key(i).value);

}

max

The example above might not be perfect but it removes at least one step of assigning an A variable each iteration. As well as eliminates the need to have the algorithm manually assign a temp value to Max by extracting the X Position Data. This instead just assigns the value of the Shape Layers X Position Value by referring to it by it’s array index.

As mentioned this code may not work as I am typing it out here. I would also suggest assigning a different variable name for max such as maxXPosVal so it doesn’t get confused with the Math.max method you are calling. It will also make future code review easier when you have spent some time apart.

I hope this all made sense, and I also hope I didn’t just make a fool of myself with the code. I just started JavaScript recently but have tons of experience with Java. Pretty similar, just slightly different syntax.

Let me know if this solves your query, or at least help you along in the right direction.

• ### Kevin Snyder

January 5, 2022 at 10:20 pm

Thank you for your effort. For some reason, this method won’t work. I’m not why as I have done it that way in the past, but in this case, it returns a syntax error.

• ### Levi Borland

January 12, 2022 at 3:28 pm

Can you screenshot the syntax error. It probably is because I didn’t put punctuation in the right place or something.

• ### Kevin Snyder

January 12, 2022 at 9:21 pm

Sure thing! Here it is.

• ### Levi Borland

January 12, 2022 at 9:33 pm

Remove .value

• ### Kevin Snyder

January 12, 2022 at 9:34 pm

I tried that right after I posted the image. The same error on line one occurs.

• ### Andrei Popa

January 12, 2022 at 9:59 pm

Your quotes are wrong, probably formatted from the forum. Delete and re-write them.

• ### Levi Borland

January 12, 2022 at 9:59 pm

Click on the orange or yellow triangle with the exclamation point to the left of the red arrow. It should give you more info. Can you screenshot that?

• ### Levi Borland

January 12, 2022 at 10:15 pm

Nevermind, I put it into AE and it doesn’t give any meaningful help.

Viewing 1 - 10 of 15 posts