Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions insert variable into string

  • insert variable into string

    Posted by Bryan Woods on September 25, 2015 at 7:59 pm

    I’m using a script to insert an expression into a layer’s position property. How do I add variables into the expression thats being set?

    for example, lets say:
    var theLayer = “Some Layer Name”;

    and the position property of the selected layer my script is adding an expression to is:

    myLayer.property(“position”).expression = “thisComp.layer(\”theLayer\”).position;

    How do I get “theyLayer” variable’s value to be inserted into the expression? I can’t figure it out. In python its as easy as %s, then defining your variables after the quotes of the string in order as they appear. But here, I’m not sure.

    Patrick Grossien replied 9 years, 2 months ago 3 Members · 6 Replies
  • 6 Replies
  • Dan Ebberts

    September 25, 2015 at 8:09 pm

    This should work:

    myLayer.property(“position”).expression = ‘thisComp.layer(“‘ + theLayer + ‘”).position;’;

    Note the use of single quotes to get the double quotes into the expression.

    Dan

  • Bryan Woods

    September 25, 2015 at 8:43 pm

    Yup, that did the trick. Although my example wasn’t entirely accurate, I needed to add .toString() to my variable to get it to work properly, but I got it working! Glad to see you’re still hanging around here Dan! Always appreciative of your help. Its been a while since I’ve needed to script in AE again; a little rusty.

    Actually, one quick followup:
    How would you insert a large chunk of expression but keep it readable in my script editor? At the moment, I have to crush all of the expression code I’m inserting into one very long line. Is there some operator, like \n to do a line break, but not affect the actual expression being inserted?

    Thanks again!

  • Dan Ebberts

    September 25, 2015 at 8:52 pm

    Mine usually end up looking like this:


    var theExpr = 'x = 100;\r' +
    'y = 100;\r' +
    'z = 100;\r' +
    '[x,y,z]';

    var theLayer = app.project.activeItem.layer(1);
    theLayer.property("Position").expression = theExpr;

    Dan

  • Patrick Grossien

    February 11, 2017 at 3:25 pm

    Hi Dan,

    I’m facing a similar problem. I’m trying to reference the name of a parent composition via a text layer source text in a pre-comp (in order to grab values of controllers in that parent comp).

    Any idea if and how I might get this to work?

    parentComp = '"'+thisComp.layer("Name_Parent_Comp").text.sourceText+'"';
    freq= comp(parentComp).layer("Glitch Controller").effect("RGB glitch FREQUENCY")("Slider");
    amp= comp(parentComp).layer("Glitch Controller").effect("RGB glitch AMPLITUDE")("Slider");

  • Dan Ebberts

    February 11, 2017 at 4:11 pm

    I think this is all you need to do:

    parentComp = thisComp.layer(“Name_Parent_Comp”).text.sourceText.value;

    Dan

  • Patrick Grossien

    February 22, 2017 at 4:26 pm

    YES! Thanks Dan!

    parentComp = thisComp.layer("Name_Parent_Comp").text.sourceText.value;
    freq= comp(parentComp).layer("Control_Layer").effect("frequency")("Slider");

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