Basic Expressions: Automatic Shadow
- Oct 9, 2016
- 4 min read
The internet is your best friend. I’m no programmer, but the ability to use After Effects Expressions can help automate some of the work for you, instead of having to keyframe every single little thing.
PremiumBeat has an excellent blog post about some easy After Effects expressions you can use in your day-to-day work, and you can find a lot more around the web, so I won’t bore you with the basics. Instead, I thought we could continue with where I left off last time: Bouncing balls.
At the end of this whole thing is a download link to the .aep file containing the rig, so you can skip to that if you’re only interested on the end result. Let’s start with something easy:
HOW TO RIG AUTOMATED SHADOWS
Problem: Having to spend time animating a bouncing ball, then spend more time animating a shadow to follow it around.
Solution: Have the shadow move where the ball moves, and also grow/shrink appropriately to simulate the ball’s height.
STEP 1: SET UP THE OBJECTS
Start by making the ball and the shadow using the Ellipse Tool. Make a perfect circle for the ball, and an oval for the shadow (so it looks like it’s at an angle on the ground). Make sure the anchor points are at the center of the objects.

Click P on the Ball layer to bring up the Position property, right-click on it and choose “Separate Dimensions”. This will come into play later, as you want to control the X and Y position of the ball independently.
Name the Ball layer “Ball” and the Shadow layer “Shadow”. You can change this to whatever you want, but you have to change the expressions on each layer accordingly.
STEP 2: SET THE SHADOW'S POSITION TO FOLLOW THE BALL'S POSITION
Add the expression below to the Position property of the Shadow layer:
ball_xPos = thisComp.layer("Ball_01").transform.xPosition;
def_yPos = transform.position[1];
[ball_xPos, def_yPos]
The first line creates a variable called “ball_xPos”, and sets that variable’s value to the x position of the ball. It may seem like a lot of words, you can drag the pickwhip from this expression to the ball’s x position instead of typing it out.

The second line just sets the variable “def_yPos” to the current yPosition. Again, you can use the pickwhip for this instead of typing it out.
The third line tells the expression to set the first and second value of the shadow’s position property to the two variables we created. Test it out. When you move the ball, the shadow should now follow it horizontally, but not vertically.

STEP 3: SHADOW GROWS WHEN THE BALL IS CLOSE TO THE GROUND, AND SHRINK WHEN FURTHER
Just like bouncing a ball in real life, the closer it is to the ground, the bigger the shadow. Now we’re going to automate that too. First off, let’s make a Slider to we can set some values without having to go back into the expression. This is a good habit to have, as moving some sliders in the Effect Controls panel is much easier than having to go into the code and manually changing some values.

To do this, select the Ball layer and add a Slider Control by going Effect > Expression Controls > Slider. Make three of these, one named “Min. Ball Height”, “Max.Ball Height" and another named “Shadow Scale Factor”. This is to let our code know what’s the min/max height the ball can reach and a multiplier for the shadow’s scale (more on that later). Why is this important? Because we don’t want our shadow to keep scaling to oblivion.
Add the expression below to the Scale property of the Shadow layer:
def_yScale = transform.scale[1];
ball_yPos = thisComp.layer("Ball").transform.yPosition;
ball_yMin = thisComp.layer("Ball").effect("Min. Ball Height")("Slider");
ball_yMax = thisComp.layer("Ball").effect("Max. Ball Height")("Slider");
scale_factor = thisComp.layer("Ball").effect("Shadow Scale Factor")("Slider");
if(ball_yPos > ball_yMin){
ball_yPos = ball_yMin;
}
if(ball_yPos < ball_yMax){
ball_yPos = ball_yMax;
}
new_xScale = ball_yPos/scale_factor;
[new_xScale, def_yScale]
Let’s break it down.
def_yScale = transform.scale[1];
ball_yPos = thisComp.layer("Ball").transform.yPosition;
ball_yMin = thisComp.layer("Ball").effect("Min. Ball Height")("Slider");
ball_yMax = thisComp.layer("Ball").effect("Max. Ball Height")("Slider");
scale_factor = thisComp.layer("Ball").effect("Shadow Scale Factor")("Slider);
The first line creates variable “def_yScale” and sets it to the current yScale of the Shadow. This effectively locks the yScale to whatever you have it currently.
The second line creates variable “ball_yPos” and sets it to the current yPosition of the Ball. This just tracks the yPos of the ball within this new expression.
The third, fourth and fifth lines creates variables that are set by the three Slider Controls you created earlier. Now, the value of these variables will follow the values on those Slider Controls.
if(ball_yPos > ball_yMin){
ball_yPos = ball_yMin;
}
if(ball_yPos < ball_yMax){
ball_yPos = ball_yMax;
}
Now comes some math. These are called Conditional Statements. You’re telling the program “if A, do B. If X, do Y.” We’re going to tell the shadow that “if the yPosition of the Ball layer exceeds the Min. Ball Height, the ball_yPos variable will stop counting and just be limited to the minimum height value.”

The second line does the same for the maximum value. The shadow will now shrink as the Ball goes further from the ground, but will stop scaling down up to a point, the maximum height. It will also grow as the Ball goes closer, but will stop when it reaches the minimum height.
new_xScale = ball_yPos/scale_factor;
[new_xScale, def_yScale]
Finally, the two lines that actually tell the Shadow what to do.
You’re telling the Shadow to have a yScale of the default (which you have set earlier and will not change), and an xScale that follows that mathematical equation.
Now, trying to explain how this expression works is a bit complicated, I know. Honestly, it’s not the sort of thing that can be explained on a blackboard. It’s the sort of thing you have to fiddle around yourself with. Most websites will only give you the code, but I hope a brief explanation at least helps you understand WHY you write the code in such a way.

Last but not least, here’s a link to Download the Automatic Shadow Rig from this tutorial. I suggest you fiddle around with it, or if you want something done quickly, just copy-paste the codes or even the whole layers to your new project.
In the future, we'll continue rigging the bouncing ball by adding an easy Squash / Stretch slider, and a way for the ball to roll whilst still squashing / stretching in the appropriate direction.




Comments