ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Expression that finds the distance that a layer has traveled

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Christopher Bly
Expression that finds the distance that a layer has traveled
on Feb 21, 2019 at 11:00:10 pm

I'm currently working on an expression that would be applied to the size of a rectangle shape layer. The end result should be the rectangle being as long as the distance that the layer has traveled over a number of previous key frames. Currently I have written the expression below, but it creates an error and I have no idea why. Could someone please help me out by explaining what I'm doing wrong? Thanks!

Note: For some reason, a < symbol in my for loop is not appearing correctly when I paste it here, so be sure to change that back if you paste this expression into After Effects for yourself

function getDistanceTraveled(){
var pos = thisLayer.toWorld(thisLayer.anchorPoint);
var sFrame = framesToTime(1,1/thisComp.frameDuration);
var tSample = Math.ceil(effect("Time Sample (Frames)")("Slider"));

var dist = 0;
for(i = 0; i &lt; tSample; i++){
let tOne = time - (sFrame * i);
if (tOne &lt; 0) {timeOne = 0;};
let tTwo = tOne - sFrame;
if (tTwo &lt; 0) {tTwo = 0;};
let xDist = Math.abs( pos.valueAtTime(tOne)[0] - pos.valueAtTime(tTwo)[0] );
let yDist = Math.abs( pos.valueAtTime(tOne)[1] - pos.valueAtTime(tTwo)[1] );
dist += Math.sqrt( Math.pow(xDist,2) + Math.pow(xDist,2) );
};

return dist;
};

var x = getDistanceTraveled();
[x,0];


Return to posts index

Dan Ebberts
Re: Expression that finds the distance that a layer has traveled
on Feb 22, 2019 at 1:37:39 am

I think the most immediate problem is that toWorld() doesn't have a valueAtTime() method, but it has a time parameter (check the help doc). Also, your dist calculation has xDist in there twice, but not yDist.

BTW, when you post code, don't preview your post, or it will eat your < symbols.

Dan



Return to posts index

Alex Printz
Re: Expression that finds the distance that a layer has traveled
on Feb 22, 2019 at 3:08:11 pm

Dan is right it's the toWorld;

toWorld is a calculation, not a property, so "pos" it cannot check it's value at a different time since it has no 'time', it's just a number.

You must get your correct time calculated you want to check prior to a world position conversion, and then put it as a comma after your position to check, eg:

target.toWorld(target.anchorPoint,targetTime)

Try this:

function getDistanceTraveled(){
var T = thisLayer;
var sFrame = framesToTime(1,1/thisComp.frameDuration);
var tSample = Math.ceil(effect("Time Sample (Frames)")("Slider"));

var dist = 0;
for(i = 0; i < tSample; i++){
let tOne = time - (sFrame * i);

if (tOne < 0) {timeOne = 0;};

let tTwo = tOne - sFrame;

if (tTwo < 0) {tTwo = 0;};

var posOne = thisLayer.toWorld(thisLayer.anchorPoint,tOne);
var posTwo = thisLayer.toWorld(thisLayer.anchorPoint,tTwo)var
let xDist = Math.abs( posOne[0] - posTwo[0var];
let yDist = Math.abs( posOne[1] - posTwo[1] );
dist += Math.sqrt( Math.pow(xDist,2) + Math.pow(xDist,2) );
};

return dist;
};

var x = getDistanceTraveled();
[x,0];


Alex Printz
Mograph Designer


Return to posts index

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