ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Problem with Math.max expression

FAQ   •   VIEW ALL
 Problem with Math.max expression on Aug 1, 2018 at 12:26:48 pm

Hi,

I am trying to automate some animation by using a few expressions however I am struggling to make it work completely how I need it to. I have one layer that I am trying to move the position of based on the scale of another object. This I have managed to do with this expression:

a = thisComp.layer("CIRCLE_5");
[a.transform.position[0] + a.width*a.transform.scale[0]/100,value[1]] + 35;

However, where the problem starts is that I have another layer which also wants to drive the position of the first layer. I tried to use the same expression again:

b = thisComp.layer("CIRCLE_10");
[b.transform.position[0] + b.width*b.transform.scale[0]/100,value[1]] + 35;

The problem is the second one seems to deactivate the first one. I am also trying to use a Math.max expression to tell my layer to follow the layer that is biggest at the current time. For this I have been trying to use this expression:

a = thisComp.layer("CIRCLE_5");
b = thisComp.layer("CIRCLE_10");
Math.min(a,b)

This doesn't work unfortunately and I am getting very confused with it all. Essentially, if you look at the image below you will see what I am trying to achieve. The right side of the box is tied to a null and the null should always be a certain distance away from the last circle. The circles animate on with a scale up and they are all linked to each other. If there is only 3 red circles the box should be following the blue circles but if there is less blue circles then it should follow the red. Hope this makes sense.

Cheers

Rory

 Re: Problem with Math.max expressionon Aug 1, 2018 at 6:22:11 pm

This is not going to resolve your problem, but should move some things forward.

What I gather from your description is that CIRCLE_5 is the last red circle and CIRCLE_10 is the last blue circle, this means no other circles are involved in your expression. Thus if you have less than five circles in both rows, nothing further will happen, since both circle 5 and circle 10 are at 0 scale and circles 1-4 and 6-9 are not being queried for scale. So that sounds like the first thing that needs resolution. I don't know how your animation is supposed to look, but perhaps you could use a scale from 0 to 500 to scale all five circles.

As for the second expression overriding the first, you're correct. The last value returned by the expression is the one used as the final value. To compare the different results, you'd need to assign them to variables and compare those variables. This should theoretically work:

```a = thisComp.layer("CIRCLE_5"); pos_a = [a.transform.position[0] + a.width*a.transform.scale[0]/100,value[1]] + 35; b = thisComp.layer("CIRCLE_10"); pos_b = [b.transform.position[0] + b.width*b.transform.scale[0]/100,value[1]] + 35; Math.min(pos_a, pos_b) ```

But as described, this will only take into account circles 5 and 10, so it is not a complete solution. You need to work out how to get the result from both rows so that you know which circles should be compared. Or something along those lines.

Kalleheikki Kannisto
Senior Graphic Designer

 Re: Problem with Math.max expressionon Aug 2, 2018 at 8:41:21 am

Hi Kalleheikki,

Thanks for getting back to me.

I see what your saying about the other circles, I probably didn't explain that bit well enough. I have them all linked to each other with this expression:

L = thisComp.layer("CIRCLE_1");
[L.transform.position[0] + L.width*L.transform.scale[0]/100,value[1]];

This works the same way as the box essentially they move right as the circle before them scales up. So they all kind of effect each other. which works well. But it only works on the one line of circles. What I can't make work is both lines working and the Math.max expression.

I tried your expression but I get this error: invalid numeric result (divide by zero?)

I feel like it's pretty close to working I have just ran out of knowledge on expressions as it's not something I have a lot of experience with.

Thanks again for taking the time to help.

Rory

 Re: Problem with Math.max expressionon Aug 2, 2018 at 6:32:55 pm

Any possibility of uploading a stripped-down version of the project with just those layers so I can see what the problem is?

Kalleheikki Kannisto
Senior Graphic Designer

 Re: Problem with Math.max expressionon Aug 3, 2018 at 8:31:07 am

Hi Kalleheikki

Here is the project. 12603_gfxtest.aep.zip
Thanks for taking a look.

Cheers

Rory

 Re: Problem with Math.max expressionon Aug 3, 2018 at 2:37:35 pm

Hi Rory,

Ok, I see what happened, trying to use Math.max on two arrays, not on two variables. Here's the corrected code:

```a = thisComp.layer("CIRCLE_5"); pos_a = a.transform.position[0] + a.width*a.transform.scale[0]/100 +35; b = thisComp.layer("CIRCLE_10"); pos_b = b.transform.position[0] + b.width*b.transform.scale[0]/100 + 35; c = Math.max(pos_a, pos_b); [c, value[1]]```

Kalleheikki Kannisto
Senior Graphic Designer

 Re: Problem with Math.max expressionon Aug 6, 2018 at 9:02:04 am

Hi Kalleheikki,

Thanks so much, that has worked a treat. I think I get where I have gone wrong. Slowly learning how to use expressions.

Thanks again

Rory