Back to Blog Index

Everything About Expressions You Didn't Know...Part III: The Return of Math

No items found.

Enhance your expression knowledge with a close look at the Javascript Math, Random Numbers, and Path Properties Expression Language menus.

The Expression Language Menu holds a lot of little pieces for you to assemble. Where do you even start?! This series will walk you through each category and highlight a few unexpected items in each, leaving you better equipped to start expressing yourself via expressions.

expressions-3-article.jpg

These menus are all about working with numbers and properties. Unsurprising, maybe, as it's in the name. With these expressions you can exert some serious control of your animations without the need for tedious looping. We're going to cover:

  • Javascript Math
  • Random Numbers
  • Path Property
Article 3 Menu.png

Check Out the Full Series!

Can't express yourself enough? Check out the rest of the series:

Part 1 - Property and Effects, Layer, Key, Marker Key

Part 2 - Light, Camera, Text

Part 4 - Global, Comp, Footage, Project

Part 5 - Interpolation, Vector Math, Color Conversion, Other Math

Javascript Math

expressions-explained-part3-1.jpg

This whole section is directly from Javascript, with nothing specific to AE added in. As such, there are a million resources for it online!

With that said, we're going to cover working with it specifically for AE, focusing on:

  • Snapping numbers to the nearest whole digit
  • Getting the minimum or maximum value in a set
  • Mathemagically animating something in a perfect wave
  • For more information, see the MDN Javascript Math reference

I can already tell that you + this section will = a good, informative time, so let's explore Javascript Math.

ROUNDING NUMBERS

expressions-explained-part3-3.jpg

When we say 'rounding', we mean snapping a number with decimal places to the nearest whole number— 1.234 will round down to 1, 2.5 will round up to 3, 133.7 will round up to 134, etc.

Most of the time you'll want to use this when displaying text on screen without having a bajillion trailing digits, but one cooler use is to pair it with Time to have a number snap every second.

In this example, we'll apply time to the second hand of a stopwatch. First, we'll show this without the rounding:

const multiplier = 5;
time * multiplier;

You can see that the hand is smoothly moving in a circle. This is what we'd expect! Time moves smoothly, so the clock hand moves smoothly too.

But if we round this value, the clock hand will only snap every second, as opposed to evenly moving:

const multiplier = 5;
Math.round(time) * multiplier;

Now it's worth pointing out that this will snap at the midway mark between seconds, as opposed to the start; for more accuracy, use Math.floor() in place of Math.round() if you decide that really matters.

FINDING HIGHEST AND LOWEST VALUES

expressions-explained-part3-2.jpg

Let's say you've got a bunch of different numbers, and want to find the largest one of the group...or the smallest!

There are a few clever (and a lot of not-so-clever) ways to accomplish this, but the built-in Math.min() and Math.max() functions are by far the simplest.

In this case, we've got a few cars on a racetrack, and we'd like to indicate which car is currently winning without needing to do any sort of manual keyframing work on our label.

We can accomplish this by first getting all of the X positions of each car, feeding those into Math.max(), and using that value to drive the position of our label.

const car1X = thisComp.layer("Car 1").position[0];
const car2X = thisComp.layer("Car 2").position[0];
const car3X = thisComp.layer("Car 3").position[0];
const car4X = thisComp.layer("Car 4").position[0];
const car5X = thisComp.layer("Car 5").position[0];

Math.max(car1X, car2X, car3X, car4X, car5X);

If we wanted to attach our label to the losing car, we can simply swap Math.max() with Math.min() and that'd be that!

EASILY WAVING

expressions-explained-part3-4.jpg

If you're looking for some sinfully, sinuously smooth curves, you've come to the right place.

We can use the Math.sin() function to generate a sine wave from any number, which is preeeetty much just a nerdy way to smoothly sway a number from -1 to +1, forever and ever and ever.

NOW that may not sound exciting, but it is! Er, it can be!

By controlling the wave with time—which always increases—we can have it repeat forever at a consistent speed. By multiplying the result by, say, 30, we can have it go from -30 to +30, forever. By multiplying the speed higher, we can make it move faster!

All this to say: this is a function to automate extremely controllable waving animation for us with no keyframes. This metronome has no keyframes, but will tick and tock all day long:

const speed = time * 2.1;
const amount = 30;

Math.sin(speed) * amount;

No need for looping expressions, a billion keyframes, or anything of the sort.

Adding to Good Times

All of this together clearly adds up to some good times. No longer will you need to divide your focus or subtract distractions from your workday; you can practically calculate the benefits right from the get go.

From zero to AE hero, Javascript Math will keep you even. Or odd. Care for some Pi, anyone?

Random Numbers

expressions-explained-part3-5.jpg

Sometimes in AE you want to mix things up a little, create some chaos—that's where the Random Numbers category comes in!

Of course, we don't want too much chaos—that'd be reckless. Thankfully, we can use the functions in this category to control the exact limits to our randomness.

We’ll look at:

So with that said, let's dig into 41, 20, 110, 0.1231... sorry, got carried away. Let's dig into Random Numbers!

GENERATING NEW RANDOM NUMBERS WITH SEEDRANDOM()

expressions-explained-part3-6.jpg

Now, we all know how to get some smooth random motion-- that's wiggle()! You've seen this before so we're gonna skip over that.

Instead, I want to answer a common question that comes up:

"I like the overall feeling of the movement, but I want it... slightly different. Is this possible?"*

*okay I've never actually been asked this, BUT it's something that I've personally faced.

To solve this we'll use seedRandom(), which effectively lets us take a dice roll, and... roll it again. We're still working with a 6-sided die (that's a "d6" in geek parlance), we just want to get a different result!

Now, this is a special type of expression. You don't assign it to anything—it just sits at the top of your expression and modifies everything below...like an adjustment layer for your code, modifying anything beneath it.

To generate a brand new wiggle without needing to make new layers, we can put this expression onto position, and every time we change the number in the seedRandom() we'll get a whole new wiggle:

seedRandom(12345);
wiggle(0.5, 100);

GENERATING NEW RANDOMNESS PER LAYER

expressions-explained-part3-7.jpg

In this sample, we've got a whole bunch of layers and we want each one to have a uniquely random opacity...using ONLY ONE EXPRESSION!

To accomplish this, we'll use our friend seedRandom() from above but add in a second option, timeless, which tells AE to lock in the values it's got.

And instead of giving in a number for the first option, we'll give it index, which is the layer's # in your timeline.

We'll add this to each layer's Opacity to get this done:

seedRandom(index, true);
random(0, 100);

rannum1.png

That's, like, totally random!

This is only a short look at what Random Numbers can do for you! There are plenty of other random functions here to take a look at, all with their own peculiarities.

Path Property

expressions-explained-part3-8.jpg

Shape layers are big these days. With the amount of 2D-centric animation flying about, a lot of emphasis gets placed on shape paths.

This section is going to look into some of the possible ways to use expressions with both shape & mask paths, unlocking some cool features that would otherwise be inaccessible or just fairly tedious to build by hand.

We'll look at:

Without further ado let's walk down this path and explore the Path Property submenu.

COPYING SHAPE PATHS BY EXPRESSIONS

expressions-explained-part3-9.jpg

All path properties have a special function called points(), which returns all of the positions of all of the points on a path!

With this, we can have one layer grab the points from another layer, and create a path from that.

const driverLayer = thisComp.layer("Driver");
const driverPath = driverLayer.content("Path 1").path;
const driverPoints = driverPath.points();

createPath(driverPoints);

Aaaaaaaand presto! Two paths for the price of one.

ATTACHING LAYERS TO SHAPE PATHS

future-of-education-13.jpg

One of the most common AE tasks that expressions can solve is being able to take a layer and attach it to a shape path.

Now, the built-in 'nulls follow shapes' panel can help with this, but it can be fairly clunky and bloated when you're just trying to accomplish something simple.

We can build the main part of this using pointOnPath(), a function that you give a specific % of completion along the path, and it'll return the X and Y coordinates of the corresponding point on the path at that percent.

Think of it like this—a straight line from [0, 0] to [100, 0]. 0% of the way along is [0, 0]. 50% of the way is [50, 0], and 100% is [100, 0].

That math is easy, but if we have some long windy path, we can't really compute this ourselves!

const driverLayer = thisComp.layer("Driver");
const driverPath = driverLayer.content("Path 1").path;
const progress = effect("Progress")("Slider");

const pointOnPath = driverPath.pointOnPath(progress);
driverLayer.toComp(pointOnPath);

Now we can animate a simple slider, and have our other layer follow the path all the way along it.

CONNECTING COMP LAYERS WITH SHAPE PATHS

expressions-explained-part3-11.jpg

Not only can we get path info and stick things to it, we can actually make our own brand spankin' new paths right from an expression!

Now this expression has got a lot going on, but the idea is that we can leverage a function called pointOnPath() and pass it in a list of points, and it's going to make a brand new shape path for us.

In this example, this expression on the Path property is going to connect all of the layers in our comp with a new shape, that will always be updating no matter where and how our other layers move.

const points = [];

for (let ii = index + 1; ii <= thisComp.numLayers; ii++) {
 const layer = thisComp.layer(ii);
 const layerPos = layer.position;
 const layerPosInShapeSpace = fromComp(layerPos);

 points.push(layerPosInShapeSpace);
}

createPath(points);

As we add or remove layers, the path will update live to connect ALL the dots.

WALK YOUR OWN PATH

expressions-explained-part3-12.jpg

The Path Property submenu is one of the more recent additions to AE, and this is just a small snippet at what it offers.

Now that we've seen how to clone paths, fix things to them, and make them from scratch, you'll be better aware of the possibilities when dynamic paths come up in your own projects.

Expression Session

If you're ready to dive into some radioactive goop and gain a new superpower, don't do that! It sounds dangerous. Instead, check out Expression Session!

Expression Session will teach you how to approach, write and implement expressions in After Effects. Over the course of 12 weeks, you'll go from rookie to seasoned coder.

EXPLORE ALL COURSES

Dive into real-time 3D with our Unreal Engine beginner's course by Jonathan Winbush. Master importing assets, world-building, animation, and cinematic sequences to create stunning 3D renders in no time! Perfect for motion designers ready to level up.

Explore this Course

Unlock the secrets of character design in this dynamic course! Explore shape language, anatomy rules, and motifs to craft animation-ready characters. Gain drawing tips, hacks, and Procreate mastery (or any drawing app). Ideal for artists seeking to elevate their craft.

Explore this Course

Elevate your freelance motion design career with our guide to client success. Master a repeatable method for finding, contacting, and landing clients. Learn to identify prospects, nurture leads, and develop a thriving freelance philosophy amidst chaos.

Explore this Course

Rev up your editing skills with After Effects! Learn to use it for everyday needs and craft dynamic templates (Mogrts) for smarter teamwork. You'll master creating animated graphics, removing unwanted elements, tracking graphics, and making customizable templates.

Explore this Course

Stand out with Demo Reel Dash! Learn to spotlight your best work and market your unique brand of magic. By the end, you'll have a brand new demo reel and a custom campaign to showcase yourself to an audience aligned with your career goals.

Explore this Course

Illuminate your 3D skills with Lights, Camera, Render! Dive deep into advanced Cinema 4D techniques with David Ariew. Master core cinematography skills, gain valuable assets, and learn tools and best practices to create stunning work that wows clients.

Explore this Course

Master After Effects at your own pace with Jake Bartlett's beginner course. Perfect for video editors, you'll learn to create stylish animated graphics, remove unwanted elements, and track graphics into shots. By the end, you'll be equipped for everyday AE needs and more.

Explore this Course

Revolutionize your Premiere workflow with customizable AE templates! Master creating dynamic Motion Graphics Templates (Mogrts) in After Effects to speed up your team's work. By the end, you'll craft easily-customizable templates for seamless use in Premiere Pro.

Explore this Course
Your download is in your inbox!!!
Check your email (spam, too) for the download link!
Please check the spam folder if you don't see the message within a minute or two. Google likes to hide your downloads, sometimes.
Oops! Something went wrong while submitting the form.

Not sure where to start?

If you’re a beginner, here are some great courses to help you get started:

After Effects Kickstart

Dive into the fundamentals of motion design with our most popular (and recently updated) After Effects course.

LEARN MORE

Photoshop + Illustrator Unleashed

Master the basics of Photoshop and Illustrator and gain invaluable insights in this introductory level course.

LEARN MORE

Design Kickstart

An introduction to the design principles behind all great work.

LEARN MORE

More Advanced?

If you’re a more advanced student looking to up your game, here are some great options:

Animation Bootcamp

Learn the art and principles of creating beautiful movements in Adobe After Effects.

LEARN MORE

Design Bootcamp

Learn to design for motion in this intermediate-level, project-based course.

LEARN MORE

Cinema 4D Basecamp

Learn Cinema 4D from the ground up in this exciting introductory C4D course.

LEARN MORE

Now is the time to learn the skills you need to advance in your motion design career: