Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Using variables to build an expression in a script

  • Using variables to build an expression in a script

    Posted by Robert Headrick on April 18, 2013 at 10:51 pm

    In a script I’m working on, I have several layers stored as elements in an array. Later on in the script, I create a series of new layers. I want the value of certain parameters to be linked via expressions to the position values of the layers stored in my array. However, I’m not sure how to do this, because the syntax seems to be that you have to write the expression as a string in the script, like so:

    app.project.activeItem.layer(“example layer”).transform.position.expression = “expression goes here as a string”;

    I’m not sure how that works when part of what I want to include in my expression is a reference to an array. I’ve tried the following, which does not work:

    app.project.activeItem.layer(“example layer”).transform.position.expression = layerArray[3] + “.transform.position;”;

    Any ideas? I’m probably just approaching this wrong.

    Robert Headrick replied 13 years ago 2 Members · 3 Replies
  • 3 Replies
  • Xavier Gomez

    April 19, 2013 at 11:52 am

    layerArray[3] is a layer object, you only want a string that refers to it. I’m not sure for the sequel though.

    To refer by index, maybe “thisComp.layer(” + layerArray[3].index + “)” + “.transform.position;”;

    Similarly for reference by name.

  • Robert Headrick

    April 22, 2013 at 2:09 pm

    This sort of works, but I’m having some further issues. I created this line of code:

    beamEffect(“Starting Point”).expression = “thisComp.layer(” + nodeLayer.index + “).transform.position;”;

    This creates a proper expression, but because it’s referencing layers by index value, the expressions start referencing the wrong values if the order of layers changes (which it does, as a part of the script; multiple new layers are being created in the process).

    I tried adjusting it to reference layers by name instead of index, but it didn’t seem to like that. I adjusted the code to read like this:

    beamEffect(“Starting Point”).expression = “thisComp.layer(” + nodeLayer.name + “).transform.position;”;

    But it errors out on that line saying “Class ‘global’ has no property or method named ‘nodeLayer'”, so it doesn’t seem to like that syntax.

  • Robert Headrick

    April 22, 2013 at 6:24 pm

    Figured it out. The result of layerName.name is the name with no quote marks, but the expression requires them. So all I need to do was double the quotations in my string to create the following:

    beamEffect(“Starting Point”).expression = ‘thisComp.layer(“‘ + nodeLayer.name + ‘”).transform.position;’;

    It might be hard to see on here, but I changed the double-quotes to single quotes, and added a double-quote immediately after the open-parentheses and immediately before the close-parentheses. This adds quote marks around the layer name inside the expression when it generates the string. And now my expression properly targets the actual layer, rather than referencing a layer index.

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