Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Auto Center 2 Text layers that change lengths depending on language

  • Auto Center 2 Text layers that change lengths depending on language

    Posted by Miles Wert on January 3, 2025 at 10:45 pm

    I have a CSV file linked to text layers that change language based on a slider controller.

    Depending on the language, certain words may be longer or shorter. After switching between languages, I’d love an expression to find the center of 2 different text layers, center them in the comp (on the X axis)…all while keeping their same distance from each other.

    For Example:

    231x More Sales is changed to 231x mehr Verkäufe. Now that the German translation is longer, the two separate text layers (231x & mehr Verkäufe) are longer in length and no longer centered on the X axis in the comp.

    I’m not quite sure how to approach this. Do I tell a null’s anchor point to find the center of those two text layers and then move to the center point of the comp on the x axis?

    > a = thisComp.layer(“My Layer 1”).position;
    b = thisComp.layer(“My Layer 2”).position;

    (a + b) / 2

    I know that gets me the center between two objects but does that also work for text layers? And how could I then snap that value to the center of the comp?

    My thought process:

    I have: Text Layer 1, Text Layer 2, Null 1 and Null 2.

    1) Link Text Layer 1 and Text Layer 2 to Null 1.

    2) Null 2 sits in comp center on x axis.

    3) Null 1 calculates the center point between Text Layer 1 and Text Layer 2 and automatically snaps to position of Null 2.

    Does that even make sense? 🤣

    Thanks in advance for any help.

    Brie Clayton replied 2 weeks, 3 days ago 3 Members · 5 Replies
  • 5 Replies
  • Dan Ebberts

    January 4, 2025 at 9:39 pm

    Assuming that your text layers (renamed to “Text 1” and “Text 2” in this example) are not parented to anything and have not been scaled, you should be use position expressions like these to center the combined length (plus a gap between them).

    Expression for Text 1:

    gap = 20;
    w1 = thisComp.layer("Text 1").sourceRectAtTime(time,false).width;
    w2 = thisComp.layer("Text 2").sourceRectAtTime(time,false).width;
    centerX = thisComp.width/2;
    targetX = centerX - (w1 + gap + w2)/2;
    curX = value[0] + sourceRectAtTime(time,false).left;
    offsetX = curX - targetX;
    value - [offsetX,0]

    Expression for Text 2:

    gap = 20;
    w1 = thisComp.layer("Text 1").sourceRectAtTime(time,false).width;
    w2 = thisComp.layer("Text 2").sourceRectAtTime(time,false).width;
    centerX = thisComp.width/2;
    targetX = centerX + (w1 + gap + w2)/2 - w2;
    curX = value[0] + sourceRectAtTime(time,false).left;
    offsetX = curX - targetX;
    value - [offsetX,0]
  • Miles Wert

    January 6, 2025 at 6:39 pm

    Hey Dan! Thanks so much for the reply!

    I’ve plugged in the expression but seem to be getting an error for the last line.

    Thinking (Offset x) needed to be a value, I added a numerical character there but still got the error.

    I do have another expression before that one so I thought that may be causing the error but even in a new comp with only two text layers (Text 1 & Text 2), the error persists.

    I’ve broken down the expressions that will precede the auto centering expression. Would these interfere at all?

    Text 1 is a counter followed by x (for example: 231x)

    Text 2 is the layer that is being translated between different languages. These translations are in a spreadsheet and I can cycle through them with the slider control.

    Text 1:

    Math.floor(effect("Slider Control")("Slider"))	+	"x"

    Text 2:

    const slider = effect("Slider Control")("Slider")
    footage("Untitled spreadsheet - Sheet1.csv").dataValue([2, Math.round(slider)]);
  • Dan Ebberts

    January 6, 2025 at 8:09 pm

    The expressions I posted are for the Position property. It looks like your existing expressions are for Source Text, is that correct?

  • Miles Wert

    January 6, 2025 at 8:21 pm

    🤣

    You are correct. No wonder it wasn’t working. Working perfectly now!

    Cheers to the LEGEND, DAN EBBERTS!!!!!🙌

  • Brie Clayton

    January 7, 2025 at 2:46 pm

    Thanks for solving this, Dan!

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