Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Dynamic Text scaling to fit inside of a shape ?

  • Dynamic Text scaling to fit inside of a shape ?

    Posted by Joe Wood on June 1, 2017 at 9:19 pm

    I am trying to figure out the expressions to make a text layer automatically resize to fit inside of a shape layer or any other layer for that matter.

    I know this is possible as I have seen it work in the new essential graphics panel in Premiere, where when you change the text, it automatically resizes to fit an area.

    I found some stuff about the “sourceRectAtTime” expression, and it works but it does not constrain proportions.

    Any advice?????

    // grab sourceRect based on layer time
    R = sourceRectAtTime(sourceTime(time), true);

    // calculate scale relative to comp
    w = thisComp.layer("Shape Layer 1").content("Rectangle Path 1").size[0] / R.width * 100;
    h = thisComp.layer("Shape Layer 1").content("Rectangle Path 1").size[1] / R.height * 100;

    [w,h]

    Paul Connors replied 6 years, 2 months ago 6 Members · 7 Replies
  • 7 Replies
  • Dan Ebberts

    June 1, 2017 at 10:58 pm

    Try this:

    L = thisComp.layer(“Shape Layer 1”);
    rL = L.sourceRectAtTime(time,false);
    r = sourceRectAtTime(time,false);
    hisUL = L.toComp([rL.left,rL.top]);
    hisLR = L.toComp([rL.left+rL.width,rL.top+rL.height]);
    myUL = toComp([r.left,r.top]);
    myLR = toComp([r.left+r.width,r.top+r.height]);
    w = (hisLR[0]-hisUL[0])/(myLR[0]-myUL[0]);
    h = (hisLR[1]-hisUL[1])/(myLR[1]-myUL[1]);
    [value[0]*w,value[1]*h]

    Dan

  • Kevin Camp

    June 1, 2017 at 11:04 pm

    to constrain the proportions, you just need to use the same value for x and y scale… if you wanted to fit to either the height or width, you could use a Math.min() function .
    // grab sourceRect based on layer time
    R = sourceRectAtTime(sourceTime(time), true);

    // calculate scale relative to comp
    w = thisComp.layer("Shape Layer 1").content("Rectangle Path 1").size[0] / R.width * 100;
    h = thisComp.layer("Shape Layer 1").content("Rectangle Path 1").size[1] / R.height * 100;

    s = Math.min ( w, h );

    [ s, s ]

    Kevin Camp
    Art Director
    KCPQ, KZJO & KRCW

  • Ricky London

    August 24, 2018 at 3:34 pm

    Hi Guys I am trying to put this expression on my text layer scale property and i’m getting this error:

    error at line 5 property ‘scale’ of layer (text) in comp 1.

    property or method named rectangle path 1 in class ‘group’ is missing?

    any ideas where I am going wrong?

    Thanks,Ricky.

    // grab sourceRect based on layer time
    R = sourceRectAtTime(sourceTime(time), true);

    // calculate scale relative to comp
    w = thisComp.layer("Shape Layer 1").content("Rectangle Path 1").size[0] / R.width * 100;
    h = thisComp.layer("Shape Layer 1").content("Rectangle Path 1").size[1] / R.height * 100;

    s = Math.min ( w, h );

    [ s, s ]

  • Dan Ebberts

    August 24, 2018 at 4:24 pm

    Is your shape layer not a rectangle?

    Dan

  • Wes Kennison

    February 28, 2019 at 2:15 pm

    Hey Ricky, any chance you figured this out? I’m having an identical issue with the line 5 expression error not being able to reference the Rectangle path though it’s very clearly a rectangle. Any input would be hugely appreciated, thanks!

  • Joe Wood

    March 1, 2019 at 3:32 pm

    Did you try tying to the scale of the shape layer itself instead of the scale of the rectangle?

    // grab sourceRect based on layer time
    R = sourceRectAtTime(sourceTime(time), true);

    // calculate scale relative to comp
    w = thisComp.layer("Shape Layer 1").transform.scale[0] / R.width * 100;
    h = thisComp.layer("Shape Layer 1").transform.scale[1] / R.height * 100;

    s = Math.min ( w, h );

    [ s, s ]

  • Paul Connors

    February 22, 2020 at 2:37 pm

    Hi Kevin. I can’t tell you how many times I’ve used this expression in the past. It’s one of my favorites! I did have a question for you though … do you know if there is a way to ignore ascenders and descenders in words that have accents like Ç or Á ? Thanks very much!

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