• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

After Effects Expression to track x pos of longest text line

Community Beginner ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

This is a tricky one and I am stumped.


I am working on a complex rig, and I need a null object to track the position of the last character of a text object as the text moves off screen, but I need to do this on hundreds of text lines. It's actually a flush-right text, so the longest character would be the first letter of the first word in this instance.


Imagine a flush right text line reading:

 

FUTURA EXTRA BOLD.  I need to track is 'F' in this case. 

PALATINO SANS. I need to track the 'P'

Any thoughts on how this could be achieved?

#Expressions #Help

TOPICS
Expressions , How to , Scripting

Views

255

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 01, 2024 Apr 01, 2024

Copy link to clipboard

Copied

Oh, I see now that you aren't trying to track multiple lines in a single text layer. Please disregard the project I posted, which is meant for a much more complicated task.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 02, 2024 Apr 02, 2024

Copy link to clipboard

Copied

Thanks for this reply. I will check out this project. I am not using text animations inside the text layer to answer your question. The text is being used like any other object and easing into position on the X axis.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 02, 2024 Apr 02, 2024

Copy link to clipboard

Copied

I could not open A cobb`s project ( apple ) to see if its what you needed, so Ill post my copy just in case.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 02, 2024 Apr 02, 2024

Copy link to clipboard

Copied

Adam:

That seems to have worked. Can you roughly explain how you got there so I can replicate this?

Thanks

Jim

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 02, 2024 Apr 02, 2024

Copy link to clipboard

Copied

sure the code is in the layer and its also parented to the text layer, so just copy the expression to your layer in your project on the x. the dimensions are seperated by the way, and parent your layer to the text layer

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 02, 2024 Apr 02, 2024

Copy link to clipboard

Copied

I've attached a simplified example of what I am trying to achieve.  In Comp 2 there are 3 lines of text, flush right, moving in X.
In front of each line is an object that is just parented to the line and offset to be in front of the first letter in each line.

What I am trying to achieve is that a null object is always tracked to the first character of the word (ie the character furthest from the anchor point).
This is an issue I am trying to solve for broadcast where we need to make 100s of variations on the text and we always want the object in front of the first letter without the need to position the object manually for each line of text.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 02, 2024 Apr 02, 2024

Copy link to clipboard

Copied

what i just posted should work for you

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 02, 2024 Apr 02, 2024

Copy link to clipboard

Copied

This works almost perfectly, except that the null always tracks the bottom line, not necessarily the longest line. How would you change it to correct that?

This expression is more sophisticated than what I am used to. If I understand correctly it is returning the length from here which is reading the last line as [lines.length-1]: 
(time < inPoint) ? lines[lines.length-1] : theText

Is that right?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2024 Apr 02, 2024

Copy link to clipboard

Copied

In your example, if Red Solid 1- Layer 2 is below the text layer and has the anchor point in the default middle of the layer, and the text layer is Right Justified with the baseline shift of zero, this expression will position the square exactly at the top left corner of the text layer.

 

ref = thisComp.layer(index-1);
box = ref.sourceRectAtTime();
pos = ref.position;
xOfst = width/2 - box.left;
yOfst = box.height;
[pos[0] - xOfst, pos[1] - yOfst]

 

 If you want to offset the Red Solid 1 layer by 10 pixels to the right and move it up 5 pixels, modify the last line of the expression to add the padding like this:

 

ref = thisComp.layer(index-1);
box = ref.sourceRectAtTime();
pos = ref.position;
xOfst = width/2 - box.left;
yOfst = box.height;
ofst = [-5, -5];
[pos[0] - xOfst, pos[1] - yOfst] + ofst;

 

If you have multiple lines of text and you want the Red Solid 1 layer to stay lined up with the left edge of multiple lines of text, you'll need to add a Math Max function to look at the left edge of multiple layers like this: (the example is for two lines of text) ;

 

ref = thisComp.layer(index-1);
box = ref.sourceRectAtTime();
ref2 = thisComp.layer(index-2);
box2 = ref2.sourceRectAtTime();

xOfst1 = width/2 - box.left;
xofst2  = width/2 - box2.left;
xOfst = Math.max(xOfst1, xofst2);

pos1 = ref.position;
pos2 = ref2.position;
posX= Math.max(pos1[0], pos2[0])
posY = ref.position[1];
yOfst = box.height;
ofst = [-5, -5];
[posX- xOfst, posY - yOfst] + ofst;

 

You have separated dimensions in your sample comp. I almost never do that because you can't edit the motion paths. If you want different motion paths or the position of the red box to attach itself to the longest line of text, you can throw those calculations into the expression.

 

These expressions will lock the Red Solid 1 layer to the position of the top text layer, and the bottom text layer must have the same X position values. The expression offered by Adam does not lock the layer to the text layer's position.

 

I hope this helps. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 03, 2024 Apr 03, 2024

Copy link to clipboard

Copied

LATEST

Jim, let me know if Rick's solution gives you what you need, otherwise I think I have other projects that have rigs in it that will give you what you need. I obviously misunderstood your initial request.

Will your projects have a fixed number of text layers in the comp ? if so, are they only to be edited by the user, or do they have to decide how many text layers they need ? would they ever need to create a new text layer ? would the text layer ever need to be moved vertically ? 

I have a situation where I have multiple text layers, and so I have an object for each layer and I use an if else statement to turn on the layer I need to see, so in your case, that would be the object following the longest text, then the other layers following the other texts would turn off

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines