## Combining Linear Expressions

Scripting for After Effects

### Combining Linear Expressions

How can you combine two or more linear expressions?

Simple Example:
When I drag my slider from 0 to 1 I want an object to move from the left to the center of my comp. But when I drag it from 1 to 2 I want it to move from the center to the top of my comp.
legobenj

Posts: 32
Joined: 08/1/2008, 10:03 pm

### Re: Combining Linear Expressions

Combining Linear Expressions

To achieve what you describe you can nest a linear function inside another linear function and it could look something like this:
Code: Select all
`sld =effect("Slider Control")("Slider");p0 = [0,240];p1 = [360,240];p2 = [360,0];s0= 0;            //value of slider to acheive position zero (p0)s1 = 100;       //value of slider to achieve position one (p1)s2 = 200;       //value of slider to achieve position two (p2)linear( sld , s1, s2 , linear( sld , s0, s1, p0, p1) , p2 )`

It's also possible to add a little "ease" in your motion so instead of instantly changing direction at p1 you get a diagonal interpolation connecting the two motion paths. To try that you could change one part of the code...
Code: Select all
`linear( sld , s1, s2 , linear( sld , s0, s1, p0, p1) , p2 )`

Change the first value of s1 to something less, ie. s1 - 10.
Code: Select all
`linear( sld , s1-10, s2 , linear( sld , s0, s1, p0, p1) , p2 )`

Then when the slider is between 90 and 100 it's adding the both movements together resulting in a straight diagonal for linear() and a curve for ease().

A Different Approach

The code above will get more and more confusing with each new linear function you nest together so it's probably not the best approach if you're using more then three or four points. A different idea is to use expressions along with keyframes. Keyframes are easy to set so you can just drag the layer to the appropriate point on the screen at a specific time. That way you have easy access to make changes and wont end up with a huge list of p0 = ... , p1 = ..., p2 =..., p3 =..., etc... to specify the locations of your points.

So try this:
make several keyframes for the position of the layer so it moves around the comp.
then add this expression to the position:
Code: Select all
`sld =effect("Slider Control")("Slider");t0 = key(1).time;tn = key(numKeys).time;t = linear( sld , 0, 100, t0 , tn );valueAtTime(t)`

Now the slider is still controlling where the layer is but now you can use easy ease keyframes, and other keyframe options to control the exact movement the layer travels. And you don't have to worry about long nested code or long lists of positions and slider values. I know it's not what you asked for but it may be much better in what you're trying to do.

Hope it helps,
Cheers
ngambles

Posts: 1216
Joined: 09/20/2008, 12:47 pm

### Re: Combining Linear Expressions

multi animation with one slider

s=effect("Slider Control")("Slider");
if(s>0, s<10)
{
linear(s,0,10,[0,240],[240,360])
}
else if(s>10, s<20)
{
linear(s,10,20,[240,360],[720,800])
}
else if(s,20,30)
{
linear(s,20,30,[720,800],[1500,200])
}
vjk_vfx

Posts: 15
Joined: 05/2/2016, 11:57 pm
System Specs: i3, gigabytes
8gb ram
500 hdd