Creative Communities of the World Forums

The peer to peer support community for media production professionals.

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

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

    Posted by Bart Remmers on May 13, 2016 at 1:37 pm

    Hi guys,

    I’ve been messing around with this but I still haven’t got to the results that I wish it to be.

    I’ve got a text layer (in a bounding box). But the content (the source text) may change later. It’s been rigged with an thisComp.layer(“TEXT MASTER”).text.sourceText, so I can easily change this later.

    Behind the text-layer there is a shape (rectangle) and I want it’s width to get larger + some extra space around it, because that looks nice. Let me show you:

    So to sum it up: I want the width size of the rectangle behind the text to automaticly scale itself depending on the width of the text layer.
    And after that I still want to animate it (with the anchorpoint in the middle, or at the far most right) to have it scale up from 0 to 100%).

    Does anyone have a clue how I can get this done? I’m quite new at expressions, I roughly know the basics, this is why I’m seeking for help.


    Ter Ber replied 3 years, 9 months ago 14 Members · 23 Replies
  • 23 Replies
  • Kalleheikki Kannisto

    May 13, 2016 at 4:12 pm

    For rectangle size:

    margin = 25;
    text_width = thisComp.layer("my_text").sourceRectAtTime().width;
    text_height = thisComp.layer("my_text").sourceRectAtTime().height;
    box_width = text_width + margin*2;
    box_height = text_height + margin*2;
    [box_width, box_height]

    for rectangle position:

    text_height = thisComp.layer("my_text").sourceRectAtTime().height;
    [0, text_height/2]

    It works with multiline as well.

  • Bart Remmers

    May 16, 2016 at 9:39 pm

    Well, it might be a beginners mistake, but I can’t seem to figure this out…
    I’ve done exactly as you said but I think I still did this thing wrong.
    It gives me errors when adding the expressions to the scale/position of the rectangle.
    One of these errors are displayed below. I think I named my layers right. I did name my text layer for this example ‘my_text’.

    I’m sorry for messing up, haha, still a rookie.

  • Bart Remmers

    May 16, 2016 at 9:40 pm

    Sorry, I attached the wrong picture. Here’s the right one. I can’t seem to edit my last post.

  • Dan Ebberts

    May 16, 2016 at 11:35 pm

    I notice you’re using CC 2014. Is it the 13.2 update? I think that’s when sourceRectAtTime() was added.


  • Bart Remmers

    May 17, 2016 at 7:44 am

    Thank you for replying so fast!
    Yeah, that’s it! I’ve been working on an older version (without knowing). I just updated to 13.2 instead of 13.0.2 and now it does work.
    Only too bad still something goes wrong. The rectangle (which has an anchor point in the mid-centre of it) now positions itself with the anchor in the far upper right corner of the composition, and is far to wide. But the size DOES change the way I want it too, so I’m on the way to achieving what I’m looking for. Maybe I’m still doing something wrong. I’ve been trying other values aswel in the expression, but I still don’t get what I’m trying for.

    Thanks again!


  • Dan Ebberts

    May 17, 2016 at 1:39 pm

    Sizing and positioning a shape layer is tricky because, as with a text layer, you have to use sourceRectAtTime() to get its extents. Try this for the shape layer’s position:

    t = thisComp.layer(“my_text”);
    tRect = t.sourceRectAtTime(time,false);
    tUL = t.toComp([tRect.left,]);
    tLR = t.toComp([tRect.left+tRect.width,]);
    tCenter = (tUL + tLR)/2
    myRect = sourceRectAtTime(time,false);
    myUL = toComp([myRect.left,]);
    myLR = toComp([myRect.left+myRect.width,]);
    myCenter = (myUL + myLR)/2
    delta = myCenter – tCenter;
    value – delta

    Try this for the shape’s scale (not the layer’s scale, the one in the Transform: Rectangle 1 section):

    t = thisComp.layer(“my_text”);
    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)/(myLR[0]-myUL[0]),(tLR[1]-tUL[1]+ 2*margin)/(myLR[1]-myUL[1])]*100


  • Bart Remmers

    May 18, 2016 at 12:06 pm

    Dan, thank you very much for helping me out with this.

    That code really looks like a massive math project. Phew! How do you do it?! Haha!

    I’ve been struggling with this for a few days now. I’m trying to make my projects somewhat easier to edit for later, like working with a Mastercomp where the texts can be changed later on and some colors that are used in the animation, you never know. This way I can make my animations more variable.

    Thanks again! I really appreciate this!


  • Matthew Mammola

    May 25, 2016 at 2:28 pm

    This is awesome! Thanks y’all! At first, when using Dan’s latest script there, I was having an issue with the box not aligning properly to the center of the text, and then I set my Rectangle anchor point to 0,0 and all was perfect with the world.

    Of course, my text and box are centered absolute in the comp, so that may not be helpful to other purposes.

    Thanks for the awesome scripting!

  • Kalleheikki Kannisto

    May 26, 2016 at 5:19 pm

    My method expects the shape box to be drawn and placed in a similar fashion to how you want it to look, correctly placed and sized to the text.

    Starting setup project:

  • 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 😀

Page 1 of 3

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