toComp() and toWorld()

weight-slider drag

These two expressions are beyond useful. The way they work finally clicked in my head recently. (I had a good idea of what toComp() did because it’s an expression that a lot of tutorials use. Drawing a line between two 3D nulls with the beam effect, for instance.)

toWorld() does essentially what toComp() does, but with the third dimension. So, knowing that– what is it that they do? Well, I was just about to explain it! Calm down. Holy Ravioli.

These two expressions basically calculate the whereabouts of a layer after it has been parented to another layer. Let’s think about it in terms of nulls….

We have NullA, NullB, NullC. All 3D layers. We’re going to parent NullB to NullA… and then move NullA around. We find that the values in the Position property of NullB don’t change at all. This being the case, if we want NullC to be exactly where NullB is (using expressions), we can’t simply expression-pick-whip to NullB’s Position– because that number doesn’t fully represent where NullB is in space.

toWorld() is built for this problem.  We can use something like thisComp.layer("NullB").toWorld([0,0,0]) in the Position property of NullC to access the point in space where NullB is located– after NullA has been moved around, rotated, and scaled.

I’ve started to use toWorld() in place of transform.position in some of my expression set-ups, to save myself the headache later on– in the event some of my layers need to be parented to other layers.

Hey! Let’s do something useful now!

Here is an expression that makes a layer travel between two other layers, based on the value of a slider (between 0 and 1, more specifically).

THE SETUP

We’ll use NullA, NullB, and NullC. Let’s unparent NullB from NullA (though, it doesn’t need to be unparented). NullC is going to exist between NullA and NullB. Where NullC exists (Closer to A? Closer to B?) will be driven by a Slider Control. We’ll call it weight. We’re going to want to edit the Slider Range in the  “Edit Value…” window, so instead of being set to the default 0 to 100, it will now be 0 to 1. This will just make it easier to control when we’re dragging this value.

Let’s get our keyboard fingers dirty! (Ugh. Gross.) We’re adding this expression to the Position property of NullC. I’ll paste the finished product here, and then I’ll explain it.

a=thisComp.layer("NullA").toWorld([0,0,0]);
b=thisComp.layer("NullB").toWorld([0,0,0]);
wh=thisLayer.effect("weight")("Slider");
aW=a*wh;
bW=b*(1-wh);
aW+bW

There are two parts, essentially. First, we’re setting up variables for NullA, NullB, and our weight Slider. Second, we’re using a bit of math to calculate the positions between NullA and B. To simplify the idea, consider the following: if you wanted to just have NullC exist exactly in the middle of A and B, you’d do something like this:

a=thisComp.layer("NullA").toWorld([0,0,0]);
b=thisComp.layer("NullB").toWorld([0,0,0]);
(a+b)/2

Which would be the same thing as this:

a=thisComp.layer("NullA").toWorld([0,0,0]);
b=thisComp.layer("NullB").toWorld([0,0,0]);
(a*.5)+(b*.5)

To vary C’s proximity to either A or B, we’re just affecting those .5’s– making sure they always add up to 1. If we want the C to be closer to A, we’ll just give that side more “weight”– .8, let’s say… and that would leave .2 on the B side. In our expression, this bit is handling that math:

aW=a*wh;
bW=b*(1-wh);

The exciting part is that the values BEYOND 0 and 1 on the slider will give you positions for NullC that are still in-line with NullA and NullB.

That’s it! Post questions if you have them!

I’ve started a GitHub account— so I’ll be publishing the expressions I post here to “Gists”, in case there’s ever a problem copy/pasting my code from WordPress.

Published by thatsmadden

I'm an animation artist with an interest in code-driven motion.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: