ThemeForest

Need a help with expression

1951 posts
  • Elite Author
  • Community Superstar
  • Has been a member for 2-3 years
  • Sold between 50 000 and 100 000 dollars
  • Author had a File in an Envato Bundle
  • Contributed a Tutorial to a Tuts+ Site
  • Author had a Free File of the Month
+5 more
EFEKT_Studio says

So guys, I don’t know if it’s even possible (felt should know this one).

Maybe it’s not possible as I said, but maybe it’s simple as banana.

So, we have a text layer. Exactly: text.sourceText

I’m crushing my head here with this expression I added to some other layer:

if(thisComp.layer(“Text layer”).text.sourceText=”a”) {100} else 0

What I’m trying to do is if I enter character “a” in text layer to get result 100 for example, otherwise 0

Possible?

4401 posts
  • Elite Author
  • Community Moderator
  • Sold between 50 000 and 100 000 dollars
  • Author had a File in an Envato Bundle
  • Beta Tester
  • Has been a member for 4-5 years
  • United Kingdom
+5 more
felt_tips moderator says

You’re using a single = sign. This is used to set the value on the left to the value on the right. I’d need to check what happens here, as I’m not sure you can set the parameter in that way. In any case, the if statement will probably return true if it is able to set the sourceText to “a” and false if it isn’t. You need to use the comparitive ==

Also, you have to be careful with expressions. After Effects allows you a shorthand that allows you to write thisComp.layer(“my layer”).position; and AE will be clever enough to work out that you mean thisComp.layer(“my layer”).transform.position.value; It works this out apparently by looking at the type of data expected. This doesn’t always work with a string, as a string can contain pretty much anything (converted to a string of course). Technically speaking, thisComp.layer(“Text Layer”).text.sourceText will return [Object Property]. It’s the value of that [Object Property] that you’re after and sometimes you need to be explicit about it. I think in your case here, it’s okay… in fact I’m not sure what stops After Effects from recognising the data-type. Maybe I’m confusing with scripting here.

One other thing to watch out for is that if you are mixing up calculated values and letters in a single string, you may sometimes need to use the toString() method to make sure everything’s string compatible.

For instance
var x = 23; var y = 12.1;
var a= x*y;
var returnText = “Result\nThe number I calculated is ” + a.toString();

You’ve also missed some curly braces off the second part of your if statement

Your expression should look like this…

if(thisComp.layer("Text Layer").text.sourceText.value == "a") {100;} else {0;}

You can also write the if statement like this.

var returnText = (thisComp.layer("Text Layer").text.sourceText.value == "a") ? 100 : 0;
1951 posts
  • Elite Author
  • Community Superstar
  • Has been a member for 2-3 years
  • Sold between 50 000 and 100 000 dollars
  • Author had a File in an Envato Bundle
  • Contributed a Tutorial to a Tuts+ Site
  • Author had a Free File of the Month
+5 more
EFEKT_Studio says

YES FELT !! That’s it!! Thank you so much… I know I should use ==, but I thought it is for numerical value, and that = is for character value… Great, thnx!

4401 posts
  • Elite Author
  • Community Moderator
  • Sold between 50 000 and 100 000 dollars
  • Author had a File in an Envato Bundle
  • Beta Tester
  • Has been a member for 4-5 years
  • United Kingdom
+5 more
felt_tips moderator says

= sets a value, == compares two values.

To see what I mean about values and strings, try setting up a layer called “Other Layer” and making a Slider Control effect on it and set its value to 12.

Then make a text layer and paste this into the Source Text property:

"hello " + thisComp.layer("Other Layer").effect("Slider Control")(1).toString();

You will see “hello [Object Property]” as the result. If you try the same thing with the little addition of .value…..

"hello " + thisComp.layer("Other Layer").effect("Slider Control")(1).value.toString();

Then you will see “hello 12”;

You can also replace the (1) in the above statements with (“Slider”). In fact (“Slider”) is the AE default, but for effects that only have one property anyway, I find that using the index is a space saving option. It also has the advantage that an index can be opened in an alternative language version of AE, whereas the name will give an error.

1951 posts
  • Elite Author
  • Community Superstar
  • Has been a member for 2-3 years
  • Sold between 50 000 and 100 000 dollars
  • Author had a File in an Envato Bundle
  • Contributed a Tutorial to a Tuts+ Site
  • Author had a Free File of the Month
+5 more
EFEKT_Studio says

This is cool, I’m trying to get more into scripting :)

4401 posts
  • Elite Author
  • Community Moderator
  • Sold between 50 000 and 100 000 dollars
  • Author had a File in an Envato Bundle
  • Beta Tester
  • Has been a member for 4-5 years
  • United Kingdom
+5 more
felt_tips moderator says

This is cool, I’m trying to get more into scripting :)

The above will work in expressions and scripting.

by
by
by
by
by