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

Auto resizing shape layer

New Here ,
Aug 30, 2022 Aug 30, 2022

Copy link to clipboard

Copied

Hello.

 

I wanted to ask about some expression and how to do it. So I have this shape layer, and I have two text layer. I have created a null object with a check control. And that has the control to turn the opacity of the "Month" text layer and the "00" number text layer. And I want to resize the width of the blue shape layer if I turn the check control on and off. I want it to resize it fit only the "Date" and the "Date number" text layer. Is that possible with sourceRectAtTime ?

TOPICS
Expressions , How to

Views

324

Translate

Translate

Report

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
LEGEND ,
Aug 30, 2022 Aug 30, 2022

Copy link to clipboard

Copied

Sure. You just need to sample the two layers and then add their values.

 

layA=thisComp.layer("Month").sourceRectAtTime();
layB=thisComp.layer("Date").sourceRectAtTime();

//padding
padX=20;
padY=20;

laySwitch=thisComp.layer("Null").effect("Checkbox")("Checkbox");

if (laySwitch == 1)
{layW=layA[0]+layB[0]}
else
{layW=layA[0]};

X=layW+padX;
Y=layA[1];

[X,Y]

 

Mylenium

 

 

Votes

Translate

Translate

Report

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
New Here ,
Aug 30, 2022 Aug 30, 2022

Copy link to clipboard

Copied

I'm terribly sorry. But I'm getting this error. Can you help as to what it means ? 

Votes

Translate

Translate

Report

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
LEGEND ,
Aug 30, 2022 Aug 30, 2022

Copy link to clipboard

Copied

Try layA.width and layA.height instead.

 

Mylenium

Votes

Translate

Translate

Report

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
New Here ,
Aug 30, 2022 Aug 30, 2022

Copy link to clipboard

Copied

Okay two things :-

1) It worked but still the shape layer continued to jitter in width. Like it kept wiggling ? Like scaling-wiggle. 
2) When I untick the checkbox from the adjustment layer I got new error.

😞

Votes

Translate

Translate

Report

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
New Here ,
Aug 30, 2022 Aug 30, 2022

Copy link to clipboard

Copied

I'm uploading my file, if you need it. If that makes it convenient for you to explain. 

Votes

Translate

Translate

Report

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 ,
Aug 30, 2022 Aug 30, 2022

Copy link to clipboard

Copied

You applied the expression to Scale instead of Rectangle Size. You also have a Space in the layer name for Date .  Just double-click the name and type it again without adding a space.

 

See my other reply for more details and an expression that takes into account the distance between the layers. You'll also want to add in the height and width of the other layers to make the outline fit the data block.

 

Also, please drag your screenshots to the reply field, or copy and paste them there, or use the toolbar and size them up so we can see them without downloading them.

 

Votes

Translate

Translate

Report

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 ,
Aug 30, 2022 Aug 30, 2022

Copy link to clipboard

Copied

You will also have to compensate for the distance between the Date and Month layers. This expression will do that for layer size:

 

xPad = 100; //set horizontal padding
yPad = 80; // set vertical padding
refMonth = thisComp.layer("Month");
refDate = thisComp.layer("Date");
mPos = refMonth.position;
dPos = refDate.position;
mSize = refMonth.sourceRectAtTime();
dSize = refDate.sourceRectAtTime();
tWidth = dPos[0] - mPos[0] + mSize.width/2 + dSize.width/2;
// Controls
lyrOff = thisComp.layer("Date Controls").effect("Month On/Off")("Checkbox");

if (lyrOff == 1){
	totX = tWidth + xPad;
}
else{
	totX = dSize.width + xPad;
}

[totX, mSize.height + yPad]

 

You can use sourceRectAtTime().left and top to get the actual position of the text layers and link that to the shape layer position so that the Rectangle is always centered over the text layers. Paragraph justification and Baseline shift can also be adjusted if you add in those parameters, and the Rectangle will always be centered over the text layers.

 

Let us know if you also want help centering the shape layer over the text.

Votes

Translate

Translate

Report

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 ,
Aug 31, 2022 Aug 31, 2022

Copy link to clipboard

Copied

I thought the idea of sizing a background shape layer to fit over two horizontally spaced text layers would make a good animation preset, so I spent a few minutes this afternoon creating one. Here's the basic code before I added the Expression Controls for padding and animation. 

 

Create two text layers. name one Left and the other Right, and make sure they are lined up horizontally.

 

Add a shape layer below the text layers by double-clicking the Rectangle tool. Spin down the properties until you get to Contents/Rectangle 1/Rectangle Path 1/Size and add this expression.

// two text layers stacked horizontally
// Left side
L = thisComp.layer("Left");
lBox = L.sourceRectAtTime();
lLeft = -lBox.left;
lCenter = lBox.width/2;
// Left width add 
if (lLeft < 2){
	lx = 0;
	}
if (lLeft < lCenter + 10 && lLeft > lCenter - 10){
	lx = lCenter;
}
if (lLeft > lBox.width - 20){
	lx = lBox.width;
}

// Right Side
R = thisComp.layer("Right");
rBox = R.sourceRectAtTime();
rLeft = -rBox.left;
rCenter = rBox.width/2;
// Right width add 
if (rLeft < 2){
	rx = rBox.width;
	}
if (rLeft < rCenter + 10 && rLeft > rCenter - 10){
	rx = rCenter;
}
if (rLeft > rBox.width - 20){
	rx = 0;
}
// Gap Calculations

rhPos = R.position[0];
lhPos = L.position[0];
hGap = rhPos - lhPos;

// Calculate Width 

x = hGap + rx + lx;

// Calculate height
if (lBox.height > rBox.height){
	y = lBox.height;
}
else{
	y = rBox.height
}

[x, y]

Add the following expression to the Shape Layer/Contents/Rectangle1/Rectangle Path 1/Position:

ref = thisComp.layer("Left");
refBox = ref.sourceRectAtTime();
xOfst = refBox.width + refBox.left;
box = sourceRectAtTime();
l = box.left;
w = box.width;
[l + w + xOfst - refBox.width , refBox.height/2 + refBox.top]

The last step is to reveal the Left Text layers' position and the shape layers' position and use the pickwhip to tie them together. You'll get this:

thisComp.layer("Left").transform.position

Feel free to use this expression and modify it as you like to add padding, turn on or off a layer, or compensate for the baseline shift on both text layers in the Rectangle Path 1/Position expression.

 

The last expression you will need is applied to the Right layer's Transform Position Property to keep it lined up vertically with the Left layer. It looks like this:

x = value[0];
y = thisComp.layer("Left").transform.position[1];
[x, y]

The expressions I shared let you reposition everything by moving the Left layer, the Right layer follows, and Paragraph Justification is taken into account.

RickGerard_0-1661992074714.gif

 

Votes

Translate

Translate

Report

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
New Here ,
Aug 31, 2022 Aug 31, 2022

Copy link to clipboard

Copied

Thank you very much for making such an elaborate script. I apologize for the delayed reply. Was down due to a minor cold. 
So, as far as the script went, it worked perfectly as I wanted, but a few things I wanted to say.
1) The shape layer scales to the left side if I change the "Left" text layer. But I wasn't going to change the text. I had controlled the opacity of the text layer with a check control. So I wanted it to scale down or scale to the left side based on the opacity On/Off of the text layer.  Because my requirement was to enable the Month/Year text layer whenever I want. So for example if I don't want year to show, I'll just disable it from the check control I made on the null object in my original composition. If I didn't want month to show I'll disable that. So I want the shape layer to adjust the scale based on that. Is that possible ? 
2) And the shape layer follows the left text layer. So how do I link the "Right" text layer as well to go along with it. 

Votes

Translate

Translate

Report

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 ,
Aug 31, 2022 Aug 31, 2022

Copy link to clipboard

Copied

LATEST

Anything is possible; you just have to modify the code. I gave you the basic setup that uses sourceRectAtTime() and top, left, height and width to manage the actual position of the text layer. All you have to do is link the appropriate parts together. It takes a bit of fiddling.

 

If you can't figure it out, you can try some of the tools available at AEScripts.com, or you could pay someone to write the code for you. 

 

When I want to automate any kind of process with expressions, I always start with a block diagram drawn with a pencil. From there, it is fairly easy to figure out where the switches need to go and what needs to be modified, and how the math works. From my block diagram, I could see that the left Text has a width, the right Text has a width, and the distance between anchor points was equal to the Right position (the biggest value) minus the Left Position. I could also see that if the text layers were center justified, the total distance from the left side to the right side would equal half the width of the left Text + the difference between the layers + half the width of the right Text. That part was easy. 

 

Left Text Width/2 + distance between layers + Right Text Width /2

 

Correcting for left and right paragraph justification uses sourceRextAtTime().left. I created a couple of if/else statements to add use the 0 (zero) for the left layer/Left Justified, keep half the width for the Center Justified, and use the full width for the Right justified and the opposite for the right layer. 

 

To position the rectangle, I used the Left, Width, Top, and Height parameters and a little math to lock the rectangle to the Left layer's position. If you want to lock the text box to the right layer, you just have to change the math and which layer you are looking at for the top and left data.

 

Adding a switch to turn off the Left layer is also an easy process with a simple if statement that says if (the switch = 1) everything on the left layer = zero else, keep everything.

 

I hope this helps. 

 

I will have a tutorial series published fairly soon that digs deeply into sourceRectAtTime() and shows you how to use all of the data you can gain from any layer to line things up and make things happen. If I remember, I'll post a link to the tutorial series when it is published here. 

My Quick Tips Playlist

Votes

Translate

Translate

Report

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