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("_").replace("_", "\n");
b = thisLayer.name.split("_").replace(" ", "\n");
c = thisLayer.name.split("_").replace(" ", "\n");
a+" "+b+" "+c
Expression 2 (for the layers that only have one line)
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.
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
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:
var txt = thisComp.layer("The_Quick_Brown_Fox_Jumps_Over_The_Lazy_Dog").name; var finalTxt = txt.split('_').join('\n'); finalTxt;
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
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!
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.