Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Auto-aligning anchor point with “sourceRectAtTime()” aligns point incorrectly

  • Auto-aligning anchor point with “sourceRectAtTime()” aligns point incorrectly

    Posted by Pau Amer on September 17, 2021 at 12:53 am

    I have a preset with text layers animated with expressions because I’m going to use it alot and I don’t want to manually set each keyframe when text is changed.

     

    Because of this, I have 4 keyframes, two for IN and two for OUT. The text is under a shape layer that acts as a MATE.

     

    Obvsiusly, I have to set the anchor point for the text layer, wich is set at the middle of the left side of the layer using “sourceRectatTime()”. When doing this in a normal layer without animations driven by expressions, it works correctly. When I do it in this text layer, it doesn’t work properly. The anchor point is set slightly over the middle and it offsets al the layer, unaligning all the design.

     

    I attached one screencap to show the issue and make it more graphical.

     

    The code I use for the text layer is the following one (first example slides text from top to bottom, second example slides from left and goes backwards):

    And a download link for sample I made, wich has the same issue: https://drive.google.com/file/d/1iY8ik97dJst-aFUEaaMsiENi4zwWjUjR/view?usp=sharing

    // customBezier(time, startTime, endTime, startValue, endingValue, beizerPoints);
    function customBezier(t,tMin,tMax,value1,value2,bezierPoints){
        if(arguments.length!==6)return value;
        var a=value2-value1;
        var b=tMax-tMin;
        var c=clamp((t-tMin)/b,0,1);
        if(!(bezierPoints instanceof Array)||bezierPoints.length!==4)bezierPoints=[0,0,1,1];
        return a*h(c,bezierPoints)+value1;
        function h(f,g){var x=3*g[0];
            var j=3*(g[2]-g[0])-x;var k=1-x-j;
            var l=3*g[1];var m=3*(g[3]-g[1])-l;
            var n=1-l-m;var d=f;for(var i=0;i<5;i++){var z=d*(x+d*(j+d*k))-f;
                if(Math.abs(z)<1e-3)break;d-=z/(x+d*(2*j+3*k*d));
                }
            return d*(l+d*(m+d*n));
            }
        }
    var w = thisLayer.sourceRectAtTime(time, false);
    if(time<key(2).time) {
        customBezier(time, key(1).time, key(2).time, [w.left, w.height*1.20], [w.left, w.top/2], [.39,.01,.4,.99]);
    } else {
        customBezier(time, key(3).time, key(4).time, [w.left, w.top/2], [w.left, -w.height*2], [.39,.01,.4,.99]);
    }
    // customBezier(time, startTime, endTime, startValue, endingValue, beizerPoints);
    function customBezier(t,tMin,tMax,value1,value2,bezierPoints){
        if(arguments.length!==6)return value;
        var a=value2-value1;
        var b=tMax-tMin;
        var c=clamp((t-tMin)/b,0,1);
        if(!(bezierPoints instanceof Array)||bezierPoints.length!==4)bezierPoints=[0,0,1,1];
        return a*h(c,bezierPoints)+value1;
        function h(f,g){var x=3*g[0];
            var j=3*(g[2]-g[0])-x;var k=1-x-j;
            var l=3*g[1];var m=3*(g[3]-g[1])-l;
            var n=1-l-m;var d=f;for(var i=0;i<5;i++){var z=d*(x+d*(j+d*k))-f;
                if(Math.abs(z)<1e-3)break;d-=z/(x+d*(2*j+3*k*d));
                }
            return d*(l+d*(m+d*n));
            }
        }
    var w = thisLayer.sourceRectAtTime(time, false);
    if(time<key(2).time) {
        customBezier(time, key(1).time, key(2).time, [w.width, w.top/2], [0, w.top/2], [.39,.01,.4,.99]);
    } else {
        customBezier(time, key(3).time, key(4).time, [0, w.top/2], [w.width, w.top/2], [.39,.01,.4,.99]);
    }
    Pau Amer replied 4 years, 10 months ago 2 Members · 6 Replies
  • 6 Replies
  • Andrei Popa

    September 17, 2021 at 7:36 am

    I have tried your expression on a text layer and it works fine unless I use stroke. If I use any stroke, the text goes a bit off. The thicker the stroke, the bigger the difference.

  • Pau Amer

    September 17, 2021 at 7:55 am

    I have no issue when using capital letters. A text like “ABC” is not a problem, but when I do “ABCEfg” it turns weird because “g” and “f” are changing the layer dimensions. Have you tried it? I think this can be software bug…

  • Andrei Popa

    September 17, 2021 at 8:25 am

    I think the anchor point in your picture is correct, and it is the left boundary of the layer, boundary given by the dot of the i.

  • Pau Amer

    September 17, 2021 at 9:12 am

    If the anchor point it’s correct I don’t understand why it’s not positioned over the square underneath of it. I tried the “sourceRectAtTime()” without animations or anything else and it does center correctly.

    I’ve given you permission to download the files, if you want to give a look.

  • Andrei Popa

    September 17, 2021 at 10:57 am

    Oh, you meant vertically? I am sorry, I missunderstood the issue. You need to replace the

    w.top/2

    with

    w.top+w.height/2

    This means that you start fromt he top point, then add half the height of the rectangle.

  • Pau Amer

    September 17, 2021 at 11:16 am

    Thank you so much… It really helped me. I’m having a hard time learning sourceRectAtTime()…

    Thank you!

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