ADOBE AFTER EFFECTS: Forum Expressions Tutorials Podcasts Creative Cloud

If/Else Statement + Source Text + Position Coordinates

COW Forums : Adobe After Effects

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Paul ConnorsIf/Else Statement + Source Text + Position Coordinates
by on Sep 28, 2016 at 3:04:06 pm

Hi Guys,

I'm trying to write an expression to set the exact X,Y coordinates of a layer based on the Source Text of another layer.

• Text layer named "SETUP"
• Solid named: "S1"

When the source text for "SETUP" says: "Target1" I'd like the position of S1 to be at 760,340.

Here is the expression I have on the position of S1:

input = thisComp.layer("SETUP").text.sourceText;
xPos = if( (input == "Target1") ) 760 else 0;
yPos = if( (input == "Target1") ) 340 else 0;
[xPos, yPos]

In my mind that should be working. But it's not. Haha. I'm getting an error that says: "Error at line 2 in property 'Position' of layer 3 ('S1') in comp 'Positions Test_001'. Illegal use of reserved word., an expression was disabled as a result of an error.

Anyone got any ideas? It'd be greatly appreciated. Thank you!!!

Return to posts index

Walter SoykaRe: If/Else Statement + Source Text + Position Coordinates
by on Sep 28, 2016 at 6:57:12 pm

You cannot use an if statement to assign a value like this. If controls flow and does not return a value.

You have two options. One is to use if properly:

input = thisComp.layer("SETUP").text.sourceText;
if (input == "Target1") xPos = 760 else xPos = 0;
if (input == "Target1") yPos = 340 else yPos = 0;
[xPos, yPos]

The other is to use the ternary conditional operator, which looks like this:
statementForEvaluation ? valueIfTrue : valueIfFalse;
and returns one of the values according to the evaluation of the statement:

input = thisComp.layer("SETUP").text.sourceText;
xPos = (input == "Target1") ? 760 : 0;
yPos = (input == "Target1") ? 340 : 0;
[xPos, yPos]

Walter Soyka
Designer & Mad Scientist at Keen Live [link]
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
@keenlive [twitter]   |   RenderBreak [blog]   |   Profile [LinkedIn]

Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2016 All Rights Reserved