linear(); | After Effects Expressions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey it's Jake and I'm really excited because today we're gonna be talking about one of my favorite expressions the linear expression and the reason why I like it so much is because a lot of times when you're making custom controls for yourself and after effects you can run into issues and it can get kind of frustrating but then the linear expression comes along and goes I got you so what does linear allow you to do well I've set up a demo here that's hopefully going to give you a very good visual representation of the possibilities that the linear expression gives you so we've got this grid that comes on all these pixel values and then a line so what are we looking at here this is a grid that's spaced out by 50 pixel increments I've marked the hundred pixel increments here and you can see those little tick marks on the rulers and then we have a line connecting these two dots this dot is driving this dot on the y axis so if I were to grab this and move it up and down that's what's gonna happen it's just gonna follow it and that line is gonna stay straight across between the two it's just connecting them so if I continue on my timeline you see that it moves up and down and it goes up to 100 pixels and then drops down to 900 pixels and the dot on the right is just following whatever it does so here are the position keyframes you can see it just move up and back down and it's doing exactly the same thing now the way that I've set this up to follow that is by going into the position so let me bring up position and look at just the Y and I've written a variable the variable is just referencing the Y position of the other layer which is named input I'm calling this layer output and this layer input so we've got variable input equals and then the Y position for the input layer and then I just simply call that variable down here in my expression so that the Y position of the output is always the same as the input great but that's not all that useful in its simplest form what the linear expression allows you to do is remap a range of values to a different range of values so I'm gonna get rid of my size and stroke width by holding down option or alt and shift and then clicking on them to hide those properties so I can focus on just the white position this input layer starts at 540 right in the middle of the comp this is a 1920 by 1080 comp so it starts right in the middle of the comp and then it goes up to 100 pixels on the y-axis and then drops down to 900 pixels on the y-axis and again the output is mirroring exactly that same number you see up here that number changes with the input but let's say when this number goes down to 100 we don't want the output to go all the way down to 100 what if we wanted it to only go to say 400 and then when the input goes up to 900 we only wanted to travel to say 600 well that's exactly what linear is going to allow us to do so I'm gonna go back to my output layer into this expression and rewrite it a little bit so I'm gonna get rid of input and I'll start writing out the linear expression the format of it is linear then an open parenthesis then the property that we want linear to be dealing with in this case it's the inputs Y position and since I have that as a variable already I can just type in input and then we need to do four numbers and we separate everything by commas so after input I'll put a comma and then we need two numbers this is our first range and it's a range of this input of whatever you put in first in this linear expression so the range we want to look at is the Y position from 100 to 900 because that's what these keyframes go to it first goes up to 100 then down to 900 so I'll just type in 100 and this is the minimum value that we're going to be looking at the input for then another comma and we need to put the max value which would be 900 pixels so say 900 and that's our first range the minimum and maximum values of our input then we need to put another comma and two more numbers and this is going to be the output range so we can think about it like this when the input is set to 100 100 pixels what do we want the output to be set to while we said 400 pixels when this is at 100 this one should be at 400 so I'm going to type in 400 and then a separate that by another comma and now we need the maximum value of the output which we said would be 600 we'll type in 600 then close that off with a parenthesis and finish it off with a semicolon that is our complete linear expression so we want to linearize the input value which is the Y position for the input layer that's what we define in our variable and we want to linearize the number ranges from 100 to 900 on the input range to an output range of 400 to 600 I'll click off to apply that expression in already this updates so I'll play this back and we'll see what happens look at that so now when this input value goes up to 100 the output value is only going up to 400 and when it goes down to 900 it only goes down to 600 so that's how you can think about linear it allows you to remap one range of values to any other range of values and these values can be whatever you want right now we're only looking at the input of 100 to 900 so if I were to bring this up past 100 you'll notice that right there once I get past the 100 this number stops moving so if this was up to 0 it's gonna ignore everything between 0 and 100 then it'll kick in and then once it gets to 900 it's gonna stop paying attention and I can keep going as far as I want it's never going to affect this so instead of 100 and 900 I could say 0 and 1080 since that's the height of my comp this number around see it keeps traveling with it so once it hits zero is when that goes to 400 and if I push that all the way to 1080 that's when it gets to 600 to make this even a little more dynamic for this second number instead of 1080 I could say this comp dot height and that's going to just measure the comp ight now that happens to be 1080 but if I were to resize this comp to say 800 by 800 or something like that then it's gonna update and that makes it a little bit more dynamic and let's say I wanted to invert it I could instead of saying 400 and 600 I could say the output minimum value would be this comp dot height and the maximum value would be zero now what's gonna happen is this is going to be an inverted number of this because we're looking at the range from zero on the minimum to the comp aiight on the maximum and translating it to the comp aiight on the minimum and zero on the maximum so let's play this back and see what happens now those are going to move in opposite directions so these range values can be any number it doesn't have to be just a hard-coded number it could even be a variable if you wanted it to be it could be equations you can even have linear equations as one of these range or values it's incredibly powerful and it opens up so many possibilities when writing your expressions and if you're wondering how this line was built I actually didn't do it this is a preset from the konig's mixtape from paul Conigliaro I think I'm pronouncing that right but this is just a pack of presets that he's developed that are just incredibly useful and it's name your own price the guy put a lot of work into this and he's actually helping me out with my own expressions personally so definitely go support this guy Paul's awesome alright now they've gotten a basic understanding of how this linear expression works I've set up another demo here to kind of get you thinking about the possibilities that this opens up so I just have a simple square and if we take a look at the rotation this property's measured in degrees right so going from 0 to 360 is a full revolution it rotates 360 degrees let's say that I wanted to control that rotation but with a slider so I'm going to come up to effect expression controls slider control which defaults to having a value between 0 and 100 now this does nothing on its own it's just a value driven by a slider that gives me the ability to reference it using expressions so I want to write an expression on the rotation I'll hold out an option or alt and click on the stopwatch and then just grab the expression pick whip and select that slider control now the rotation is being driven directly by this slider control so if I turn it up and see my wrote my box is rotating and I can turn that all the way up to 100 problem is that is a direct relationship between those two values 0 to 100 gives me a range of 0 to 100 degrees what if I wanted this 0 to 100 to equal a full rotation you can kind of think of the slider as a percentage since it goes from 0 to 100 at 0 it should be at 0 degrees and at 100 it should be at 1 full rotation well that's a perfect candidate for the linear expression so let's go ahead and write that I'll edit my expression and since I already have this in here I'll turn it into a variable at the front I'll type var and then just our four rotation equals that slider semicolon to finish that line and then drop down and type in my linear expression so linear open parenthesis R and that is going to reference the slider that we just added comma and then we'll put our first range the minimum and maximum input values of the rotation slider so we want this to be a range from 0 to 100 and then we need our output minimum and maximum so we'll do another comma and say 0 is at the minimum we still want the value to be 0 degrees but at the maximum comma we want it to be at 360 degrees I'll close that off semicolon to finish it and apply the expression now when I rotate this you can see that it goes from 0 to 0 and as I turn this up it's rotating rotating rotating once it gets to 100 then it's done a complete rotation 360 degrees everywhere in between is a different angle so that's a great example of how you can use the linear expression to control different values with custom-built controls even though the numbers aren't even really in the same units the linear expression allows you to translate those numbers into whatever you need them to be so I'll rename the slider just rotation and then I'll duplicate it and we'll make a slider that's called exposition and let's tie the exposition of this square to that as well so press P to bring that up and on the exposition hold down option or alt and click and then define that variable so var X for X position equals expression pick whip that slider and semicolon to finish that variable drop down and type out the linear and then I'll put in X to target that slider comma and the range I want to deal with is still 0 to 100 because that is the range of this slider then I'll do another comma and I'll say 0 on the X comma and then this comp dot width close parenthesis semicolon so what I'm telling after-effects to interpret is when the slider value is at 0 put the value of the exposition to 0 and when it's at 100 then make the exposition value equal to this comps width which is 1920 so this should push the box across the screen as I move the slider I'll apply it and it jumps over there because my X position is set to 100 I grab that slider I can now reposition the box from the left edge of the comp to the right with a simple slider that again you can think of like a percentage so if I set it to 50 it's gonna go right in the middle of the comp so now I have two sliders affecting two different properties but when I put this all the way down to zero half of my box is off to the side same thing on the opposite side what if I wanted to contain this within the comp well we can do that pretty easily with this linear expression all we need to know or all we need to tell after effects is what the size of this box is so I'm going to open up this layer go into the contents into the rectangle into the rectangle path and there's my size I'll just get myself a little bit more room and then add another variable to my x position expression so I'm going to drop down a line and type in var W for width equals and then expression pick whip the first number in the size which is the width of that box and I'll press semicolon to finish that so now I have a variable for the exposition slider and the width of the box so in my linear expression I can now look at my second range which is going from zero to the comps width instead of zero I want to be half the size of the box because the anchor points right in the middle so I only need half of that width to be accounted for so I'm going to get rid of that zero and type in W divided by two so half of the width and then on the second value the maximum output where the box is right now I want it to be this comp width minus the width of the box divided by two so in the minimum output instead of zero I want it to be half of the box's width and then on the output I want it to be the comps width minus half the box so that it stays inside I'll apply that and look at that now my box is lined up perfectly with the edge of the comp zoom in here and move my slider now that's staying right inside the comp perfectly and I'll double tap on the keyboard to bring up the expressions option shift click on the properties I don't need to look at and that's working exactly the way that I want it to but if i zoom in here nice and close you can see that this boxes shape is being buttered up right against the side of the comp but the stroke is actually getting caught off well we can actually account for that too so I'm going to collapse this layer one more time go in and find the stroke there's my stroke width and give myself a little bit more space again and I want to write another variable into this exposition expression I'll drop down one more line var asks for stroke equals and then I'll expression pick whip the stroke width and finish that off with a semicolon now you'll notice that this stroke is centered so I'm only going to need half of that stroke just like I only needed half of the box and with that in mind I can keep editing my expression so I want the minimum output to be not just the width divided by two but the width divided by two plus the stroke divided by two I'll click off that and look at that my box is shifted over to the right just barely to account for that stroke width and I need to make sure that I do the same thing on the other side so the maximum output will be the width of the comp minus half of the box minus half of the stroke by zoom back out to fit and test this out see the full width of the stroke and to show you why this is so powerful if I now just adjust the stroke of the box you'll see that it is dynamically getting pushed away from the edge of the comp because that linear expression is accounting for that size and if I double tap you to bring up all my modified properties I can easily increase the size of the box and the same thing is going to happen because my linear expression is paying attention to the width of both the box and the stroke and accounting for that within the position value and that's all being driven by this single x position slider and one last thing I want to point out is that you don't have to have these values in a linear expression paying attention to an entire range so this slider goes from 0 to 100 on the rotation but if I were to edit this in the look at the minimum input and maximum input I don't have to say from 0 to 100 just because that's the minimum maximum of my slider I could say from 25 to 75 on my min and Max input and leave output to zero in 360 now nothing is going to happen on this rotation with this slider until it hits 25 right there and then between 25 and 75 is where that complete rotation is going to happen and nothing after 75 is gonna affect it so again the linear expression just opens up so many possibilities and there's so many different ways that you can implement it into your own graphics to make custom rigs that can a lot of times make animations easier because once they're set up you can focus on the animation and allow After Effects to do a lot of the complex calculations for you in the background so there's your crash course basics intro to the linear expression hopefully through those demos you're able to see just why it's so valuable and why I'm using it constantly for doing things like rigging up controls and making these relationships between different properties just so much more easy to manage so if you're using this linear expression at all be sure to tag me anywhere on social media at Jake in motion so I can see you have any trouble at all feel free to leave a comment and I'll try and help you work that problem out but now that we have a pretty good understanding of this linear expression we can use it in other projects and I'm definitely going to be referencing it a lot in other tutorials so thank you so much for watching and I'll see you in the next one
Info
Channel: Jake In Motion
Views: 30,177
Rating: undefined out of 5
Keywords: After Effects, Animation, Motion Graphics, Mograph, Motion Design, Tutorial, Adobe, Adobe After Effects, expressions
Id: 2h9ZfUHVG6Q
Channel Id: undefined
Length: 18min 1sec (1081 seconds)
Published: Fri Apr 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.