Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Changing Shape Layers size (width) due to text width

  • Changing Shape Layers size (width) due to text width

  • Tim Drage

    May 27, 2016 at 9:14 am

    Incredible thanks 🙂

    Could be better if the second expression, instead of changing the shape’s scale, controlled the rectangle path size, that way any round corners wouldn’t be distorted. I’ll try and figure it out myself but the last line of that expression is a bit much for me this morning 😀

  • Tim Drage

    May 27, 2016 at 9:25 am

    … OK i figured it out myself; this in Rectangle 1>Rectangle Path 1>Size will make the box resizing happen before the round corners and other effects. 🙂

    t = thisComp.layer(“tessssstr”);
    tRect = t.sourceRectAtTime(time,false);
    tUL = t.toComp([tRect.left,]);
    tLR = t.toComp([tRect.left+tRect.width,]);
    myRect = sourceRectAtTime(time,false);
    myUL = toComp([myRect.left,]);
    myLR = toComp([myRect.left+myRect.width,]);
    margin = 10;
    [(tLR[0]-tUL[0] + 2*margin),(tLR[1]-tUL[1]+ 2*margin)]

    Thanks again Dan for another great solution 🙂

  • Nuno Trindade

    December 10, 2016 at 11:36 am

    Many thanks

    Can you help me in a simple thing?

    If i wont to adjust the text, not in the center, but aligned to the left?

    I already download your example… but if i change the alignment of my_Text… the box don’t move…


  • Multipasser

    November 6, 2017 at 1:35 pm

    Damn, no reply ☺

    want also LEFT aligned box!

  • Emilio Le Roux

    April 16, 2018 at 1:54 am

    Thank you very much, I was facing a similar problem and this solved it.

    Just a tip for everyone – since I am creating lots of these self adjusting boxes, I decided PARENT the boxes to their respective text layers and then replace:

    text_width = thisComp.layer(“my_text”).sourceRectAtTime().width;


    text_width = parent.sourceRectAtTime().width;

    And so on with the rest of the code. This way you can duplicate both text and box several times and as far as the box is parented to the right text, it will have the right size.

  • Alberto Garcia

    August 20, 2018 at 5:16 pm


    This is not a solution but a workaround. What I did was to simply mask the shape layer to get cropped on the left side (or whichever side you wish). I also repositioned the shape so its center was on top of the beginning of the editable text.
    I changed the code a bit as well to match my design:

    margin = 0;
    text_width = thisComp.layer("my_text").sourceRectAtTime().width*2;
    text_height = thisComp.layer("my_text").sourceRectAtTime().height/6;
    box_width = text_width + margin*2;
    box_height = text_height + margin*2;

    [box_width, box_height]

    Hope this helps.

  • Adam Leverett

    July 9, 2019 at 4:11 pm

    I’m trying to use this code for a text layer for subtitles, so as the voiceover changes, I have another text keyframe and I’d like the box to adjust as the text changes in width. Seems like the code only is seeing the last keyframe text width size. I’m assuming this is because the code doesn’t execute every time there is a new keyframe…is there a way to do this?

  • Tomas Bumbulevičius

    July 9, 2019 at 7:05 pm

    Adam, try adding time value of the keyframe into sourceRectAtTime(), like:

    textLayer = thisComp.layer("my_text");
    textLayer .sourceRectAtTime(textLayer.key(X).time, false).width;

    Where X – your keyframe index inside text layer.

    With the same logic, if you only keep it ‘time’ only, instead specifying the exact keyframe, it should update automatically.

    Find out more:
    After Effects Tutorials: motion design, expressions, scripting.
    Boxer – Dynamic Text Boxes Template with a Live Preview

  • Huntington Carpenter

    July 18, 2019 at 4:33 am

    Is there a way to separate the width so it stays at a static length but the height still changes?

  • Ter Ber

    July 21, 2020 at 8:20 pm

    Thanks so much for this. I only have a minor request for you guys:
    How can I scale or change the rectangle’s margin proportionally with the text size?

    Thanks again for helping.

Viewing 11 - 20 of 24 posts

Log in to reply.

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