Skip to main content
Participant
January 12, 2023
Question

Auto-resizing shape

  • January 12, 2023
  • 2 replies
  • 409 views

I have a shape layer that resizes to my text (width), the issue I'm having is that it does not resize when making more than one line of text (height) - the text ends up outside of the box. 

This is something I'm editing that was already created - here is the expression currently

 

Expression: Size

w = thisComp.layer("Kane Cam").sourceRectAtTime().width;

h = thisComp.layer("Kane Cam").sourceRectAtTime().height;

margin = 60;

 

[w+margin,90]

 

Expression: Anchor Point

s=thisLayer.sourceRectAtTime(); [s.left,s.top+s.height]

 

Many thanks

This topic has been closed for replies.

2 replies

Community Expert
January 12, 2023

Your expression declares a variable for height that is not used in the size array. Change the last line of the size array to [w + margin, h + margin]

 

I don't see any that ties the shape layer's position to the text layer's position.  You will also need that. 

 

The complete solution to creating a shape layer that stays attached to a text layer above it would use thisComp.layer(index - 1) instead of thisComp.layer("Layer Name") so you could add the expression to any shape layer below a text layer. You need an expression on the Rectangle Size, Rectangle Position, and Shape layer Position to keep the Shape Layer Rectangle lined up with the Text layer.

 

Here's how I would do it:

// Shape Layer/Contents/Rectangle1/Size
src=thisComp.layer(index - 1);
b = src.sourceRectAtTime(); // size of the text box
xPad = 60;// could be a alider
yPad = 50;// could be a slider
x = b.width + xPad;
y = b.height + yPad;

[x, y]

//Shape Layer/Contents/Rectangle 1/Position
src=thisComp.layer(index - 1);
b = src.sourceRectAtTime(); // size of the text box
t = b.top;
l = b.left;
x = b.width/2 + l;
y = b.height/2 + t;

[x, y]

//Shape Layer/Transform/Position
src=thisComp.layer(index - 1).position;

thing 

Mathias Moehl
Community Expert
Community Expert
January 12, 2023

[w+margin,90] says that the height is fixed to 90 pixels.

Maybe you try

[w+margin,h]

or

[w+margin,h+margin]

 

Also note that this iExpression could be very useful:

https://mamoworld.com/after-effects-expression/auto-resizing-background-shape

 

And here is a related tutorial for the even more complex case of the shape being around multiple texts or other layers:

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Participant
January 12, 2023

Thanks for your reply, unfortunately neither of those worked...

The [w+margin,h+margin] does resize the box as I type more lines, but instead of it following the text down, it actually scales upwards instead and my text still hangs off the bottom of the box.