In this post, I will go over functions in After Effects and how they can speed up your workflow. So without any further ado, let’s dive right in!
Alright, so we are in After Effects and for this example, I’m going to ALT+Click (OPT+Click on a Mac) on the Source Text stopwatch icon of my UKRAMEDIA.COM text layer.
Now let me explain what is function. So essentially function is something where you can store an expression in.
You can have a long expression and instead of typing that expression over and over again, you can just refer to that function.
Let’s create our first fuction. I am going to say function and then I will give it the name ukramedia(), but it can be anything.
function ukramedia()
And in the parenthesis, I am going to give it a variable. So I am going to say a for this example, but it can be anything.
function ukramedia(a)
Then I will add open curly brackets and inside of the curly brackets is where we are going to create our expression.
function ukramedia(a) {
}
So I am going to do the following:
return “S” + a
By the way, a becomes whatever I put inside ukramedia parentheses, so if I put 5, it is going to become 5.
Next, I am going to do .toFixed() and I will add 2 inside of the parenthesis.
function ukramedia(a) {
return “S” + a.toFixed(2);
}
Make sure you close the curly brackets.
Now we are done as far as creating a function. So if I type 5 after the closed curly brackets.
You will see our 5
However, it will be different if I run it through the function that we just created.
Let me show you what I am talking about. So let’s run 5 through our function. To do that, I am going to say ukramedia() and then I will put that 5 inside those parentheses.
function ukramedia(a) {
return “S” + a.toFixed(2);
}
ukramedia(5)
If I let go, you can see that it is doing exactly what it is supposed to do.
As you can see, this function added a dollar sign in front of our 5 and it gave us two decimals.
We can keep going with this. We can say something like this:
function ukramedia(a) {
return “S” + a.toFixed(2);
}
ukramedia(5) + “-” + ukramedia(10);
You can see how easily you can reuse that function instead of me typing that expression over and over again.
By the way, you can also do if/Else Statements within that function.
If front of the return “S” + a.toFixed(2); I am going to say if() and inside my parentheses, I am going to say the following:
if a is smaller than 10, I want it to run return “S” + a.toFixed(2);
function ukramedia(a) {
if(a < 10) return “S” + a.toFixed(2);
}
ukramedia(5) + “-” + ukramedia(10);
Next, I am giong to add something else to it. I am going to say + and let’s do “0” + like so:
function ukramedia(a) {
if(a < 10) return “S” + “0” + a.toFixed(2);
}
ukramedia(5) + “-” + ukramedia(10);
So if a is less than 10, I want to add 0 to my 5. So it is going to have a dollar sign 05.
If it is not less than 10, then I will say else return and I will paste return “S” + “0” + a.toFixed(2); like so:
function ukramedia(a) {
if(a < 10) return “S” + “0” + a.toFixed(2) else return “S” + “0” + a.toFixed(2);
}
ukramedia(5) + “-” + ukramedia(10);
So again, it is going to add a 0 if my number is less than 10 and if it is not, it will just leave it the way it is.
Okay so let’s see what happens.
ukramedia(5) + “-” + ukramedia(10);
As you can see, my first ukramedia(5) is less than 10 and because of that it added 0 to it and the second ukramedia(10) is not less than 10 and because of that, it did not add 0 to it.
By the way, there is one thing I forgot to mention. You can set your variables in two ways.
In this example, we set our variable by typing function ukramedia(a) { at the very beginning but you can also set it differently by typing ukramedia = function(a) { instead.
ukramedia = function(a) {
if(a < 10) return “S” + “0” + a.toFixed(2) else return “S” + “0” + a.toFixed(2);
}
ukramedia(5) + “-” + ukramedia(10);
As you can see from the screenshot above, it will do exactly the same thing.
I hope you found this post useful. If you would like to learn more about Expression and how it can speed up your workflow in After Effects, check out my Learn After Effects Expressions Course.