Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

How to make a shape layer responsively adapt to the length of two different text layers?

Community Beginner ,
Jul 12, 2023 Jul 12, 2023

I am working on creating a dynamic lower third, where the shape layer container grows/shrinks automatically, based on the length of the type inside. Currently I'm using the expression below, so the shape layer (L3 Box) is affected by the text layer (Artist Name). This is currently working just fine. That said, I also have a second text layer (Artist Title), that I want the box to respond to as well. For instance, if the Artist Title ends up being longer than the Artist Name, the box won't be big enough. How would I update or change my expression so it listens to both text layers?

 

s=thisComp.layer("Artist Name")

w=s.sourceRectAtTime().width;

h=s.sourceRectAtTime().height;

[w,h]

TOPICS
Expressions , How to , Scripting
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 12, 2023 Jul 12, 2023

You need to use sourceRectAtTime height and width for both text layers and add them together, plus the padding between the lines for the height, and use the longest of the two layers for width plus padding. This will give you the size of the shape layer background.

 

Then you need to tie the position of the second text layer, your subtitle, by adding the padding and the height + the top for vertical position and then use width + left + padding to position the shape layer horizontally. 

 

If you use index - 2 to define the first text layer and index - 1 to define the second text layer and put the shape layer below both text layers, these expressions will do the trick:

 

 

//Shape Layer/Contents/Rectangle 1/Size
//source layers 
src1 = thisComp.layer(index - 2);
src2 = thisComp.layer(index - 1);
// text size
tBox1 = src1.sourceRectAtTime();
tBox2 = src2.sourceRectAtTime();
// max width
tWidth = Math.max(tBox1.width, tBox2.width).toFixed(2);
//  layer spacing 
lyrSpace = effect("Leading Middle")("Slider");
// total height
srcHeight = Math.ceil(tBox1.height + tBox2.height);
tHeight = srcHeight + lyrSpace;
// box size
txtBase = [tWidth, tHeight];
// padding control
cornerPad = content("Rectangle 1").content("Rectangle Path 1").roundness;
hPad = tWidth * effect("H Padding")("Slider") * .01;
vPad = tHeight * effect("V Padding")("Slider") * .01;

box = [(hPad + cornerPad), vPad] + [txtBase[0], txtBase[1]];
[box[0], box[1]]


//Shape Layer/Contents/Rectangle 1/Position

//source layers 
src1 = thisComp.layer(index - 2);
src2 = thisComp.layer(index - 1);
// text size
tBox1 = src1.sourceRectAtTime();
tBox2 = src2.sourceRectAtTime();
// max width
tWidth = Math.max(tBox1.width, tBox2.width).toFixed(2);
lyrSpace = src2.position[1] + src1.position[1] - thisComp.height;
boxHeight = tBox1.height + tBox2.height;
refScale = thisComp.layer(index - 2).scale * .01;
x = tWidth / 2;
y = boxHeight / 2;
lineSp = effect("Leading Middle")("Slider")/2;
t = tBox1.top + lineSp;
// max width
maxWidth = Math.max(tBox1.width, tBox2.width).toFixed(2);
// set position
if (tBox1.width.toFixed(2) == maxWidth)
	l = tBox1.left;
if (tBox2.width.toFixed(2) == maxWidth)
	l = tBox2.left;
newBox = [(x + l) * refScale[0], (y + t) * refScale[1]];

[newBox[0], newBox[1]]


//Shape Layer/Transform/Position

thisComp.layer(index - 2).transform.position

 

 

You will need to add expression control Sliders to the Shape Layer and name them H Padding, V Padding, and Leading Middle. If you don't want to add sliders, you will have to type in values for the padding and leading where the sliders are shown.

 

That should get you started. I just pulled these expressions from one of the Animation Presets that I have that also include additional movement based on the layer in and out points so no keyframes have to be set to make a multi-line graphic animate into the frame and then move out.

 

I am working on a tutorial series explaining how sourceRectAtTime works in detail and simplifying the math so you can create your own automated animations using text and shape layers. 

 

One more note, the Paragraph Justification for both text layers must match. With a few more calculations using left and top values, you can mix and match paragraph justification. These expressions compensate for the baseline shift in the text layers. 

 

Just incase I made some typos in the expressions I have uploaded an AE 23 project file. Let me know if you need an earlier version.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 13, 2023 Jul 13, 2023

Thanks Rick! Your are a lifesaver.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 12, 2023 Jul 12, 2023

With Pins & Boxes, create pins for both text layers and then a box around all those pins:

 

Here is the second part of the tutorial, which shows how to animate the elements:

 

Learn more about Pins & Boxes here:

https://aescripts.com/pins-and-boxes/

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 13, 2023 Jul 13, 2023
LATEST

Another good option. Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines