ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Expression error – can't find out what's wrong with my syntax

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Jarle Leirpoll
Expression error – can't find out what's wrong with my syntax
on May 10, 2019 at 5:37:29 pm

Expressions question:

I've opened an old project in AE 16.1.1, and I get some expressions errors (as expected). I've fixed most of them, but five remain, and I don't understand how I can fix them.

Here's one of the expressions, and the error message. The syntax is very similar on the other four.

This code triggers an error on line 6: index 3 out of range for stream of dimension 1

txt = thisComp.layer("Source").text.sourceText;
i = Math.floor((txt.length-1)/2);
j = (txt.length%2) ? i : i+1;
n = -1;
while (i >= 0 && j &lt; txt.length){
if (txt[i] == " "){
n = i;
break;
}else if (txt[j] == " "){
n = j;
break;
}
i--;
j++;
}
if (n &lt; 0) txt; else txt.substr(0,n)


Jarle Leirpoll
PremierePro.net


Return to posts index

Dan Ebberts
Re: Expression error – can't find out what's wrong with my syntax
on May 10, 2019 at 6:03:16 pm

Change the first line to:

txt = thisComp.layer("Source").text.sourceText.value;


Dan



Return to posts index

Jarle Leirpoll
Re: Expression error – can't find out what's wrong with my syntax
on May 10, 2019 at 7:10:48 pm

Perfect! Works like a charm. I'm not surprised, though. ☺
Thanks Dan!

Whenever you have the time, I'd like to understand why it's now important to add ".value"

Jarle Leirpoll
PremierePro.net


Return to posts index


Dan Ebberts
Re: Expression error – can't find out what's wrong with my syntax
on May 10, 2019 at 7:18:54 pm

My guess is that sourceText is an object, not the actual text array--the legacy engine lets you get away with indexing it as an array but the new engine is more strict.


Dan



Return to posts index

Jarle Leirpoll
Re: Expression error – can't find out what's wrong with my syntax
on May 10, 2019 at 7:22:01 pm

Ah, that makes sense. Thanks Dan!

Jarle Leirpoll
PremierePro.net


Return to posts index

Jarle Leirpoll
Re: Expression error – can't find out what's wrong with my syntax
on May 12, 2019 at 9:29:35 am

Almost there! I've managed to split the text between words, and to adjust the split point with a slider. Works great, but the layer that grabs the second line starts with a space, not with a letter.

How can I strip away the space at the start?

Here's the code for this layer.

txt = thisComp.layer("Source").text.sourceText.value;
nWords = Math.round(thisComp.layer("Controls").effect("Word Split")("Slider"));
if (nWords > 0){
count = 0;
for (i = 0; i &lt; txt.length; i++){
if (txt[i] == " "){
count++;
if (count == nWords) break;
}
}
if (count &lt; nWords) txt
else txt.substr(i,txt.length)
}else{
i = Math.floor(txt.length-1);
n = -1;
while (i >= 0){
if (txt[i] == " "){
n = i;
break;
}
i--;
j++;
}
if (n &lt; 0) ""
else txt.substr(n+1)
}


Jarle Leirpoll
PremierePro.net


Return to posts index


Jarle Leirpoll
Re: Expression error – can't find out what's wrong with my syntax
on May 13, 2019 at 8:47:01 am

SOLVED!

I got help from Jonathan Beresford in a Facebook group. He suggested to use trim(), and that worked.
Here's the final code. Hope it can help someone else.

txt = comp("Subtitles 25 fps 1080x1920 With Line Break").layer("Source").text.sourceText.value;
nWords = Math.round(comp("Subtitles 25 fps 1080x1920 With Line Break").layer("Controls").effect("Word Split")("Slider"));
if (nWords > 0){
count = 0;
for (i = 0; i &lt; txt.length; i++){
if (txt[i] == " "){
count++;
if (count == nWords) break;
}
}
if (count &lt; nWords) txt
else txt.substr(i,txt.length).trimLeft()
}else{
i = Math.floor(txt.length-1);
n = -1;
while (i >= 0){
if (txt[i] == " "){
n = i;
break;
}
i--;
}
if (n &lt; 0) ""
else txt.substr(n+1)
}


Jarle Leirpoll
PremierePro.net


Return to posts index

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