Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects If/Else Statement + Source Text + Position Coordinates

  • If/Else Statement + Source Text + Position Coordinates

    Posted by Paul Connors on September 28, 2016 at 3:04 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!!!

    Paul Connors replied 9 years, 6 months ago 2 Members · 2 Replies
  • 2 Replies
  • Walter Soyka

    September 28, 2016 at 6:57 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   |   RenderBreak [blog]   |   Profile [LinkedIn]

  • Paul Connors

    November 1, 2016 at 3:32 pm

    Hi Walter. I’m really sorry for the late reply. Thanks very much for the help with this!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy