ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Retrieving values from 'source text'

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Ed Nudnick
Retrieving values from 'source text'
on May 17, 2013 at 12:28:52 pm

Hi folks.

I'm trying to set up an expression to automatically change the colors of words in a text layer while leaving the punctuation symbols black.

So far, in the set up, I have 3 layers (I may simplify this to two); the layering order is arbitrary.

The bottom layer contains the main source text and, its 'video-eye' is turned off.
The middle layer is to contain the words (with the changing colors), but no punctuation.
And, the top layer contains only the punctuation (black and unchanging).

In the top two layers, I have pick-whipped the 'Source Text' to the bottom layer.

In mocking this up, I added a few Opacity Animators, set to zero, and the range selectors were set to character index numbers that would hide everything but the punctuation in the one layer.

And, conversely, I hid all punctuation the other layer.

In automating this, I'll need to retrieve a few values.

First, the maximum index number for the characters in the Source Text.
Next, I'll need to step through the Source Text string, evaluating each character to determine if it is an alpha-numeric letter or a mark of punctuation.
Once I discover a mark of punctuation, that index value will have to be stored and passed to a Range Selector.

After Effects seems to already know the maximum index value of a text string.
But, I can't find those variable names.

Tremendous thanks in advance!

-Ed


Return to posts index

Dan Ebberts
Re: Retrieving values from 'source text'
on May 18, 2013 at 12:54:37 am

Say you have a text layer with this source text: !a3$V&9

Add an opacity animator, set the opacity value to 0, add an expression selector, delete the range selector, and change the default Amount expression of the expression selector to this:

c = text.sourceText.value[textIndex-1];
if (/^w+$/i.test(c)) 0 else 100

(NOTE--there is a backslash character between the "^" and the "w" in this expression, but it probably won't print here.)

You should now see: a3 V 9

If you instead want to see: ! $ & , just reverse the 100 and the 0.

Dan



Return to posts index

Ed Nudnick
Re: Retrieving values from 'source text'
on May 18, 2013 at 12:50:09 pm

Mostly there.
Though, there's an offset of one character index if there's a carriage return or line break in the source text.
So, once I figure out how to add 1 to the [textIndex] for every carriage return, this should work fine.

It seems javascript sees the carriage return as a character that advances the textIndex.
But, After Effects does not.


Return to posts index


Ed Nudnick
Re: Retrieving values from 'source text'
on May 18, 2013 at 3:36:47 pm

Mostly there. Though, since I'm not very code-experienced, special characters evade me.

So, I set up the inelegant script below.
With the variable "show," I subtract 100 in the other layer to get the reverse effect.

(See script below)
******

Sadly, with this many IF statements, it slows performance.
Instead, I think I could speed it up by defining an array with all the alphanumeric characters and testing to see if that is my condition at that textIndex moment.

Also, I've abandoned the carriage-return snafu from earlier and opted to just use a text-box for formatting. It's a crude workaround. But, it gets it done for now.

Still curious about that.

Would likely have to set a variable to count carriage-returns, then add 1 (one) to a variable holding the [textIndex-1] value for those index-slots occurring after the carriage-return index-slots.

var show = thisComp.layer("word_slider").effect("visibility")("Slider");
c = text.sourceText.value[textIndex-1];
if (/a/i.test(c)) Math.abs(show-100)
else if (/b/i.test(c)) Math.abs(show-100)
else if (/c/i.test(c)) Math.abs(show-100)
else if (/d/i.test(c)) Math.abs(show-100)
else if (/e/i.test(c)) Math.abs(show-100)
else if (/f/i.test(c)) Math.abs(show-100)
else if (/g/i.test(c)) Math.abs(show-100)
else if (/h/i.test(c)) Math.abs(show-100)
else if (/i/i.test(c)) Math.abs(show-100)
else if (/j/i.test(c)) Math.abs(show-100)
else if (/k/i.test(c)) Math.abs(show-100)
else if (/l/i.test(c)) Math.abs(show-100)
else if (/m/i.test(c)) Math.abs(show-100)
else if (/n/i.test(c)) Math.abs(show-100)
else if (/o/i.test(c)) Math.abs(show-100)
else if (/p/i.test(c)) Math.abs(show-100)
else if (/q/i.test(c)) Math.abs(show-100)
else if (/r/i.test(c)) Math.abs(show-100)
else if (/s/i.test(c)) Math.abs(show-100)
else if (/t/i.test(c)) Math.abs(show-100)
else if (/u/i.test(c)) Math.abs(show-100)
else if (/v/i.test(c)) Math.abs(show-100)
else if (/w/i.test(c)) Math.abs(show-100)
else if (/x/i.test(c)) Math.abs(show-100)
else if (/y/i.test(c)) Math.abs(show-100)
else if (/z/i.test(c)) Math.abs(show-100)
else Math.abs(show);


Return to posts index

Dan Ebberts
Re: Retrieving values from 'source text'
on May 18, 2013 at 3:36:56 pm

Try this:

s = text.sourceText.replace(/r/g,"");
c = s[textIndex-1];
if (/^w+$/i.test(c)) 0 else 100


Note there should be a backslash between the / and the r in the first line and another between the ^ and the w in the last line.

Dan



Return to posts index

Ed Nudnick
Re: Retrieving values from 'source text'
on May 18, 2013 at 4:32:25 pm

Oh jeez, do I feel dumb.

The script below worked perfectly...
(with replacing the *'s with backslashes)

s = text.sourceText.replace(/*r/g,"");
c = s[textIndex-1];
if (/^*w/i.test(c)) 100 else 0;


Return to posts index


Rex Abba-Abba
Re: Retrieving values from 'source text'
on Oct 31, 2019 at 7:05:23 am

Hi Dan,

I am try to do something similar but I hope Iess complicated is there a way to count the number of carriage returns in a body of text?

I am trying to control a text layer's position with based on how many carriage return a within it.

Can you help please?

Thanks,
Rex


Return to posts index

Dan Ebberts
Re: Retrieving values from 'source text'
on Oct 31, 2019 at 1:13:34 pm

This should tell you how many lines there are:

text.sourceText.match(/$/mg).length

you could subtract one to get the number of carriage returns.


Dan



Return to posts index

Rex Abba-Abba
Re: Retrieving values from 'source text'
on Oct 31, 2019 at 1:37:26 pm

Thanks a lot Dan, exactly what I needed! :)


Return to posts index

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