Highlighted

Beginner question with Expressions-- How to set a if/else inside of a variable

Community Beginner ,
Apr 12, 2020

Copy link to clipboard

Copied

Trying to add functionality to a basic MOGRT and I'm really rough with expressions.

On top of this graphic is a line of text (master property, in this case layer XXX). 

 

This code is on a white box that will expand/contract it's width based on length of text in XXX.

Found out that this would work: 

 

L= thisComp.layer("XXX");
X= L.sourceRectAtTime().width*.7;  /*as the string of text in XXX gets longer, needed a multiplier to tighten up the padding... 0.7 works fro the range of possible text*/
Longer=X+8; /*needed to give some padding when XXX is just a single "i"*/
Y=100;
[Longer,Y];

So, this works great... except when there is NO text in the XXX layer.

It returns the 8. Which creates a thin white backgorund box. 

 

What I think would work; add another variable "P" for padding. 

 

P= if(L.sourceRectAtTime().width)>0){8}else{0}; /*this is saying 'illegal use of if'*/

and then I would change this line Longer=X+P; 

 

Try replacing your second line with:

 

P = L.sourceRectAtTime().width > 0 ? 8 : 0;

 

 

Dan

TOPICS
Expressions, How to

Views

422

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Beginner question with Expressions-- How to set a if/else inside of a variable

Community Beginner ,
Apr 12, 2020

Copy link to clipboard

Copied

Trying to add functionality to a basic MOGRT and I'm really rough with expressions.

On top of this graphic is a line of text (master property, in this case layer XXX). 

 

This code is on a white box that will expand/contract it's width based on length of text in XXX.

Found out that this would work: 

 

L= thisComp.layer("XXX");
X= L.sourceRectAtTime().width*.7;  /*as the string of text in XXX gets longer, needed a multiplier to tighten up the padding... 0.7 works fro the range of possible text*/
Longer=X+8; /*needed to give some padding when XXX is just a single "i"*/
Y=100;
[Longer,Y];

So, this works great... except when there is NO text in the XXX layer.

It returns the 8. Which creates a thin white backgorund box. 

 

What I think would work; add another variable "P" for padding. 

 

P= if(L.sourceRectAtTime().width)>0){8}else{0}; /*this is saying 'illegal use of if'*/

and then I would change this line Longer=X+P; 

 

Try replacing your second line with:

 

P = L.sourceRectAtTime().width > 0 ? 8 : 0;

 

 

Dan

TOPICS
Expressions, How to

Views

423

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 12, 2020 0
Most Valuable Participant ,
Apr 12, 2020

Copy link to clipboard

Copied

Nothing wrong with the code per se, you just threw in one too many closing parentheses. Remove the one ) after .width and everything will work.

 

Mylenium 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 12, 2020 1
Community Beginner ,
Apr 12, 2020

Copy link to clipboard

Copied

Error: Illegal use of reserved word 'if' 

Nick_Schale_0-1586708952840.png

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 12, 2020 0
Community Beginner ,
Apr 12, 2020

Copy link to clipboard

Copied

Is there something else I need to typ to start the if/else math? ...seems to get hun up on "illegal use of 'if' "

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 12, 2020 0
Most Valuable Participant ,
Apr 12, 2020

Copy link to clipboard

Copied

That's funky, but perhaps simply filling in the relevant paramters to sourceRectAtTime() might fix it. Also, now that I have a second look, you are also of course creating a cyclic condition. I mean, if the rectangle is zero, anyway, then there's no reason to double-zero your variable P. Or in other words: You are using the testing condition also for the output value, which makes no real sense. In fact that could be the actual error. A source rectangle of zero width cannot exist, anyway.

 

Mylenium

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 12, 2020 0
Guide ,
Apr 12, 2020

Copy link to clipboard

Copied

Try replacing your second line with:

 

P = L.sourceRectAtTime().width > 0 ? 8 : 0;

 

 

Dan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 12, 2020 1
Community Beginner ,
Apr 12, 2020

Copy link to clipboard

Copied

THANK YOU

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 12, 2020 0