Skip to main content
Known Participant
April 12, 2020
Answered

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

  • April 12, 2020
  • 2 replies
  • 1109 views

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; 

 

This topic has been closed for replies.
Correct answer Dan Ebberts

Try replacing your second line with:

 

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

 

 

Dan

2 replies

Known Participant
April 12, 2020

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

Mylenium
Legend
April 12, 2020

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

Mylenium
Legend
April 12, 2020

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 

Known Participant
April 12, 2020

Error: Illegal use of reserved word 'if'