ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Expression to dynamically move layers to maintain similar distance if content changes.

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Cameron Warn
Expression to dynamically move layers to maintain similar distance if content changes.
on Jun 20, 2019 at 2:36:20 am

Hi All.

I'm trying to find an expression that creates a set-space between layers (text/image/object) that dynamically moves to maintain same distance if text or objects are changed.

For example:

Say I have an image I want to offset a specific amount (say 50) to the right of a text line, then I have another text line I want to off-set the same amount to the right of the image.

I can't use anchor positions as reference as they will change depending on the content, as will any formatting (if only text layers were involved), so believe I need an expression based on finding the edges of the bounding boxes.

I'd also like all the layers to stay centred to the overall comp. I think I can do with an expression I've found that I've attached to a null then parented objects I wish to centre to, but any other examples would be great too.

I've no expressions knowledge (not an AE person) and haven't found any scripts online that help me so far, so any help would be appreciated as I have no idea how to go about scripting this.

Cheers


Return to posts index

Kalleheikki Kannisto
Re: Expression to dynamically move layers to maintain similar distance if content changes.
on Jun 20, 2019 at 7:29:11 am

A good morning challenge.

Text layer 1 position:
margin = 50;
myTextWidth = thisLayer.sourceRectAtTime().width;
imageWidth = thisComp.layer("Image").sourceRectAtTime().width;
otherTextWidth = thisComp.layer("Text layer 2").sourceRectAtTime().width;
combinedWidth = myTextWidth+imageWidth+otherTextWidth+2*margin;
compCenterX = thisComp.width/2;
yPos = transform.position[1];
xPos = compCenterX+myTextWidth/2-combinedWidth/2;
[xPos, yPos]


Image position:
margin = 50;
text1PositionX = thisComp.layer("Text layer 1").transform.position[0];
text1Width = thisComp.layer("Text layer 1").sourceRectAtTime().width;
myWidth = thisLayer.sourceRectAtTime().width;
myXPosition = text1PositionX + text1Width/2 + margin + myWidth/2;
myYPosition = transform.position[1];
[myXPosition, myYPosition]


Text layer 2 position:
margin = 50;
text1PositionX = thisComp.layer("Text layer 1").transform.position[0];
text1Width = thisComp.layer("Text layer 1").sourceRectAtTime().width;
myWidth = thisLayer.sourceRectAtTime().width;
myXPosition = text1PositionX + text1Width/2 + margin + myWidth/2;
myYPosition = transform.position[1];
[myXPosition, myYPosition]


The text layers need to be center aligned and the anchor point of the image needs to be centered on the layer. Usually it is, but in some cases it isn't, and if not should be fixed with Layer>Transform>Center anchor point in layer content.

Kalleheikki Kannisto
Senior Graphic Designer


Return to posts index

Cameron Warn
Re: Expression to dynamically move layers to maintain similar distance if content changes.
on Jun 21, 2019 at 6:53:24 am

Lovely!

That seems to work quite well for keeping selected space and overall centering too.

If I need to keep the text lines and objects on the same horizontal level is it just a matter or changing the anchor point to the lower edge of bounding box, or is there a better way to do it?

Cheers


Return to posts index


Kalleheikki Kannisto
Re: Expression to dynamically move layers to maintain similar distance if content changes.
on Jun 21, 2019 at 5:47:32 pm

The code doesn't currently affect vertical alignment, so you can move things up and down freely.

If you want the code to keep the layers at the same height, I guess you could put the anchor point of the image at the suitable height and modify the expression so that two of the layers get their y coordinate from one of the layers, whichever you want to have as the one that controls it. I would make it the image, so when you move the image up or down, the text layers follow.

Kalleheikki Kannisto
Senior Graphic Designer


Return to posts index

Cameron Warn
Re: Expression to dynamically move layers to maintain similar distance if content changes.
on Jun 23, 2019 at 11:28:18 pm

Sounds good.

I'll have a play and see how I go. Thanks again for your help.


Return to posts index

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