ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Changing Shape Layers size (width) due to text width

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Bart Remmers
Changing Shape Layers size (width) due to text width
on May 13, 2016 at 1:37:15 pm

Hi guys,

I've been messing around with this but I still haven't got to the results that I wish it to be.

I've got a text layer (in a bounding box). But the content (the source text) may change later. It's been rigged with an thisComp.layer("TEXT MASTER").text.sourceText, so I can easily change this later.

Behind the text-layer there is a shape (rectangle) and I want it's width to get larger + some extra space around it, because that looks nice. Let me show you:



So to sum it up: I want the width size of the rectangle behind the text to automaticly scale itself depending on the width of the text layer.
And after that I still want to animate it (with the anchorpoint in the middle, or at the far most right) to have it scale up from 0 to 100%).

Does anyone have a clue how I can get this done? I'm quite new at expressions, I roughly know the basics, this is why I'm seeking for help.

Bart.


Return to posts index


Kalle Kannisto
Re: Changing Shape Layers size (width) due to text width
on May 13, 2016 at 4:12:36 pm
Last Edited By Kalle Kannisto on May 13, 2016 at 4:13:33 pm

For rectangle size:

margin = 25;
text_width = thisComp.layer("my_text").sourceRectAtTime().width;
text_height = thisComp.layer("my_text").sourceRectAtTime().height;
box_width = text_width + margin*2;
box_height = text_height + margin*2;
[box_width, box_height]


for rectangle position:

text_height = thisComp.layer("my_text").sourceRectAtTime().height;
[0, text_height/2]


It works with multiline as well.


Return to posts index

Bart Remmers
Re: Changing Shape Layers size (width) due to text width
on May 16, 2016 at 9:39:08 pm

Well, it might be a beginners mistake, but I can't seem to figure this out...
I've done exactly as you said but I think I still did this thing wrong.
It gives me errors when adding the expressions to the scale/position of the rectangle.
One of these errors are displayed below. I think I named my layers right. I did name my text layer for this example 'my_text'.



I'm sorry for messing up, haha, still a rookie.


Return to posts index


Bart Remmers
Re: Changing Shape Layers size (width) due to text width
on May 16, 2016 at 9:40:51 pm

Sorry, I attached the wrong picture. Here's the right one. I can't seem to edit my last post.



Return to posts index

Dan Ebberts
Re: Changing Shape Layers size (width) due to text width
on May 16, 2016 at 11:35:17 pm

I notice you're using CC 2014. Is it the 13.2 update? I think that's when sourceRectAtTime() was added.

Dan



Return to posts index

Bart Remmers
Re: Changing Shape Layers size (width) due to text width
on May 17, 2016 at 7:44:33 am

Thank you for replying so fast!
Yeah, that's it! I've been working on an older version (without knowing). I just updated to 13.2 instead of 13.0.2 and now it does work.
Only too bad still something goes wrong. The rectangle (which has an anchor point in the mid-centre of it) now positions itself with the anchor in the far upper right corner of the composition, and is far to wide. But the size DOES change the way I want it too, so I'm on the way to achieving what I'm looking for. Maybe I'm still doing something wrong. I've been trying other values aswel in the expression, but I still don't get what I'm trying for.



Thanks again!

Bart


Return to posts index


Dan Ebberts
Re: Changing Shape Layers size (width) due to text width
on May 17, 2016 at 1:39:42 pm

Sizing and positioning a shape layer is tricky because, as with a text layer, you have to use sourceRectAtTime() to get its extents. Try this for the shape layer's position:

t = thisComp.layer("my_text");
tRect = t.sourceRectAtTime(time,false);
tUL = t.toComp([tRect.left,tRect.top]);
tLR = t.toComp([tRect.left+tRect.width,tRect.top+tRect.height]);
tCenter = (tUL + tLR)/2
myRect = sourceRectAtTime(time,false);
myUL = toComp([myRect.left,myRect.top]);
myLR = toComp([myRect.left+myRect.width,myRect.top+myRect.height]);
myCenter = (myUL + myLR)/2
delta = myCenter - tCenter;
value - delta

Try this for the shape's scale (not the layer's scale, the one in the Transform: Rectangle 1 section):

t = thisComp.layer("my_text");
tRect = t.sourceRectAtTime(time,false);
tUL = t.toComp([tRect.left,tRect.top]);
tLR = t.toComp([tRect.left+tRect.width,tRect.top+tRect.height]);
myRect = sourceRectAtTime(time,false);
myUL = toComp([myRect.left,myRect.top]);
myLR = toComp([myRect.left+myRect.width,myRect.top+myRect.height]);
margin = 10;
[(tLR[0]-tUL[0] + 2*margin)/(myLR[0]-myUL[0]),(tLR[1]-tUL[1]+ 2*margin)/(myLR[1]-myUL[1])]*100


Dan



Return to posts index

Bart Remmers
Re: Changing Shape Layers size (width) due to text width
on May 18, 2016 at 12:06:00 pm

Dan, thank you very much for helping me out with this.

That code really looks like a massive math project. Phew! How do you do it?! Haha!

I've been struggling with this for a few days now. I'm trying to make my projects somewhat easier to edit for later, like working with a Mastercomp where the texts can be changed later on and some colors that are used in the animation, you never know. This way I can make my animations more variable.

Thanks again! I really appreciate this!

Bart


Return to posts index

Matthew Mammola
Re: Changing Shape Layers size (width) due to text width
on May 25, 2016 at 2:28:32 pm

This is awesome! Thanks y'all! At first, when using Dan's latest script there, I was having an issue with the box not aligning properly to the center of the text, and then I set my Rectangle anchor point to 0,0 and all was perfect with the world.

Of course, my text and box are centered absolute in the comp, so that may not be helpful to other purposes.

Thanks for the awesome scripting!


Return to posts index


Tim Drage
Re: Changing Shape Layers size (width) due to text width
on May 27, 2016 at 9:14:27 am

Incredible thanks :)

Could be better if the second expression, instead of changing the shape's scale, controlled the rectangle path size, that way any round corners wouldn't be distorted. I'll try and figure it out myself but the last line of that expression is a bit much for me this morning :D


Return to posts index

Tim Drage
Re: Changing Shape Layers size (width) due to text width
on May 27, 2016 at 9:25:03 am

... OK i figured it out myself; this in Rectangle 1>Rectangle Path 1>Size will make the box resizing happen before the round corners and other effects. :)

t = thisComp.layer("tessssstr");
tRect = t.sourceRectAtTime(time,false);
tUL = t.toComp([tRect.left,tRect.top]);
tLR = t.toComp([tRect.left+tRect.width,tRect.top+tRect.height]);
myRect = sourceRectAtTime(time,false);
myUL = toComp([myRect.left,myRect.top]);
myLR = toComp([myRect.left+myRect.width,myRect.top+myRect.height]);
margin = 10;
[(tLR[0]-tUL[0] + 2*margin),(tLR[1]-tUL[1]+ 2*margin)]

Thanks again Dan for another great solution :)


Return to posts index

Kalle Kannisto
Re: Changing Shape Layers size (width) due to text width
on May 26, 2016 at 5:19:24 pm

My method expects the shape box to be drawn and placed in a similar fashion to how you want it to look, correctly placed and sized to the text.

Starting setup project:

10129_selfadjustingtextbox.aep.zip


Return to posts index


Nuno Trindade
Re: Changing Shape Layers size (width) due to text width
on Dec 10, 2016 at 11:36:31 am

Many thanks

Can you help me in a simple thing?

If i wont to adjust the text, not in the center, but aligned to the left?

I already download your example... but if i change the alignment of my_Text... the box don't move...

Thanks


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]