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

Wrong expression for splitting layertext into multiple lines

Participant ,
Jul 26, 2022 Jul 26, 2022

Copy link to clipboard

Copied

Hi, 

 

How can I combine the two expressions below into one? So whenever I write "_" in the text layers name, the text splits into a new line and when I don't enter "_" it just shows the layers name?  


Expression 1 (splits my layer in new lines)

a = thisLayer.name.split("_")[0].replace("_", "\n");
b = thisLayer.name.split("_")[1].replace(" ", "\n");
c = thisLayer.name.split("_")[2].replace(" ", "\n");

a+" "+b+" "+c

 

Expression 2 (for the layers that only have one line) 

thisLayer.name.split("_");

 

Problem

When I don't enter "_" in expression 1 it gives me an error message + it forces me to change the expression every time I want more/fewer lines than 3.

 

Thanks

TOPICS
Expressions

Views

1.5K

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

correct answers 1 Correct answer

Community Expert , Jul 27, 2022 Jul 27, 2022

This is all Javascript's fault unfortunately.  .replace only works on the first element it comes across.  Split turns a string of characters into an Array, which we can stitch back together using .join:

var txt = thisComp.layer("The_Quick_Brown_Fox_Jumps_Over_The_Lazy_Dog").name;

var finalTxt = txt.split('_').join('\n');
finalTxt;

javascript.png

Votes

Translate

Translate
LEGEND ,
Jul 26, 2022 Jul 26, 2022

Copy link to clipboard

Copied

Using both expressions doesn't make much sense to begin with since the .split() is redundant and if there are variable numbers of text you put it in a for() loop and also add an additional if()else() or try()catch() as a safeguard. In fact if all you want to to is just wrap the text to new lines you'd only need the .replace() part, anyway, as string functions automatically iterate through all occurances of a substring. So more or less all you would likely ever need is a plain

 

thisLayer.name.replace("_", "\n");

 

Mylenium

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 ,
Jul 26, 2022 Jul 26, 2022

Copy link to clipboard

Copied

Thanks for your quick reply Mylenium! You are always so on spot and clear with your responses throughout the forum - something that I both appriciated and get inspiration from.

 

I tried your expression but after the second "_" a new line is not added. Instead it shows the _ in the output. Example:

Line 1

Line 2_Line3

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 ,
Jul 27, 2022 Jul 27, 2022

Copy link to clipboard

Copied

This is all Javascript's fault unfortunately.  .replace only works on the first element it comes across.  Split turns a string of characters into an Array, which we can stitch back together using .join:

var txt = thisComp.layer("The_Quick_Brown_Fox_Jumps_Over_The_Lazy_Dog").name;

var finalTxt = txt.split('_').join('\n');
finalTxt;

javascript.png

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 ,
Jul 27, 2022 Jul 27, 2022

Copy link to clipboard

Copied

Awesome ShiveringCactus - it works! Thank you!

 

To be able to dublicate the text-layer and change the layers name directly (since I'm working with the layers inside the main-comp), I changed it to this expression which makes it possible: 
var txt = thisLayer.name;
var finalTxt = txt.split('_').join('\n'); finalTxt;

Now when this expression already frees up a ton of time for me, I wonder if it can be pushed even further:
What expression can be added, so that when changing the layer name, the anchor point automatically moves to the center of the new text-size and positions the layer in the center of the comp? Even if I change for example the alignment of the text, size or font etc. manually afterwards.

Thanks for taking the time

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 ,
Jul 27, 2022 Jul 27, 2022

Copy link to clipboard

Copied

Sorry, that was lazy of me asking that question! So I did my research and found the last expressions I was looking for.

 

Auto-center the anchor point by adding this expression to the anchor-point property (from School of Motion: https://www.schoolofmotion.com/blog/anchor-point-expressions-in-after-effects) :

 

a = thisComp.layer("Text1").sourceRectAtTime();
height = a.height;
width = a.width;
top = a.top;
left = a.left;

x = left + width/2;
y = top + height/2;
[x,y];

 

 

Auto-position the layer to the center of the comp by adding this expression to the position property (from https://stackoverflow.com/questions/33163171/after-effects-centering-layer-by-expression) :

var w=thisComp.width;
var h=thisComp.height;
var w2=w/2;
var h2=h/2;
[w2,h2]

 

Thanks again for your help Mylenium and ShiveringCactus!

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 ,
Jul 27, 2022 Jul 27, 2022

Copy link to clipboard

Copied

LATEST

Sorry I didn't reply earlier - the day job got in the way.

Well done for finding the solution and big thanks for posting it here for the next person.

 

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