Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Finding the edges of text and shape layers just got a lot easier

  • Finding the edges of text and shape layers just got a lot easier

  • Dan Ebberts

    December 17, 2014 at 10:17 pm

    With the latest AE release (CC 2014.2) expressions now have a long-awaited feature that allows them to find the edges of text and shape layers without having to do the sampleImage() hack. It’s sourceRectAtTime(), and it works the same as it does in scripting. Here are some general purpose examples for aligning a layer with the left, right, top, and bottom edges of a text layer:

    // right edge

    gap = 10;
    L = thisComp.layer(“some text”);
    r = L.sourceRectAtTime(0,true);
    x = L.position[0] + (r.left + r.width – L.anchorPoint[0])*L.scale[0]/100 + scale[0]*width/200 + gap;
    [x, value[1]]

    // left edge

    gap = 10;
    L = thisComp.layer(“some text”);
    r = L.sourceRectAtTime(0,true);
    x = L.position[0] + (r.left – L.anchorPoint[0])*L.scale[0]/100 – scale[0]*width/200 – gap;
    [x,value[1]]

    // top edge

    gap = 10;
    L = thisComp.layer(“some text”);
    r = L.sourceRectAtTime(0,true);
    y = L.position[1] + (r.top – L.anchorPoint[1])*L.scale[1]/100 – scale[1]*height/200 – gap;
    [value[0],y]

    // bottom edge

    gap = 10;
    L = thisComp.layer(“some text”);
    r = L.sourceRectAtTime(0,true);
    y = L.position[1] + (r.top + r.height – L.anchorPoint[1])*L.scale[1]/100 + scale[1]*height/200 + gap;
    [value[0],y]

    Dan

  • Tiago Cav

    June 1, 2015 at 12:18 pm

    Hey Dan, i have a problem, and is related to the Text “Anchor Point” Alignment.

    With “sourceRectAtTime()” expression, between a Shape and a Text.

    I need that the Text always stayed on top of the Shape, but as the expression is very sensitive to the letters, variations occur, and make the text is misaligned.

    I can post a link of Adobe Forum here?
    To show my problem with more details.

    Thanks.

  • Dan Ebberts

    June 1, 2015 at 6:59 pm

    I posted something in the other forum–hope it helps.

    Dan

  • Tiago Cav

    June 2, 2015 at 4:06 am

    Thank you, Dan!

  • Adam Hannis

    June 6, 2018 at 11:31 am

    Hi Dan,

    This works really well, thank you for sharing!

    I was wondering if it was possible to align a layer to the top/right using these examples?

    I’ve tried combining the top edge and right edge code but I’m a beginner when it comes to expressions so haven’t had much luck.

    Many thanks,
    Adam

  • Dan Ebberts

    June 6, 2018 at 5:16 pm

    Upper right corner would be something like this:

    gap = 10;
    L = thisComp.layer(“some text”);
    r = L.sourceRectAtTime(0,true);
    x = L.position[0] + (r.left + r.width – L.anchorPoint[0])*L.scale[0]/100 + scale[0]*width/200 + gap;
    y = L.position[1] + (r.top – L.anchorPoint[1])*L.scale[1]/100 – scale[1]*height/200 – gap;
    [x, y]

    Dan

  • Rachel Kochany

    July 27, 2019 at 3:03 pm

    Hi Dan. I’m wonder what the original ” sampleImage() hack” was?
    sourceRectAtTime() is great but I am also trying to find the edges of a raster layer, not shapes or text.

  • Sami Bayley

    November 30, 2021 at 8:42 am

    Hey Dan!

    Is there a revised version of this expression for newer versions of after effects? For some reason, when I add the “right edge” expression, even with the gap at 0, its WAY off to the right. No other expressions or parenting. Using on 2 text layers.

    Cheers!

    Another note: not sure why this happens, but when I copy and paste expressions, AE gives me errors. I just retype the whole expression myself exactly the way it was copied and it ends up working. Very odd!

  • Andrei Popa

    November 30, 2021 at 8:53 am

    About the errors: the post is old, and when the forum changed, some texts got reformated. Your errors are probably because most quotes are different.

    About the expression not working.

    You probably have a paragraph text, and not a point text. In order to ignore the empty parts of the paragraph text, you should use sourceRectAtTime with the second argument as false.

    // right edge
    gap = 10;
    L = thisComp.layer("some text");
    r = L.sourceRectAtTime(0,false);
    x = L.position[0] + (r.left + r.width - L.anchorPoint[0])*L.scale[0]/100 + scale[0]*width/200 + gap;
    [x, value[1]]
  • Sami Bayley

    November 30, 2021 at 9:23 am

    Thanks Andrei, unfortunately, still way off for some reason!

Viewing 1 - 10 of 11 posts

Log in to reply.

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