Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Dynamically Distribute Text Layers Horizontally

  • Dynamically Distribute Text Layers Horizontally

    Posted by Christian Simpson on February 17, 2023 at 3:27 pm

    Hello All,

    I am a bit stumped. I am trying to evenly distribute the x position of 4 text layers horizontally all on the same y axis in a composition. I would like to for the group of 4 to also be centered within the comp. And for the cherry on top I would love to have a slider to control the gap distance between layers for tweaking as needed.

    Does anyone have any insights on how to accomplish something like this? I’ve attempted several solutions with sourceRecTime and have been successful so far.

    Dan Ebberts replied 2 years, 4 months ago 4 Members · 9 Replies
  • 9 Replies
  • Filip Vandueren

    February 17, 2023 at 4:17 pm

    Are they all single line texts ? Are the differences in ascenders and descenders from layer to layer the problem ?

  • Christian Simpson

    February 17, 2023 at 4:20 pm

    Single line yes, and there are descenders and ascenders.

  • Christian Simpson

    February 17, 2023 at 5:35 pm

    The ascenders and decenders aren’t even the issue yet, I haven’t even made it that far.

  • Christian Simpson

    February 17, 2023 at 8:42 pm

    I tried using this on each layer but it still doesnt space evenly.

    gap = thisComp.layer("Control").effect("Spacing")("Slider")+(thisComp.layer("A").transform.xPosition)
    L = thisComp.layer("A");
    rect = L.sourceRectAtTime();
    [gap+rect.width+rect.left];
  • Dan Ebberts

    February 18, 2023 at 7:19 am

    I think something like this might get you started. Set nLayers to the number of text layers participating (they need to be contiguous) and set firstLayer to the index of the first participating text layer.

    gap = thisComp.layer("controls").effect("Gap")("Slider");
    nLayers = 4;
    firstLayer = 1;
    curEdge = 0;
    wTotal = gap*(nLayers-1);
    for(i = firstLayer; i < firstLayer+nLayers; i++){
    L = thisComp.layer(i);
    r = L.sourceRectAtTime(time,false);
    w = r.width;
    wTotal += w;
    if (i == index) myOffset = r.left;
    if (i < index) curEdge += w + gap;
    }
    x = thisComp.width/2 - wTotal/2 + curEdge - myOffset;
    [x,value[1]]
  • Christian Simpson

    February 25, 2023 at 3:11 am

    Brilliant, thank you Dan!

  • Christian Simpson

    April 8, 2023 at 1:24 am

    Not sure if this is possible but would there be a way to make it so that, if for example, these 4 pieces of text end up being wider than a certain predetermined amount the group as a whole scales accordingly to fit back into a predetermined width?

  • Megan Au

    March 5, 2024 at 2:12 am

    This is great! How would you amend this so it everything is anchored left at a point (instead of centred to comp), and works for both text and shape layers? Thanks!

  • Dan Ebberts

    March 5, 2024 at 8:01 pm

    If I understand what you’re asking, this should work:

    xPos = thisComp.layer("controls").effect("Left X")("Slider");
    gap = thisComp.layer("controls").effect("Gap")("Slider");
    firstLayer = 1;
    curEdge = 0;
    for(i = firstLayer; i <= index; i++){
    L = thisComp.layer(i);
    r = L.sourceRectAtTime(time,false);
    w = r.width;
    if (i == index) myOffset = r.left;
    if (i < index) curEdge += w + gap;
    }
    x = xPos + curEdge - myOffset;
    [x,value[1]]

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