Expressions Basics - After Effects Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I use time, wiggle, and loopOut("cycle") the most. Also know that you can convert the values of an expressions to keyframes by right clicking on the property > Keyframe Assistant > Convert Expression to Keyframes.

👍︎︎ 26 👤︎︎ u/cafeRacr 📅︎︎ Jan 22 2021 🗫︎ replies

That's one thing I don't get, gonna check this video Out thanks

👍︎︎ 14 👤︎︎ u/SkyShazad 📅︎︎ Jan 22 2021 🗫︎ replies

Former Flash guy here that was very used to animating things via code almost entirely... A few thoughts.

Thinking about expressions is actually pretty simple if you have a knowledge of object oriented programming. The basic building blocks are "Objects" (comps) which can contain other objects (other comps, images, footage, etc). Each object contains a set of parameters and/or variables that can be manipulated (position, rotation, scale, etc, etc). An objects parameters are generally defined in AE, but occasionally you can add more parameters and/or variables (like effects, sliders, etc). So, what you know as keyframes is actually just a GUI for manipulating parameters/variables. This can be done in code too, or "expressions".

Now... Expressions are just a different way to change a parameter of an object using math. You're just writing a snippet of code to do it instead of a keyframe.

Admittedly, AE's baked-in expressions like 'time' can be a bit hard to grasp at first, but think of them as variables themselves. 'time' for example is a variable that contains the value of time. At the 20 second mark, 'time' = 20. 'Wiggle' does some more complex math behind the scenes, basically manipulating a parameter (scale, position, etc) in a random way. Ether way, you're just changing values of parameters using math.

The shitty thing about AE/expressions is, although it's basically ECMA/javascript, for whatever reason AE doesn't use some of the syntax that is common in javascript. Math.random() in javascript is just 'random()' and other annoying things like that...

👍︎︎ 7 👤︎︎ u/specialistdeluxe 📅︎︎ Jan 22 2021 🗫︎ replies

i literally asked for the same question a few days ago. I'll check it out!

👍︎︎ 4 👤︎︎ u/Lucifersassclown 📅︎︎ Jan 22 2021 🗫︎ replies

Great tutorial to introduce people to the basics.

👍︎︎ 3 👤︎︎ u/jeeekel 📅︎︎ Jan 22 2021 🗫︎ replies

The design of that fairground is pretty sleek!

👍︎︎ 3 👤︎︎ u/atilla32 📅︎︎ Jan 22 2021 🗫︎ replies

Thank you for doing this!

👍︎︎ 2 👤︎︎ u/Jojo_Manji 📅︎︎ Jan 22 2021 🗫︎ replies

Thanks man! Such a useful video :)

👍︎︎ 2 👤︎︎ u/CosmicSpatula 📅︎︎ Jan 22 2021 🗫︎ replies

Thank you!!!

👍︎︎ 2 👤︎︎ u/teamricearoni 📅︎︎ Jan 22 2021 🗫︎ replies
Captions
expressions are the hidden gem of after effects which a lot of animators avoid or neglect because they see it as too technical although mastering expressions is technical there are still many basic expressions that even someone with zero knowledge of programming can use expressions are great if used effectively because they can reduce the amount of keyframes in your scene keeping things tidy i'm simply going to show you a scene that i've animated using my most commonly used expressions and walk you through how each one works this should give you a good starting point to go and learn more let's jump in [Music] okay so once you've got the project open you want to open the fairground scene composition here and let's take a look at this spinning ferris wheel first so if we come down to the layer view down here and we select wheel outlines let's click this dot here so that only the wheel is visible and now if we press play we've just got that wheel rotating let's click on this layer again press r and twirl down this expression view here and here we can see our expression now let's remove this expression here by just highlighting it and pressing backspace i want you to just type in a number here like 50 and then click off if you look here at the rotation it set our rotation to the number we've entered here so any expression you write is essentially just a more complicated way of setting the rotation to a specific value to make it spin we're going to use the time property so to demonstrate what the time property is actually doing i'm going to create a text field here and just call it time let's make it a little bigger let's scale it up i'm going to twirl down the layer here i'm going to twirl down text and i'm going to hold alt and click on source text i'm going to type in here time i'm going to click off you can see here as we now move our playhead the text field is changing based on the current time of our playhead so that's essentially what the time field does so let's remove that text layer let's go back to our wheel and into our expression field here where we've written 50 and if we just backspace this and type time and click off and we press space you can see it's very slowly turning so to speed this up we need to multiply this value by something so let's say times 30 and now if we press play you can see it spins quite quickly so to make this a bit more readable we're going to create a variable to store this value 30 in so let's go to the start of this line and press space to move it down let's go up a line let's type turn speed equals 30 and then press semicolon now let's go back to this line and replace 30 with turn speed and let's also end with a semicolon now if we click off the script still does exactly the same thing except we can now change this turn speed to whatever value we want and it's much more obvious what this value is so let's unsolo this layer now by clicking this dot again and let's select this boat layer here and let's solo this layer and press play so we've got a swinging motion here so let's select this layer and again open up the rotation property and twirl down the expression so here we have two keyframes one set to -35 degrees on the rotation and one set to plus 35 degrees if we wanted to do this using keyframes we'd have to have many many keyframes where we'd have -35 35 minus 35 35 etc etc etc over and over again but you can see we've only got two keyframes here and it's just looping it so if we remove this expression here by just clicking it and backspacing and click off and now we play you can see it just stops here so we're going to add the loop out expression so if we hold alt and click on the stopwatch here next to rotation and let's try typing loop out and semicolon now if we play doesn't look quite right this is because it is literally looping the two keyframes we have so if we go from our first keyframe to our second keyframe and then instantly jump back to our first you can see it's just jumping back we wanted to go from our first keyframe to our second and then back to our first like that and then back to our second so to do that we need to add a type parameter within our loop out expression so we can click inside these parentheses here and we can type type now if we say equals and we open speech marks we can say ping pong now if we click off and we press space we have the desired behavior there are a few different types for instance we can change this ping pong to offset and now if we play what offset does is it takes the value and it just keeps going with it we can also write cycle but this is just the default option that we explored earlier so when we typed when we didn't have a type that was essentially cycle so that's when it jumps back to the start so loop out loops everything after the final keyframe let's change this back to ping pong and let's change to loop in now if we press play nothing seems to happen that's because loop in actually loops before the first keyframe so if we highlight our keyframes and drag them further along the playhead and we press play you can see it's looping before the first keyframe instead of after it so again let's go back to our boat layer and unsolo it let's take a look at the hot air balloon here so let's go to the balloon layer and let's solo that let's toggle the position property and toggle down the expression window here so we've added a wiggle expression to the position property of the balloon this essentially gives the balloon random movement let's remove this wiggle expression for now and click off and you can see now the balloon isn't moving at all let's start fresh hold alt and click the stopwatch by the position property and let's type wiggle and open close parentheses and semicolon and now let's click off now nothing happens because there are certain parameters which are actually required inside the wiggle expression a bit like when we have the type property in the loop out expression except the type property was optional whereas with the wiggle expression there are required parameters so you can see if you read the error here wiggle needs between two and five arguments so we're going to add two arguments to this so the two parameters we're going to add to this are essentially the frequency of the movement and the amplitude of the movement we had one as our frequency and then we type comma and we put five this is essentially saying that the frequency is one and the amplitude is five which means every one seconds we want our object to move by a factor of five so if we click off now and we press space you can see there's some slight movement going on here i think we should up the amplitude as the movement is very subtle here let's try 15 for the second parameter yeah that's a bit better so if someone was to look at this expression it might be a little confusing so we're going to make it a bit more readable for any potential reader so we're going to go to the start of the wiggle line and press enter to make a new line above it and we're going to make some variables here like we did with the rotation speed for the ferris wheel so we're going to make a variable called frequency and we're going to set it equal to 1 and we're going to finish with a semicolon now we're going to make a variable called amplitude and we're going to set it equal to 5. now fifty and we're going to finish with a semicolon now in our wiggle expression we can reference these variables instead of writing the value which makes it more readable for anyone looking at your expression so let's replace this one with frequency and replace this 15 with amplitude so that's essentially so these are essentially referring back to our variables here when the script is compiled this frequency will be replaced with whatever value we set here so if we click off again and we press space the behavior is exactly the same but the code is just more readable here so there's one more problem we need to solve here with the wiggle expression currently if we unsolo this layer our animation seamlessly loops so if we go to 30 seconds which is the end of our composition and we press space the balloon kind of jumps it does not seamlessly loop whereas everything else does and this is because the wiggle expression produces random movement and so the end isn't necessarily going to line up with the star so to make the animation seamlessly loop we need to ramp up the randomness at the start and then ramp it down at the end so that at either end the randomness is zero and therefore it will loop seamlessly so to do that we're going to create a null layer we're going to add a slider to it and we're going to use that slider to control the strength of the wiggle so if you click this wiggle control layer here and delete it because we're going to start from scratch now let's go to layer new null object let's rename this layer to wiggle control and let's move it above our balloon layer now let's go to effect expression controls slider control let's click on this wiggle control layer and press e to bring up our slider control let's twirl it down and here we have our slider so we're going to keyframe the amplitude using this slider so let's go to the start of the animation and we're going to keyframe zero now let's go to one second in and let's keyframe a value of 15 and then we want to do the opposite at the end of the animation so let's highlight these two keyframes ctrl c move to 29 seconds ctrl v now let's right click these keyframes keyframe assistant time reverse keyframes so now we've got a starting value of zero the slider then goes up to 15 and stays there for the majority of the animation and then at the end the slider value goes from 15 back to zero so we're going to set the amplitude to be controlled by this slider value so to do that if we go back to our balloon layer to the position expression let's select the amplitude value here which we set to 15 and we're going to highlight it then we're going to use the expression pick whip and we're going to hold and drag and drag it onto the slider value now you can see here it's added a bit of code which is a reference to that slider value to test that it's looping seamlessly now we can press end and home on a windows keyboard i'm not sure what that would be on a mac keyboard but these are just shortcuts which take you to the start and the end of the composition so we're going to press end and home and you can see that as we toggle between the start and the end of the composition the balloon is staying in exactly the same place and therefore you know that the animation is looping seamlessly and in the middle the balloon is wiggling around nicely so to help you understand the code that this bigfoot automatically generated we're going to just rewrite it ourselves so we want to get the wiggle control layer so to do this we just want to write this comp which is basically saying within this composition we want to get a certain layer so dot layer and then we need to specify the name of the layer and we do that within the parentheses so we open the parentheses we type some speech marks and we type the name of the layer so we know the layer is called wiggle control now we've got the layer but we want to get the specific slider control effect within that layer so we need to now type dot effect and open parentheses again and in here we need to type which effect it's the slider control effect so a started control effect can have multiple sliders so now we need to specify the name of the slider so we're just going to open the parentheses again and we're going to add some quotation marks and we're going to write slider which is the name here of our slider and finally we want to add a semicolon and if we now click off you can see now this is working as intended so this expression pick whip here just automatically generates this code for you but you could just as easily write it yourself okay so you may have noticed that there are these clouds slowly moving horizontally across the screen and that's also done using expressions let's go into the clouds composition down here and in here we have two precomps called cloud row so let's go into one of these and in here we simply have three shape layers all of which are stationary within this precomp so let's back out of this precomp we've actually applied our expression to the pre-comp itself so for now let's delete the first one i'll explain why later click this one and press p i've separated the position dimensions into x and y and i've added an expression to the x position so if we open up the expression for the x position and click here here is our expression it's four lines long let's scrap what we've got here so i can walk you through the process so we want this composition to move across the screen over a certain amount of time so to do that we're going to use the linear expression so if we type linear and then open and close parentheses now in here we're going to have a series of arguments or parameters a bit like we did with the wiggle statement where we had two different parameters separated by commas so the first one is going to be the speed of the animation so for now we're just going to set that to time which we covered earlier so the next parameters are going to be the start and end time of this linear movement so our composition is 30 seconds long so we're going to start at 0 and end at 30. the next parameters will be which position we want to start in and which position we want to end it so let's for instance put 0 as the start position and 1920 as the end position and 1920 is the size of our composition in width now let's add a semicolon finally to the end and click off if we click this toggle mask and shape path visibility we can see the border of our pre-comp slide through the timeline here we can see that between 0 and 30 seconds these clouds move all the way across the screen now we want this to be a seamless loop so when these clouds are here we want some more clouds to be appearing to the left so let's duplicate this composition cloud row by clicking on it and pressing ctrl d and then let's open up the expression for this layer if we want this composition to start to the left here and then finish in the center we want to change the start and end position parameters here so we want this to start at minus 1920 and finish at zero now if we click off you can see here starts to the left and they both move across like this and now if we press home and end we can see they're both exactly the same which means it loops seamlessly now these expressions are completely functional and we could just leave it there we also want to try and make it more readable for anyone else who comes across our expression and wants to understand how it's working so to do that we're going to go to the start of this linear line and press enter a few times to make some lines before and let's create some variables so let's create start time and set it to zero let's create end time and set it to 30. let's create start position and set it to minus 1920 and let's create end position and set it to zero so now let's replace these values that we put into the expression with the variables that we've just created so instead of zero we'll put start time instead of 30 we'll put end time instead of start pause we'll put start pause and instead of zero we'll put end pause now if we click off here it works exactly the same but it just looks neater and to someone reading it they understand that this zero means the start time they understand that this 30 means the end time etc now let's copy all of this go to our other precomp and delete and replace but we want to then change the start and end position to 0 and 1920 for this one and there you go these clouds now move from left to right and they move seamlessly if we wanted to control the speed of the movement we can create a speed variable and set it to i don't know three and then we can multiply this time by our speed variable by typing asterisks or multiply and then speed so the next expression we're going to look at is value at time so the best way for me to explain this is actually to create a new composition so if we come here and we press create new composition and let's type in value at time let's create a new shape layer here of a sphere and let's come down here and duplicate that layer and move it across a bit and let's change its color i don't know to something else now i want to add some opacity keyframes here so select both of the shapes press t and click opacity so we've added a keyframe here for a 100 opacity so let's just zoom in here a bit on the timeline and let's go to one second in and set a keyframe for zero opacity and then at two seconds in let's set another keyframe for 100 opacity now let's just set the end point of this composition to four seconds so let's move our cursor here to four seconds and press n on the keyboard and you can see now we have this animation where the circles go from 100 to zero to a hundred so we want to offset the opacity animation for one of these circles so it happens slightly after we could do this but this can get quite messy especially if we have many many layers and we need to manually drag all of the keyframes forward a little bit like like so and then if we have all of these animations staggered it can be quite hard to tweak the animation because say we want to highlight all of the final keyframes like it gets quite messy if we're trying to just grab these ones at the end so let's just ctrl z until we get back to our two shape layers here a better way of offsetting the animation is to use the value at time expression instead of dragging our keyframes forward what we're instead going to do is we're going to add a expression to the opacity keyframe here so if we press alt and click the stopwatch and we type here value at time and in here we simply need to specify time then plus or minus a certain value so we can move these keyframes forward in time or backwards in time so for example if we do here time minus and then we play the animation you can see that this animation is now delayed by one second and we get to keep all our keyframes nice and tidy which means if we want to make this part of the animation longer for example it's much easier than if the keyframes are staggered so let's come back to the fairground scene and open the lights composition you might have to zoom in a bit here into this composition so this is essentially what i've done for all of these light layers so in the layer view here we've got a layer for each light and then a controller for the lights on the left and a controller for the light on the right i've essentially done here what we just did in the other composition so if you highlight some of these lights and you press t to pull up the opacity and then open the expression panel you can see here we've done exactly what we did in the previous example the only difference is if you open up each of the expressions they have a slightly different value for the delay you can see here it's time minus 0.15 time minus 0.30 time minus 0.60 time minus 0.75 and so each of the lights is delayed by a different value and that's what gives it this effect so before we move on to the last example i want to show you something really useful for learning new expressions let's go back into this lights composition if we take one of these layers which has an expression on it and we twirl down the expression next to the expression here we can see an arrow which says expression language menu and in here we can see many different presets of expressions for example under interpolation we can see the expression we used earlier linear and we can see ease here and ease is just like linear except it has some e's added to the start and end and you can see the many different variants of the linear and ease expressions under key you can see the time property that we use so i want you to choose a few of these and just experiment with them and see what they do so i want you to select this sun layer here and press r and open up the expression menu and in here we've got a similar expression to what we had in the ferris wheel time times a value which just makes the sun spin but because the sun's anchor point is on the ground it spins round in orbit rather than at its center point let's look at this shadow layer here if we open the rotation expression what we've done here is we've actually set the rotation of the shadow to be equal to the rotation of the sun which is how it sort of moves around in the same way as the sun so let's delete this expression and do it from scratch so the simple way we can do it is to simply use the expression pick whip so this whip here and we can drag that onto the rotation of the sun and there we go that's done except you need to add a semicolon to the end that's the only thing and that's how easy it can be so but just to refresh your memory of the essentials let's type it out ourselves so we want to say this composition dot layer we know the layer is called sun we know the property we want to get is the rotation so we're going to type dot transform dot rotation and then we're going to add a semi colon there we go that's the same as using the pick whip if you're wondering how i've gotten the shadow to only appear where the ground is if you press the shadow layer and press e i've used an effect here called set matte and this effectively says only show this layer where another layer is present so this take matte from layer property i've set to ground and that's why it only appears where the ground is i hope this has given you a good introduction to some common expressions and how they can be used i've added some links to some reading materials in the description below if you want to learn more be sure to like the video guys as it really helps me out hit the bell for notifications of future videos and subscribe to see more content see you on the flippity flop
Info
Channel: Holmes Motion
Views: 3,304
Rating: undefined out of 5
Keywords: animation, motion graphics, after effects, premiere, adobe, extension, expressions, scripting, programmer, programming, extendscript, script, javascript, coding, code, funny animation, learn programming, how to learn programming, programming languages, adobe creative cloud, adobe cc, adobe creative suite, motion graphics tutorial, motion graphics after effects, how to do motion graphics
Id: MuXiRF3BMlw
Channel Id: undefined
Length: 22min 7sec (1327 seconds)
Published: Fri Jan 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.