Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Automatically align different shapes based on layer index

  • Automatically align different shapes based on layer index

    Posted by Matthew Reeler on April 12, 2022 at 8:36 am

    Hi!

    I’m looking for an expression to do the following. Let’s say I have 3 layers of different sizes, I would like to position them underneath each other depending on the index of the layers.

    I’m assuming the solution will use the index and SourceRectAtTime expression, but I just can’t seem to get it right.

    I’ve added a project file that shows what I want to achieve, but essentially, I would like to add a layer to the comp, then add an expression to the position property which places my new layer directly underneath the layer above it on the timeline no matter what the size of the layer is above it.

    Is this possible?

    Thanks so much in advance!

    Matthew Reeler replied 4 years, 5 months ago 2 Members · 4 Replies
  • 4 Replies
  • Dan Ebberts

    April 12, 2022 at 6:03 pm

    Try this position expression:

    L = thisComp.layer(index-1);

    r1 = L.sourceRectAtTime(time,false);

    LL = L.toComp([r1.left,r1.top+r1.height]);

    LR = L.toComp([r1.left+r1.width,r1.top+r1.height]);

    r2 = sourceRectAtTime(time,false);

    UL = toComp([r2.left,r2.top]);

    UR = toComp([r2.left+r2.width,r2.top]);

    deltaX = (LL[0] + LR[0])/2 - (UL[0] + UR[0])/2;

    deltaY = LR[1] - UR[1];

    value + [deltaX,deltaY]

  • Matthew Reeler

    April 12, 2022 at 8:40 pm

    That’s amazing! It worked perfectly. Thanks so much Dan.

    Is there any way for it work with Collapse Transformations, so let’s say instead of a shape, I have a few comps with shapes inside them that aren’t the same size as their respective comps, can this script be worked to look at those if Collapse Transformations is turned on?

    From what I’ve found online, this isn’t possible, but perhaps there is a way?

    Regardless, thanks so much again!

  • Dan Ebberts

    April 12, 2022 at 10:38 pm

    If your comp has a single shape in it, and it’s layer 1 in that comp, something like this might work on the following layer if the comp layer does or doesn’t have collapse transformations on:

    L = thisComp.layer(index-1).source.layer(1);

    r1 = L.sourceRectAtTime(time,false);

    LL = L.toComp([r1.left,r1.top+r1.height]);

    LR = L.toComp([r1.left+r1.width,r1.top+r1.height]);

    L2 = thisComp.layer(index-1);

    LL = L2.toComp(LL);

    LR = L2.toComp(LR);

    r2 = sourceRectAtTime(time,false);

    UL = toComp([r2.left,r2.top]);

    UR = toComp([r2.left+r2.width,r2.top]);

    deltaX = (LL[0] + LR[0])/2 - (UL[0] + UR[0])/2;

    deltaY = LR[1] - UR[1];

    value + [deltaX,deltaY]

  • Matthew Reeler

    April 12, 2022 at 10:48 pm

    That’s really really great, thanks so much!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy