How to write an expression to rotate at the end of a motion path?

Community Beginner ,
Jul 04, 2020

Copy link to clipboard

Copied

Hello,

I have an animation that is of someone walking in an sitting down. Imagine a 'J' type motion path that auto-orients a person to the path - so that they are looking where they are going. When the person gets to his seat, I would like the rotate that person so that they are looking at the stage. I have about 30 people that I need to do this with, so, instead of manually rotating I would like to have a script that does this.

Imagine walking into a play and going to your seat. You have to turn to sit in your seat. This is what I'm looking to do.

Bonus if you can tell me how to, at the end of the motion path, I can break out of a loopout() and go to the next frame to get into the sitting pose.

Thanks
Kevin

- edit -
I'm new to After Effects scripting, but not javascript syntax. Any resources to learn would be awesome.

Adobe Community Professional
| Adobe Community Professional

If you have Auto-Orientation set to Orient Along Path and the motion path is ending as shown then all you have to do is add two rotation keyframes to the person waling layer.

If you need the person to turn to face the stage and then move back to the seat then you should add a null, parent the layer to the null, set the null to Orient Along Path, turn off Auto-Orientation for the person walking layer use the Null to move the person, then add the rotation keyframes, then animate the position of the person walking layer to move it back to the seat. Something like this:

Trying to write an expression to keep everything on one layer is going to get complicated. You'll have to use the time of the last keyframe as the starting point for a rotation animation based on a linear method, throw in an extremely complex auto orient to motion path expression with an if statement, and then move the person walking back to their seat.

If the person walking layer is not going to get any more position keyframes then you could save an animation preset for just the rotation turn and apply it when you move the CTI to the last position keyframe. That would make all layers rotate the same direction.

If all of the person walking layers are going to have the same number of keyframes and they just stop then turn to the stage, and you want the ones on the top side of the aisle to rotate counter-clockwise and the ones below the center of the aisle to rotate clockwise, you could write an expression that would rotate the layers like this:

``````rStart = position.key(3).time;
t = time - rStart;
if (t < 0)
r = value + 0
else
r = linear(t, 0, .5, 0, 90)
if (transform.position[1] < height/2)
value - r
else
value + r
``````

• rStart is the third and last keyframe in the motion path. If you have 4 keyframes then rStart should be changed to rStart = position.key(4).time;
• t = starts counting time in seconds at the keyframe specified in rStart
• The if statement simply says if t (the time counter) is less than zero, the r-value will be the existing rotation value. The else statement says otherwise as the time in seconds (t) increases from zero to .5 (a half-second) scale the r-value from zero to 90.
• The second set of if statements says if the Y value of the position property of this layer is less than half the height subtract r from the rotation value to rotate the layer counter-clockwise but if the position value is not (else) less than half the height of the comp add r to the rotation value.
• To change the time it takes to rotate change the .5 in the linear method, to change the number of degrees the layer rotates, change the 90. To ease in and out of the move change linear to ease. To ease in on the rotation change linear to easeIn. To ease out of the rotation change linear to easeOut.

That should let you know how the expression works.

TOPICS
Expressions, Scripting

Views

210

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to write an expression to rotate at the end of a motion path?

Community Beginner ,
Jul 04, 2020

Copy link to clipboard

Copied

Hello,

I have an animation that is of someone walking in an sitting down. Imagine a 'J' type motion path that auto-orients a person to the path - so that they are looking where they are going. When the person gets to his seat, I would like the rotate that person so that they are looking at the stage. I have about 30 people that I need to do this with, so, instead of manually rotating I would like to have a script that does this.

Imagine walking into a play and going to your seat. You have to turn to sit in your seat. This is what I'm looking to do.

Bonus if you can tell me how to, at the end of the motion path, I can break out of a loopout() and go to the next frame to get into the sitting pose.

Thanks
Kevin

- edit -
I'm new to After Effects scripting, but not javascript syntax. Any resources to learn would be awesome.

Adobe Community Professional
| Adobe Community Professional

If you have Auto-Orientation set to Orient Along Path and the motion path is ending as shown then all you have to do is add two rotation keyframes to the person waling layer.

If you need the person to turn to face the stage and then move back to the seat then you should add a null, parent the layer to the null, set the null to Orient Along Path, turn off Auto-Orientation for the person walking layer use the Null to move the person, then add the rotation keyframes, then animate the position of the person walking layer to move it back to the seat. Something like this:

Trying to write an expression to keep everything on one layer is going to get complicated. You'll have to use the time of the last keyframe as the starting point for a rotation animation based on a linear method, throw in an extremely complex auto orient to motion path expression with an if statement, and then move the person walking back to their seat.

If the person walking layer is not going to get any more position keyframes then you could save an animation preset for just the rotation turn and apply it when you move the CTI to the last position keyframe. That would make all layers rotate the same direction.

If all of the person walking layers are going to have the same number of keyframes and they just stop then turn to the stage, and you want the ones on the top side of the aisle to rotate counter-clockwise and the ones below the center of the aisle to rotate clockwise, you could write an expression that would rotate the layers like this:

``````rStart = position.key(3).time;
t = time - rStart;
if (t < 0)
r = value + 0
else
r = linear(t, 0, .5, 0, 90)
if (transform.position[1] < height/2)
value - r
else
value + r
``````

• rStart is the third and last keyframe in the motion path. If you have 4 keyframes then rStart should be changed to rStart = position.key(4).time;
• t = starts counting time in seconds at the keyframe specified in rStart
• The if statement simply says if t (the time counter) is less than zero, the r-value will be the existing rotation value. The else statement says otherwise as the time in seconds (t) increases from zero to .5 (a half-second) scale the r-value from zero to 90.
• The second set of if statements says if the Y value of the position property of this layer is less than half the height subtract r from the rotation value to rotate the layer counter-clockwise but if the position value is not (else) less than half the height of the comp add r to the rotation value.
• To change the time it takes to rotate change the .5 in the linear method, to change the number of degrees the layer rotates, change the 90. To ease in and out of the move change linear to ease. To ease in on the rotation change linear to easeIn. To ease out of the rotation change linear to easeOut.

That should let you know how the expression works.

TOPICS
Expressions, Scripting

Views

211

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 04, 2020 0
5 Replies 5
Adobe Community Professional ,
Jul 04, 2020

Copy link to clipboard

Copied

It would really help if we knew how you setup your animation. Are the layers 2D? Are you using Puppet Pina? Character Animator? A layered PSD or AI file?

If this were my project I would have separate body parts, point the head to a null using vector math in an expression, and have the body parts all parented to the torso and move the torso with a Null.

Show us a screenshot of what you have now with the modified properties of the layers that you are working with revealed. Select the layer, press 'uu' the take a screenshot an embed it in the forum so we understand your workflow. Somebody will then be able to point you to an efficient workflow.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 04, 2020 0
Community Beginner ,
Jul 04, 2020

Copy link to clipboard

Copied

Hello,

I understand what you are saying and thank you. Give me a minute to get all of this together. Your advice is much appreciated.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 04, 2020 0
Community Beginner ,
Jul 04, 2020

Copy link to clipboard

Copied

Below is what I'm working on. My end-goal is to illustrate how coronavirus can be spread in a crowd of people - like a church. I'll have someone 'infected' (to be outlined in red) spread the virus to others (visible particles of COVID and when they 'infect' someone, that person turns red). This is part of a larger presentation - but this is the most animation intensive. I plan on exporting this through Bodymoving and placing it on a website.

What I would like to do is have the characters walk in and sit down. But before a couple of them take their seats, they will meet and talk - as you would usually do in a church setting.

On the left, 2/3 is the animation that I'm working on. It's a top-view of the church. It's not the best, but I think it gets the point across. And that's the whole idea of this - to get the point across and keep it simple.

The right side is Illustrator with the characters I'm using as a base in After Effects that are brought to AE via Overlord. The only items on the character that is animated are the shoes. They simply move back and forth. Exciting, I know. When the characters get to their seat, which is at the end of the motion path, I would like for them to turn and face the front of the church. If I could place an expression that detects the end of the motion and has them turn to face the front, that would be great.

I do have a sitting position for the character so they can sit on a pew.

My end-goal is to illustrate how coronavirus can be spread in a crowd of people - like a church. Yes, I plan on adjusting design, this is just to get the animation down. Everything, so far, works as a little animation. I'm hoping to keep it this way.

This is part of a larger project regarding the spread of Covid including the structure, how it infects the body, and how it's distributed.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 04, 2020 0
Adobe Community Professional ,
Jul 04, 2020

Copy link to clipboard

Copied

If you have Auto-Orientation set to Orient Along Path and the motion path is ending as shown then all you have to do is add two rotation keyframes to the person waling layer.

If you need the person to turn to face the stage and then move back to the seat then you should add a null, parent the layer to the null, set the null to Orient Along Path, turn off Auto-Orientation for the person walking layer use the Null to move the person, then add the rotation keyframes, then animate the position of the person walking layer to move it back to the seat. Something like this:

Trying to write an expression to keep everything on one layer is going to get complicated. You'll have to use the time of the last keyframe as the starting point for a rotation animation based on a linear method, throw in an extremely complex auto orient to motion path expression with an if statement, and then move the person walking back to their seat.

If the person walking layer is not going to get any more position keyframes then you could save an animation preset for just the rotation turn and apply it when you move the CTI to the last position keyframe. That would make all layers rotate the same direction.

If all of the person walking layers are going to have the same number of keyframes and they just stop then turn to the stage, and you want the ones on the top side of the aisle to rotate counter-clockwise and the ones below the center of the aisle to rotate clockwise, you could write an expression that would rotate the layers like this:

``````rStart = position.key(3).time;
t = time - rStart;
if (t < 0)
r = value + 0
else
r = linear(t, 0, .5, 0, 90)
if (transform.position[1] < height/2)
value - r
else
value + r
``````

• rStart is the third and last keyframe in the motion path. If you have 4 keyframes then rStart should be changed to rStart = position.key(4).time;
• t = starts counting time in seconds at the keyframe specified in rStart
• The if statement simply says if t (the time counter) is less than zero, the r-value will be the existing rotation value. The else statement says otherwise as the time in seconds (t) increases from zero to .5 (a half-second) scale the r-value from zero to 90.
• The second set of if statements says if the Y value of the position property of this layer is less than half the height subtract r from the rotation value to rotate the layer counter-clockwise but if the position value is not (else) less than half the height of the comp add r to the rotation value.
• To change the time it takes to rotate change the .5 in the linear method, to change the number of degrees the layer rotates, change the 90. To ease in and out of the move change linear to ease. To ease in on the rotation change linear to easeIn. To ease out of the rotation change linear to easeOut.

That should let you know how the expression works.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 04, 2020 2
LATEST
Community Beginner ,
Jul 05, 2020

Copy link to clipboard

Copied

Hi Rick,

Thank you for the detailed explanation as this has been a great intro to scripting in After Effects. While I didn't use your exact code, I went through it, googled, and come up with something that applies to the changes in animation I have made. Thank you, sir!

I have more questions, but I'll post them in the main forum. I do appreciate your help - it has led me in the right direction!

Kevin

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 05, 2020 0
Resources
Troubleshooting FAQs
After Effects Community Recap
Getting started with After Effects