Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Layer Position Relative to Text Length

  • Layer Position Relative to Text Length

    Posted by Jason Smart on August 14, 2015 at 5:48 pm

    Hello, I am trying to find an expression that will move the position of a layer relative to the size or length of the text layer next to it.

    For example, I have a layer on the left and a layer on the right. I want to add a longer series of text to my left layer and I want the layer on the right to move it’s position and stay the same distance away from the text layer on my left.

    I hope that makes sense. Any help is appreciated!

    Tanja Burgin replied 1 year, 12 months ago 7 Members · 17 Replies
  • 17 Replies
  • Dan Ebberts

    August 14, 2015 at 6:38 pm

    This should give you the x coordinate of the right edge of the text layer:

    L = thisComp.layer(“text”);
    rect = L.sourceRectAtTime(time,false);
    x = L.toComp([rect.left+rect.width,0])[0];

    So something like this would position a layer 10 pixels to the right of the text:

    gap = 10;
    L = thisComp.layer(“text”);
    rect = L.sourceRectAtTime(time,false);
    x = L.toComp([rect.left+rect.width,0])[0];
    [x+gap+width/2,value[1]]

    Dan

  • Jason Smart

    August 14, 2015 at 7:03 pm

    WOW.

    You, sir, are amazing. Thank you so very much, that worked perfectly and will definitely save us a lot of time. Very very grateful, Dan!

  • Jason Smart

    August 31, 2015 at 11:10 pm

    Hey Dan,
    That expression was working brilliantly in CC 2014 but when I tried to use it in CC and CS6 it gave me an undefined error message. Was there anything in there that was specific to newer versions of AE?
    Thanks so much!

  • Dan Ebberts

    August 31, 2015 at 11:39 pm

    Yes, I think sourceRectAtTime() was added with version 13.2 (update to CC 2014). Previous versions have to a sampleImage() hack, which is just plain ugly.

    Dan

  • Jason Smart

    September 1, 2015 at 4:01 pm

    Ah I see, so I’m assuming it’s not as simple as replacing sourceRectAtTime() with sampleImage()?

  • Dan Ebberts

    September 1, 2015 at 4:10 pm

    No, you’d have to create a loop that starts at the right edge of the comp and moves left until it encounters non-zero alpha. You should be able to find examples here by searching for sampleImage.

    Dan

  • Jason Smart

    September 1, 2015 at 4:18 pm

    Great, thank you for all your help. It’s much appreciated!

  • Sven Heib

    September 2, 2015 at 4:51 pm

    How can I use the script, if I want my layer to stay relatively on the left side of the text.
    I tried different things but the layer doesn’t stay on one place. Thanks!
    Greetings
    Sven

  • Dan Ebberts

    September 2, 2015 at 5:37 pm

    Like this maybe:

    gap = 10;
    L = thisComp.layer(“text”);
    rect = L.sourceRectAtTime(time,false);
    x = L.toComp([rect.left,0])[0];
    [x-gap-width/2,value[1]]

    Dan

  • Andrew Musselman

    June 7, 2018 at 9:16 pm

    I know this is an old thread, and I’m not that great with expressions. This should do exactly what I need it to, but how do I use it? Where on which layer does this get pasted?

    Thanks for the help!

    L = thisComp.layer("text");
    rect = L.sourceRectAtTime(time,false);
    x = L.toComp([rect.left+rect.width,0])[0];

    So something like this would position a layer 10 pixels to the right of the text:

    gap = 10;
    L = thisComp.layer("text");
    rect = L.sourceRectAtTime(time,false);
    x = L.toComp([rect.left+rect.width,0])[0];
    [x+gap+width/2,value[1]]

    Never Stop Learning.

Page 1 of 2

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