Auto-Resize Shape Layer with Text Size

This After Effects expression is used to dynamically adjust the dimensions of a shape layer’s rectangle to match the dimensions of a linked text layer, with an added margin for spacing. Here’s a breakdown of how it works:

margin = 50;

text_layer = thisComp.layer("REPLACE") //pick whip to desired text layer

text_width = text_layer.sourceRectAtTime().width;
text_height = text_layer.sourceRectAtTime().height;

shape_width = text_width + margin*2;
shape_height = text_height + margin*2;

[shape_width, shape_height]
  1. Margin Setup:
    • margin = 50;: This line sets a variable called margin to 50, which represents the desired spacing around the text layer. You can adjust this value to increase or decrease the spacing as needed.
  2. Linking to Text Layer:
    • text_layer = thisComp.layer("REPLACE"): This line links the expression to a text layer named “REPLACE” within the same composition. You can replace “REPLACE” with the name of the actual text layer you want to link to.
  3. Calculating Text Dimensions:
    • text_width = text_layer.sourceRectAtTime().width;: This line calculates the width of the linked text layer at the current time using the sourceRectAtTime() method.
    • text_height = text_layer.sourceRectAtTime().height;: This line calculates the height of the linked text layer at the current time using the same method.
  4. Calculating Shape Dimensions:
    • shape_width = text_width + margin*2;: This line calculates the width of the shape layer’s rectangle by adding the text width to twice the margin. The margin is added on both sides of the text to create the desired spacing.
    • shape_height = text_height + margin*2;: This line calculates the height of the shape layer’s rectangle in a similar way, by adding the text height to twice the margin.
  5. Outputting Dimensions:
    • [shape_width, shape_height]: This line outputs an array containing the calculated width and height of the shape layer’s rectangle. These dimensions will dynamically adjust based on the dimensions of the linked text layer and the specified margin.

In summary, this expression allows you to create a shape layer whose rectangle automatically adjusts to match the dimensions of a linked text layer, with a customizable margin for spacing. It provides a convenient way to ensure that shapes and text remain properly aligned and spaced within your After Effects compositions.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *