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

Splitting up text into different layers based on line breaks

Explorer ,
Feb 01, 2024 Feb 01, 2024

Copy link to clipboard

Copied

Hey all! 
I'm building a fancy template thingy again. This time I have rounded text boxes (created using Simple Choker -> Minimax -> Tint -> Roughen Edges) and that works fine. However, the text will be up to three lines long, and this box method only looks good on a single line, since it only adapts to the outline of the whole text block, not per line.

So my idea is: 
I'll have a separate text box for the input (which is invisible in the final output). I'm taking the text content from there, split it up on each manual entered line break and send each bit of text to one of three text layers.

Text input:
This is some
Text with three
Layers.

Box 1: This is some
Box 2: Text with three
Box 3: Layers

If there are only one or two lines, the other boxes won't get any text and stay invisible.
I'm a bit rusty in terms of expressions, can anyone help me out?

I think one piece of code on the source text of each box should be sufficient, reading the text, splitting by line breaks and putting that into a string array. Box one pulls from {0}, Box 1 from {1} and Box from {2}. 

Thank you!

TOPICS
Expressions , Scripting

Views

2.0K

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 ,
Feb 01, 2024 Feb 01, 2024

Copy link to clipboard

Copied

If your text input is in a layer named "Text Input", then you could get each line with a source text expression like this:

txt = thisComp.layer("Text Input").text.sourceText;
lines = txt.split("\r");
lines[0]

where the last line determines which line of text you get.

 

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
Explorer ,
Feb 01, 2024 Feb 01, 2024

Copy link to clipboard

Copied

Sweet, thanks! That does exactly what I need. Now I only need to solve the box height for ascenders and descenders 😂

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 ,
Feb 01, 2024 Feb 01, 2024

Copy link to clipboard

Copied

This modification keeps the expression from breaking when the input text doesn't have enough lines for all the expressions (it just display a blank line):

txt = thisComp.layer("Text Input").text.sourceText;
lines = txt.split("\r");
idx = 0; // which line to get
idx > lines.length - 1 ? "" : lines[idx]

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
Explorer ,
Feb 01, 2024 Feb 01, 2024

Copy link to clipboard

Copied

Can you explain where this would be neccessary? I've tried the first expression and it works without any errors, even with just one or two lines.

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 ,
Feb 01, 2024 Feb 01, 2024

Copy link to clipboard

Copied

That's interesting. If you switch to the Legacy Extendscript expression engine, the expression will generate an error if it tries to retrieve a line that doesn't exist. Apparently the newer JavaScript engine that supresses that error. I wasn't aware of that. You learn something every day. In any case, I think it's good practice to defend against an array index running off the end of the array.

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 ,
Feb 01, 2024 Feb 01, 2024

Copy link to clipboard

Copied

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
Adobe Employee ,
Feb 11, 2024 Feb 11, 2024

Copy link to clipboard

Copied

LATEST

You have asked this about Expressions, but we have recently released in beta an ability in Scripting to set the box to auto fit based on the height of the text - so it does not go overset.

 

I am wondering if this might be something useful in your template building toolbox.

 

https://community.adobe.com/t5/after-effects-beta-discussions/new-text-box-options-available-in-scri...

 

Rather than trying to split up the lines in this way, keeping them in the same box and adapting a shape to the size of the box?

 

At the moment the box only changes in height, and from the bottom, which might be an issue for you.

 

Though the API is not supported in Expressions, and it is not exposed in the UI, you can use Scripting to set the property on the box and it will still function even though an Expression is changing the content.

 

We think it will a powerful ability, but it is early days and we are eager to get some feedback from template builders such as yourself who might try to use. it.

 

Douglas Waterfall

After Effects Engineering

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