Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Dynamically centring 2 text boxes that are right aligned

  • Dynamically centring 2 text boxes that are right aligned

    Posted by Dean Collison on December 18, 2024 at 3:20 am

    If anyone can help with the below I’d be very grateful 🙂

    I have two text layers in after effects that I want to be responsive to one another but always sit perfectly in the centre of the composition as a right aligned block, dynamically recentring depending on the width of either text box’s text. I have managed to do this with two left aligned text boxes by adding the following expressions to the anchor point of each one respectively but when I switch to right aligned text boxes in the paragraph settings it no longer properly centres the x position and I can see the anchor points off to the right. I’ve tried for hours to get it working with no luck:

    The top text box anchor point expression:

    s = thisLayer.sourceRectAtTime(thisLayer.sourceTime(1));

    r = thisComp.layer(“Size 4 Para 2”).sourceRectAtTime(thisLayer.sourceTime(1))

    h = s.height + r.height + 50;
    w = Math.max(s.width, r.width);
    t = s.top;
    l = s.left;

    TopLeft = [l,t];

    TopRight = [l+w,t];
    TopCentre = [l+w/2,t];
    Left = [l,t+h/2];

    Centre = [l+w/2,t+h/2];

    Right = [l+w,t+h/2];

    BottomLeft = [l,t+h];

    BottomCentre = [l+w/2,t+h];

    BottomRight = [l+w,t+h];

    Centre

    The second text box (that sits underneath the one above) anchor point expression:

    s = thisLayer.sourceRectAtTime(thisLayer.sourceTime(1));

    r = thisComp.layer(“Size 4”).sourceRectAtTime(thisLayer.sourceTime(1))

    h = r.height + s.height;

    w = Math.max(s.width, r.width);

    t = r.top;

    l = r.left;

    TopLeft = [l,t];

    TopRight = [l+w,t];

    TopCentre = [l+w/2,t];

    Left = [l,t+h/2];

    Centre = [l+w/2,t+h/2];

    Right = [l+w,t+h/2];

    BottomLeft = [l,t+h];

    BottomCentre = [l+w/2,t+h];

    BottomRight = [l+w,t+h];

    Centre-[0,t+r.height+50]

    Can you please help me adjust the expressions to make them work with two right aligned text boxes? Or perhaps a method using a null might be better? Thanks in advance

    Yoan Boisjoli replied 3 weeks, 1 day ago 2 Members · 3 Replies
  • 3 Replies
  • Yoan Boisjoli

    December 18, 2024 at 2:08 pm

    Hey Dean, I think this might help you:

    For both text layers add this expression for anchor point:

    var rect = thisLayer.sourceRectAtTime();
    [rect.left + rect.width / 2, rect.top + rect.height / 2];

    For the top layer add this expression to its position:

    var bottomText = thisComp.layer("Bottom Text"); // Replace with the actual name of the bottom text layer
    var topRect = thisLayer.sourceRectAtTime();
    var bottomRect = bottomText.sourceRectAtTime();
    var maxWidth = Math.max(topRect.width, bottomRect.width); // Get the larger width
    var x = thisComp.width / 2 + maxWidth / 2 - topRect.width / 2; // Align the right edge
    var y = thisComp.height / 2 - topRect.height / 2 - 25; // Position vertically
    [x, y];

    For the bottom layer, add this expression to its position:

    var topText = thisComp.layer("Top Text"); // Replace with the actual name of the top text layer
    var topRect = topText.sourceRectAtTime();
    var bottomRect = thisLayer.sourceRectAtTime();
    var maxWidth = Math.max(topRect.width, bottomRect.width); // Get the larger width
    var x = thisComp.width / 2 + maxWidth / 2 - bottomRect.width / 2; // Align the right edge
    var y = topText.position[1] + (topRect.height / 2) + (bottomRect.height / 2) + 50; // Position below the top text
    [x, y];

    This setup dynamically aligns the two text layers as a right-aligned block, ensuring the entire block is centered horizontally based on the larger width of the layers while maintaining proper spacing between them.

    I’m including me after effects file also if it can help you !

  • Dean Collison

    January 2, 2025 at 12:14 pm

    Hi Yoan. Thank you so much for your help on this. Much appreciated! Happy New Year

  • Yoan Boisjoli

    January 2, 2025 at 2:52 pm

    Pleasure was mine Dean. Happy New Year also!

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