AE | Expression Driven Gauge - Part 2


In the first post, we looked at building the base and setting up the numbers, now we're gonna make the Master control and get the bar to change colors.


Taking Control

Select the null and go Effect > Expression Controls > Slider Control. This is going to be the master control. We'll interact entirely with the null object.
You can press Enter to rename it. I'm calling it:


Master is going to have a range of 0-100%. 0% being empty, 100% being full. We need to remember this as we move forward. I'll call it % for this tutorial.
Now, we need to figure out the range of rotation we have to work with. Rotate the null to a point where your gauge should be empty.

I'm getting-50 degrees:

Sweet. Now we find the other side... 90 degrees.


We can apply the expression that makes our Master Slider rotate the Null. In the rotation control, Alt+Click the stopwatch and type:

master=thisComp.layer("Null 1").effect("Master")("Slider");
linear(master,0,100,-50,90);

First we're defining a variable: master. This will mean any time we type the word 'master', the expression will know we're referring to the Master slider we put in Null 1.

Then, linear(). Linear is a function that works like 'remapping' so to speak. It monitors the range of another control and remaps it to a range we choose. The syntax is:
linear(control to monitorcontrol's low end, control's high end, my low end, my high end )



So, we tell it the control to monitor (master). It's low and high ends are 0 and 100. However, we want to remap the range from 0-100 to -50 to 90. Using interpolation, AE can figure out that when Master is 50%, then it has to be the middle of -50 and 90, which is 20. So if master is 50%, our layer will rotate to 20 degrees.

Linear is one of the most common expressions I use especially when building preset effects.

Changing Numbers

Okay, let's apply the expression to make the numbers change.

Select the text layer, twirl down to "Source Text". Alt+Click the stopwatch and we're gonna use linear again:

master=thisComp.layer("Null 1").effect("Master")("Slider");
linear(master,0,100,-26,95);


Remember, you can always pick-whip controls instead of typing it all the time, or copy the variable's line to the clipboard. This time linear is giving it a range of -26 to 95. That's the temperature range I want. You can set what you like.

There's a problem though:


After Effects is calculating this by frames, and so the division comes down to some decimal places. Why is it doing this? Because that's what we asked for. Nothing in our expression dictates that we only want full numbers.
This can be fixed quickly by adding Math.floor() around our expression. This will round down every figure produced by the linear to a full number.

master=thisComp.layer("Null 1").effect("Master")("Slider"); Math.floor(linear(master,0,100,-26,95));

Great, now we have whole numbers.

If you'd like a single decimal point, you can change the numbers to -260 and -950, then add a /10 outside the Math.floor.
Math.floor(linear(master,0,100,-260,950))/10

Now, let's get to that color.

Color Changing Bar

We can quickly have color changing by adding an expression to the "Color" property in the circle effect.
We have to first think of the colors we want, and at what point between the 0-100 we'll see them.

I picked Yellow at low temp, Green in the middle and Red at the top. Three colors should do.

In expressions, colors are an array of 4 numbers: [Red,Green,Blue,Alpha]. Each of these values must range from 0-1.

So, red is [1,0,0,1]. yellow is [1,1,0,1] and green is [0,1,0,1].

Confusing?
You'll get used to it.

So we want to look at master and decide how much of it remains red for the bottom portion. It helps to create an animation of 0-100% spanning the comp and use the time to find the value you like.
I want it from 0-30% of master, but what I'm actually going to end up setting is an animation of the change in color, so the numbers I need are the start of the animation and the end of it: 30-45%

Okay, so I'll start with an expression that will turn it red to green, using linear and the numbers above:

master=thisComp.layer("Null 1").effect("Master")("Slider");
linear(master,30,45,[1,1,0,0],[0,1,0,0]);

I've had to put the alpha to 0 as the Color effect produces weird colors. (Why? You tell me!)
So, we define that linear must work from 30-45% of master, this means that before 30%, the value is [1,1,0,0] (Yellow) and after 45% it will be [0,1,0,0] (Green). Useful.

So, play with the master control and:

I changed the range from -26 to 17.

OKAY!
Now, for the top section.
I want to start hitting red from 80-100%, so the animation should be 75-80%. So, we know the linear will be:

master=thisComp.layer("Null 1").effect("Master")("Slider");
linear(master,75,80,[0,1,0,0],[1,0,0,0])

But, how can we make it switch between the linears? Or can we use a different expression to change the times.
I think we can make it switch using an if()else{} expression.

if (something is true) {do this} else {do this};

So we need to figure out what the if should monitor for it to decide between the first linear and the second one. Who else but master!?
We can separate the two linears by the green section. Somewhere in the middle:

master=thisComp.layer("Null 1").effect("Master")("Slider");
if(master < 50){linear(master,30,45,[1,1,0,0],[0,1,0,0])}else{linear(master,75,80,[0,1,0,0],[1,0,0,0])};


So, if master is less than fifty {use red-green expression}, if it's above fifty {use green-red expression}.
The trick here is that both expressions produce green when master hits fifty. This means you won't notice when the expression changes. (try changing the [0,1,0,0] in the second expression to see when it changes.

Finishing Up

We're done. All we need to do now is add the "CORE TEMP" text and our composition is ready to use. We can animate the "Master" slider however we like depending on the situation. Awesome.

So what can you do with this?
You can create keyframed animation and duplicate the comp multiple times. You only have to change the "Master" control to quickly create varying gauges side-by-side.

So remember:
  • Think about the expression and how it will work before you start working with the code.
  • Find the easiest way to do it. If that doesn't cut it, adding expressions also becomes easier.
  • linear(control to monitor, low value of control, high value of control, my low, my high);
  • if (this is true) {do this} else {do this};
I appreciate the comments I've been seeing, thanks a lot guys! :-D