Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Center align multiple layers along thisComp.height/2?

  • Center align multiple layers along thisComp.height/2?

    Posted by Shane Starnes on May 17, 2024 at 6:17 pm

    I have 4 text layers stacked overtop each other, all on the same x value for simplicity. There is a central text layer that (for now) sits in the exact center of the comp, and the other layers take their position value from its yPosition + sourceRect + margin etc while I figure this expression setup out. Some text layers may end up with sourceText for some renders, and others may be left blank, but not deleted.

    My question is, if the user opts to use the main text layer + a text layer below it, how can I have the two layers dynamically center around thisComp.height/2? As in, after taking into account their different sourceRect.heights, visually, the layers are positioned in a way that gives perfect balance along the height? Or if there are 3 layers used, etc? I’m comfortable with for loops if that’s necessary, but also happy to rough it with if else statements 🤣. I just am not sure how to wrap my mind around this.

    Brie Clayton replied 2 years, 2 months ago 3 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    May 17, 2024 at 9:40 pm

    It’s tricky, and a little complicated, but this example might help. It assumes that 4 text layers are participating and that they are layers 1 through 4 in your layer stack. They’ll be positioned in y in the order of their layer index (layer 1 on top), with the center of the visible layers positioned and mid y point in the comp. It should adjust for any layers that have no text.

    margin = 20;
    above = 0;
    below = 0;
    r = sourceRectAtTime(time);
    myH = r.height;
    for (i = 1; i <= 4; i++){
    if (i == index) continue; // skip own layer
    h = thisComp.layer(i).sourceRectAtTime(time).height;
    if (i < index){
    if (h > 0) above += h + margin;
    }else{
    if (h > 0) below += margin + h;
    }
    }
    tot = above + myH + below;
    mid = tot/2;
    myMid = above + myH/2;
    deltaY = mid - myMid;
    currentY = value[1] + r.top +r.height/2;
    targetY = thisComp.height/2 - deltaY;
    value + [0,targetY - currentY]
  • Shane Starnes

    May 20, 2024 at 9:17 pm

    Worked perfectly! Thank you Dan! You’re right, this is a tricky one. Wrapping my mind around it is harder than the expression itself. Thanks again.

  • Brie Clayton

    May 21, 2024 at 12:51 am

    Thanks for this solve, Dan!

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