ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

limit a string-length by the length of another string

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Matthias Schwarz
limit a string-length by the length of another string
on Mar 13, 2019 at 1:48:12 pm

Hello,

I have two text-layers, the second should follow the first one.
I have written the following expression within the second layer´s sourcetext to limit it´s length:

w= Math.floor(thisComp.layer("first text-layer").sourceRectAtTime().width); //read out the length of the first layer
text.sourceText.substr(0,w);

No error occurs but it doesn´t work...

What´s wrong with it?

Yours, Matthias


Return to posts index

Alex Printz
Re: limit a string-length by the length of another string
on Mar 13, 2019 at 2:10:23 pm

Are you trying to subdivide by sting length? SourceRec is a pixel-width, not a character string length, so it's probably returning several hundred pixels for w.

If you're actually trying to do text character length, you could try it like this:


v = value;
w = thisComp.layer("first text-layer").text.sourceText.length;
v.slice(0,w);


Alex Printz
Mograph Designer


Return to posts index

Matthias Schwarz
Re: limit a string-length by the length of another string
on Mar 13, 2019 at 4:29:57 pm

That helps a lot, but it´s not the solution. There is another fact I didn´t mentioned yet.

I´m planning to let a centered text(the first line of text) grow until it reaches a specific pixel-amount(it´s needed to stay in the title safe area). So when the string is more than 1500 pixels in length, the input needs to be stopped.
So I try to find a way to make a condition like that:
if "thisComp.layer("First line").sourceRectAtTime().width>1500"
...then "text.sourceText.substr(0,"remain at the actual text.sourceText.length")

I hope you understand what I try to reach. I can´t see a way to bring these two parameters together.


Return to posts index


Tomas Bumbulevičius
Re: limit a string-length by the length of another string
on Mar 13, 2019 at 4:31:32 pm

Do you really want to limit/control one's string characters amount by the other? Seems like situation, where text will be cut down, possibly, without a proper meaning.

Not sure what your ultimate goal is, but scaling one text towards the other's width might be another option to consider.

Find out more:
After Effects Tutorials: motion design, expressions, scripting.


Return to posts index

Matthias Schwarz
Re: limit a string-length by the length of another string
on Mar 13, 2019 at 4:53:14 pm

The goal is a lower third with two lines(centered text), the first upper line and a second, smaller, lower line. The first lines´ length defines the maximum length of the second line.

But in the end both have a limit-the title safe area(1920 pixels horizontally minus 400 pixels approximately..)

But the length of the strings depends on what is written down, capital letters have more pixels than others letters.
In some cases, the pixel-limit is reached with 40 letters, in other cases I have 50 letters-that is the main problem.


Return to posts index

Tomas Bumbulevičius
Re: limit a string-length by the length of another string
on Mar 13, 2019 at 6:04:34 pm

Thats more clearer - but:
1. What do you plan to do if one text exceed the length of the other?
2. Is it possible (not against the brand guidelines) to scale the text down?

Let say, main title is 10chars long, all caps, while subject at the bottom - 40 chars. With original logic - its a strong 'no' through my perspective. Based on answers to questions above, we could nail down a solution :)

Find out more:
After Effects Tutorials: motion design, expressions, scripting.


Return to posts index


Alex Printz
Re: limit a string-length by the length of another string
on Mar 13, 2019 at 8:51:21 pm

It's going to be difficult to split the second by characters based on pixel length because different characters will have different widths; "I" and "W" are greatly different. There is no set way to do this outside of unicode, so unless you are going to do a look up table I don't think it will work.

One (IMO not worth it option) would be to do a for loop; find a way to emulate your subtitle's sourceRecAtTime's width (since you can't actually measure it before rendering your sourceText), then comparing that to your main title's sourceRecAtTime, then if the subtitle is longer drop a letter, check again, and keep doing that loop until it is not as long. I do not recommend this though.

If you want to stop editors from writing too long of text strings, why don't you try generating a mask that cuts off any content beyond a certain limit? Now that we have path data access, you could generate a mask that has the same width as your title's sourceRecAtTime.width. There are tutorials online for generating paths and masks this method, just need to get the anchorPoint and sourceRec involved.

Alex Printz
Mograph Designer


Return to posts index

Matthias Schwarz
Re: limit a string-length by the length of another string
on Mar 13, 2019 at 11:12:58 pm

I know that a look up table is too much work...And some hours ago I tried a loop and I didn´t succeed.

But the idea of generating a mask is worth to test- but I guess it won´t work too, because you alway get pixel lengths with a comma via sourceRectAtTime, that means it´s possible that maybe a letter is partly visible after setting the mask to a specific value(in this case 1500).


Return to posts index

Matthias Schwarz
Re: limit a string-length by the length of another string
on Mar 13, 2019 at 11:20:12 pm

To answer your questions:

1. I have discarded the idea of limiting the second line to the first, it makes no sense any longer. Now with the centered lines
it looks pretty well, even if the second line is longer than the first one.

2. Yes, I have restrictments-no scaling is allowed.

What remains is that I need to stop the user(mogrt-template for premiere cc) from breaking the rules of the title safe area.


Return to posts index


Oleg Pirogov
Re: limit a string-length by the length of another string
on Mar 14, 2019 at 12:53:45 am

If you are ok with throwing away the symbols out of safe are, why not just use a Paragraph Text of safe area size?
It will do exactly what you want, as it appears to me.


Return to posts index

Matthias Schwarz
Re: limit a string-length by the length of another string
on Mar 14, 2019 at 5:13:27 pm

OMG- that is exactly what I need! And without any coding! I´ve never heard about paragraph text, so I learned
a lot from all you!

I send all of you a very warm thank you!

Yours, Matthias


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]