ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

following a path with an expression to swap compositions based on path direction, orientation or rotation?

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Dany Targa
following a path with an expression to swap compositions based on path direction, orientation or rotation?
on Jul 3, 2017 at 5:14:34 am

Say I have an object following a path, is there an expression to automatically swap to another composition or frames within a composition based on path direction?

So if the object is moving on a downward path, the object automatically swaps to the downward comp which would be the first line of sprites in the below image?

Or if the object rotates 90 degrees the comp will swap to the left composition on the second line of the below image.

If you don't know the expression, any advice on where to look to find out more about this would be much appreciated.



Return to posts index

Scott McGee
Re: following a path with an expression to swap compositions based on path direction, orientation or rotation?
on Jul 3, 2017 at 9:31:20 am

If someone can come up with a way to do it based on position that would be a better way to go, but off the top of my head with rotation.

I've not worked with sprites before, but if it's just a .png sequence. Separate them into there own comps Up, Down, Left, Right. Then precomp them altogether.

Then apply something like this to the opacity

for Up
I = thisComp.layer("Rotation").transform.rotation;
if (I == 0) 100 else 0;


for Left
I = thisComp.layer("Rotation").transform.rotation;
if (I == 90) 100 else 0;


for Down
I = thisComp.layer("Rotation").transform.rotation;
if (I == 180) 100 else 0;


for Right
I = thisComp.layer("Rotation").transform.rotation;
if (I == 270) 100 else 0;


Adding this to your rotation
transform.rotation % 360
So that it ignores 1x before it, otherwise it won't work.

But there is probably a better way to expression the position to do this, but with my skill set, the above would be how I would do it currently.


Return to posts index

Charlie Laud
Re: following a path with an expression to swap compositions based on path direction, orientation or rotation?
on Jul 3, 2017 at 2:52:59 pm

Here's what I came up with!

Basically if you precompose all the sprites into one preComp, (each sprite gets its own frame), you can enable time remapping on that preComp layer and use some lines of code to "tell" it to cycle through each different set of sprites depending on the direction your sprite is moving. That's as simple as using the valueAtTime() to find the difference between some position value at "current frame" and "current frame - 1".

So precompose all those sprites in one layer. Right click on the layer and select Time> Enable Time Remapping and add this code:

directionNum = 0;
dX = transform.xPosition.valueAtTime(time) - transform.xPosition.valueAtTime(time - framesToTime(1));
dY = transform.yPosition.valueAtTime(time) - transform.yPosition.valueAtTime(time - framesToTime(1));

if (dX > 0) {directionNum = 8;}
if (dX < 0) {directionNum = 4;}
if (dY > 0) {directionNum = 0;}
if (dY < 0) {directionNum = 12;}
framesToTime(directionNum + (timeToFrames(time) % 4))


NOTE: you'll have to Separate Dimensions on the position value, and it doesn't seem to be too happy with anything other than linear keyframes. For some reason keeping the dimensions together was giving me some big ol trouble.

Hope this helps! Or is at least enough to get you started!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]