Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Expression that finds the distance that a layer has traveled

  • Expression that finds the distance that a layer has traveled

    Posted by Christopher Bly on February 21, 2019 at 11:00 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];

    Alex Printz replied 7 years, 2 months ago 3 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    February 22, 2019 at 1:37 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

  • Alex Printz

    February 22, 2019 at 3:08 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

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy