Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Parenting text layers to other text layers

  • Parenting text layers to other text layers

    Posted by Bart Remmers on May 25, 2016 at 10:16 am

    Hi there,

    I’ve been messing around with this but I can’t seem to get this right. Let me first share a screenshot of what I want to achieve:

    You can see 4 texts. Let’s discuss the upper two (Aligned to the right). I want these two texts to be parented to each other. All that I want is the left text layer (“Parent me to“), which is parented to the right one (“This text“to move to the right when the second part of the sentance gets longer (so when it has more width/characters).

    This way I hope to create flexible texts like shown in the second example. As you can see, I want the other text layers to adjust their position depending on the width of the variable text layer (the names “Pete/Joanna“).

    I was thinking working with Nulls that move along with the edges of the text layer, but they don’t really seem to do that (so I could parent the second text layer that needs to move when the variable textlayer has been changed).

    I tried to google this and took a good look around on Creative Cow, but I couldn’t find anything on this specific thing. I hope someone can help me further along with this and I hope that my question is clear enough, if not I can always try to explain again 😉

    Thank you guys in advance!

    Bart.

    Dan Ebberts
    replied 2 weeks, 3 days ago
    4 Members · 13 Replies
  • 13 Replies
  • Dan Ebberts

    May 25, 2016 at 8:13 pm

    Here are a couple of examples that should help. This one will right justify to another text layer on the right:

    gap = 20;
    L = thisComp.layer(“other layer”);
    r = L.sourceRectAtTime(time,false);
    hisLeft = L.position[0] + (r.left – L.anchorPoint[0])*L.scale[0]/100;
    r = sourceRectAtTime(time,false);
    myRight = position[0] + (r.left + r.width – anchorPoint[0])*scale[0]/100;
    delta = hisLeft – myRight;
    value + delta – gap

    This one will left justify to another text layer on the left:

    gap = 20;
    L = thisComp.layer(“other layer”);
    r = L.sourceRectAtTime(time,false);
    hisRight = L.position[0] + (r.left + r.width – L.anchorPoint[0])*L.scale[0]/100;
    r = sourceRectAtTime(time,false);
    myLeft = position[0] + (r.left – anchorPoint[0])*scale[0]/100;
    delta = hisRight – myLeft;
    value + delta + gap

    In both, you need to set the gap variable to something that makes sense for your font size.

    Dan

    EDIT: Oops, I posted the same expression twice. This should be better.

  • Kalleheikki Kannisto

    May 26, 2016 at 5:45 pm

    Another option is to use a placeholder character such as an asterisk and replace it.

    For instance, set the text to “Hello *! How are you?” and use this expression to get the name from a text layer “name”:

    text_array = text.sourceText.split("*");
    text_array[0]+thisComp.layer("name").text.sourceText+text_array[1]

    Although if you actually want different colors for the different pieces of text, that’s not going to work.

  • Bart Remmers

    May 27, 2016 at 2:39 pm

    Hi Dan,

    Thank you for your expression again! It works like a charm in both examples!! Exactly what I needed!
    I’m very happy that you helped me out with this! A million thanks! This opens up some possibilities that I’ve never dreamed off!

    Bart.

  • Bart Remmers

    May 27, 2016 at 2:43 pm

    Hi Kalle,

    Thank you for your expression. Not in all instances is it required for my texts to have different colors, but I’d like to have the possibility to change it in whatever I’d like it to be. It’s good that you pointed this out. I’ve tried your and Dan’s expression and I went with Dan’s because this one fits more to my needs. But I’d still want to thank you offcourse for your effort and helping me out. So thank you for your input. Much appreciated!!

    I’m sure I’ll be back sometime soon when I can’t achieve what I’m trying.

    Bart.

  • Bart Remmers

    June 2, 2016 at 8:23 am

    Quick question; would there be a simple way to have the three layers (the second example that I’ve shown in the first post in this topic) centered in the middle of my composition? Right now if the left one contains 3 characters and right one would contain 10 characters and I centered the middle one, it wouldn’t be centered anymore.

    Bart.

  • Bart Remmers

    June 2, 2016 at 12:36 pm

    Hi Kalle,

    I’ve been messing around with your script. It’s really usefull too! Thanks again for it.
    Is it possible to split two times in one text layer?

    Like this:

    I put this in my text layer: “*, #

    text_array = text.sourceText.split("*");
    text_array[0]+thisComp.layer("text_1").text.sourceText+text_array[1]

    text_array = text.sourceText.split("#");
    text_array[0]+thisComp.layer("text_2").text.sourceText+text_array[1]

    When I try this it only works on the last one, so this will show up like this:

    *, text_2

    Do you have any idea if it’s possible to split it twice in one layer?

    Bart.

  • Kalleheikki Kannisto

    June 2, 2016 at 12:53 pm

    You’d have to keep using the result and split it further, like so:

    text_array = text.sourceText.split("*");
    text_array2=text_array[0]+thisComp.layer("name").text.sourceText+text_array[1];
    text_array3 = text_array2.split("#");
    text_array3[0]+thisComp.layer("day").text.sourceText+text_array3[1]

    So, for example, the original layer would have the text “Hello *, have a good #!” and the comp would have two text source layers, one with the layer name “name” and another with “day”. They would both be replaced.

    Centering the text using this method is easy as you can use the centered paragraph, but it would be the same style throughout the text, so will work only for those instances.

  • Bart Remmers

    June 2, 2016 at 1:44 pm

    Kalle, you are a true hero! Thank you!

  • Sebastien Bruzzo

    November 25, 2024 at 1:36 pm

    Hello Dan,

    While trying these two variations of expressions, I encountered the following error message: “Invalid token” on the line with the variable <code inline=””>hisRight=….
    Could this be due to changes made to the code since then?

    PS : And a huge thank you for your work and your contributions to the After Effects community. It’s been so many years now that I’ve been learning thanks to you.

  • Dan Ebberts

    November 25, 2024 at 2:23 pm

    Ah yes. All the code from the old days got mangled when they did some kind of conversion on this site (don’t remember when that was exactly). All the quote characters and minus signs got messed up. This should be better:

    gap = 20;
    L = thisComp.layer("other layer");
    r = L.sourceRectAtTime(time,false);
    hisRight = L.position[0] + (r.left + r.width - L.anchorPoint[0])*L.scale[0]/100;
    r = sourceRectAtTime(time,false);
    myLeft = position[0] + (r.left - anchorPoint[0])*scale[0]/100;
    delta = hisRight - myLeft;
    value + delta + gap
Page 1 of 2

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