Now you’re familiar with the many parts that go into an expression,
let's look at how to put them all together to form a complete expression, step by step.
A lot of advanced expressions are comprised of three main parts that work together.
Variables, statements, and conditional rules. A good place to start is to create variables
and link the properties that you'll need.
There's no way of knowing every value you'll eventually use,
but it's good to start with the ones that you know.
So, let’s create a wiggle expression with lots of controls and advanced options.
Here, you have a sphere at the center of your composition. Start by highlighting it in the timeline,
and then adding an Expression Controls, Checkbox Control
which is going to turn the wiggle on and off.
So you can rename it and call it Toggle and go ahead and enable it.
Then, come up to Effect, Expression Controls, Slider Control, and duplicate it so we have two.
And call the first one Frequency and go ahead and set the value to something like 10.
And then I'm gonna call the second one Amplitude and I'm gonna set this to a value of something like 50.
Now, come down to the Planet layer and hit P on the keyboard to open Position.
And go ahead and add a new expression. Now first, you can remove the default expression
and create a new variable for the toggle, so tog, equals, and then I'm going to Pick Whip
the Toggle checkbox up here, and then end the line with a semi colon, move to the next line.
Set a variable for Frequency, so freq, equals, and then pick whip the Frequency slider,
end that line with a semi-colon. The third line, I'm gonna add a variable for Amplitude, so amp, equals,
and then you guessed it, pick whip the Amplitude slider, add a semi-colon at the end of the line.
And this is all the variables that we're gonna need right now.
So, once you have your variables, you can begin to form statements that perform tasks
and calculations using the variables as inputs.
These allow you to manipulate the values in your expression.
This expression has one crucial function, wiggle, so go to the next line
and start that with wiggle, open parenthesis, freq for Frequency, comma space, amp for Amplitude,
close parenthesis, and end that line with a semi-colon.
And this is gonna conclude the statements portion.
So once you have variables and statements calculating all the math, you can use
conditional rules to perform new tasks based on the outcome of the previous statements.
So, let's rearrange a few variables that are only needed if the toggle is set to true.
So after the first variable here, tog, come to the end of the line and start a new line.
Now, we're going to wrap frequency and amplitude into an if statement.
So start with if, open parenthesis, and then tog for toggle, double equals sign, and true, close parenthesis.
Now this statement will only execute if the toggle is set to true.
After this, add an open curly bracket and we're gonna wrap the remaining variables into the statement.
So come to the beginning of the next line, and I'm just gonna indent these forward two spaces,
and I'm gonna open up the expression window just a little bit,
and I'm also gonna indent wiggle two spaces.
Now, after wiggle I'm gonna create a new line and close curly bracket, and that's gonna finish the if statement.
And now, we can add an else statement to act as our fallback
So space, else, open curly bracket, and then I'm gonna go to the next line, indent two spaces, and type value
And this is just going to take the original Position value if the toggle is turned off.
So, end this with a semi-colon, next line, close curly bracket,
and this is gonna complete the wiggle expression.
So if you click away from the expression window, and if you scrub the timeline,
you'll see that the Position value is wiggling. Now, if I come forward and then turn off the Toggle,
you'll see that the Planet Position defaults to its original value.
Now, I can turn this back on and I can increase the amplitude and scrub the timeline
and you'll see an updated wiggle.
So this expression is a great way to add control to a wiggle expression
without having to come in and manually edit the frequency, amplitude, and turning it on and off.
And now, it's time for a quick bonus. Let's add a Pseudo Effect to replace
these expression controls, and then use a Popup Control combined with a JavaScript switch statement
to add some user friendly axis options.
So first, let's create the Pseudo Effect. Now here you can see I'm using Pseudo Effect Maker
which is a great plugin for creating templates.
Now, based on the existing controls,
I know I’ll need a checkbox control, two slider controls, and a new pop up control.
So here you can see I've named everything, I've set defaults, I have a couple of axis options here,
and now we can apply it to the layer. So make sure Planet is selected, and hit Apply,
and it's going to create a control. So I can close this window now,
I'm going to open up the expression window a little bit, and come to the existing expression
and make a new line at the very beginning. And set a new variable, src, for source,
and I'm going to link it to one of the controls on the Pseudo Effect.
Now here's a tip with Pseudo Effects: if you remove the property and just include the effect
and the effect name, you can use this as a reference for other areas in the expression.
So now that we have the effect object attached to src, I can come down here to the toggle
and remove effect and replace it with src, and then remove the reference to the Checkbox property.
And so now src("Toggle") is looking at src for the effect, and then looking for the Toggle property.
So this is a really clean, concise way to get a property reference.
And so now I can do this for the others, so for frequency just replace effect with src, same with amplitude,
and then remove the Slider property reference.
So that's pretty simple and if you click away, everything's going to stay linked.
And now you can remove the old properties.
And so one last step before we move on, I'm going to attach wiggle and value to an actual variable.
So I'm just going to use val, equals, and same for the value down here, val equals.
So how do we link up the X Y and Z axis controls? The popup control outputs an integer of 1, 2, 3, and 4
based on each of the four options. Now if this had a 5th line, it would output 5, so on and so fourth.
To change the output based on the selection, we're going to use a JavaScript Switch statement.
The Switch statement evaluates a condition, and then runs through each case until a condition matches,
and then outputs the result. It’s similar to an If...Else If...Else statement,
but Switch can be much more efficient when using a long list of conditions.
So after the If...Else statement, we're going to add a JavaScript Switch statement.
So go to the next line and just start this with Switch, open parenthesis, src to reference
the effect object, open parenthesis, open double quotation, and then Axis
which is the reference to the property, close double quotation, close parenthesis,
and then we're going to add period operator value, which is going to reference the 1 through 4 integer
that the popup outputs. And now close this with a close parenthesis and an open curly bracket.
Now, on the next line, indent 2 spaces and then we're going to start our first option.
So this starts with case, space, 1, and then a colon.
And now we're going to specify what the output is when the first option is selected.
So next line, indent 4 spaces, val, semi colon, next line, indent 4 spaces, and then break, semi colon.
So if the first option is selected, it's going to output our value from earlier, and then it's going to break the loop.
And now let's specify our second option. So next line, indent 2 spaces, case, space, 2, colon.
Now this expression is getting kinda long so I'm going to go ahead and full screen this
by hitting tilde and pulling this down a bit.
And now for the second option, I'm going to go to the next line, indent 4 spaces, and then start an array.
So open square bracket, val, open square bracket, 0, close square bracket, comma.
And this is going to give us just the X axis of the wiggle. And then space, value, open square bracket, 1,
close square bracket, comma, and then do this again for the third index, value, open square bracket,
2, close square bracket, and then close the entire array with another close square bracket.
Now after this, add a semi colon, go to the next line, indent 4 spaces,
and then we're going to break the loop, semi colon. So this is going to output the X axis of the wiggle,
and then the original Position value of Y and Z.
Now our third option is going to be case 3, colon, and then go to the next line, indent 4 spaces,
open square bracket, value[0], comma, and then val, open square bracket, 1, close square bracket, comma,
and finally value[2], and then close the array and close with a semi colon.
So this is option 3, or Y axis, and it's going to give us the original X axis and the Z axis,
and then brings in the Y axis from the wiggle. And so after this we're going to break the loop
and move to the final fourth option. So indent twice, case 4, colon, next line,
indent 4 spaces, open square bracket, value[0], comma, value[1], comma, and then val, open square bracket,
2, close square bracket, and close the array, semi colon.
And then we don't need a break statement here because it's the last option.
And so we're going to go to the next line and close curly bracket.
So I'm going to click away from the expression and now we have a JavaScript Switch statement
that's looking at the axis value and then providing four outputs based on the selected option.
So I'm going to close up the expression window and then hit tilde to collapse this back,
and then I'm going to scrub the timeline. So right now, all three are wiggling,
but if I come up to the Axis control and switch it to X only,
and if you pay attention to these values you'll see that the X axis is changed and the Y and the Z are default.
So if I scrub the timeline you'll see that the wiggle is only applying to the X.
If I come up here and select Y and now you can see the original X and Z axis,
but the Y is being affected by the wiggle.
And then same with the Z axis, the X and the Y are original values and the Z is being changed.
Now you also have the original frequency and amplitude sliders,
so you can put this back to X Y Z, crank the frequency, crank the amplitude,
and the expression still works as intended.
And now, you’ve successfully combined all the major components of an expression together.
We started with variables, added statements, and then provided conditions to control the output.
And you’re left with a wiggle template with user friendly options and advanced controls.
We've arrived at the end of Lesson 507: Putting Everything You've Learned Into One Expression
If you enjoy this expressions course, consider purchasing the paid content.
It includes in-depth documentation, extra tutorial content, high definition videos,
and all the project files used in the training.
Your purchase will help to create more free courses like this in the future.