ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

insert variable into string

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Bryan Woods
insert variable into string
on Sep 25, 2015 at 7:59:03 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.


Return to posts index


Dan Ebberts
Re: insert variable into string
on Sep 25, 2015 at 8:09:38 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



Return to posts index

Bryan Woods
Re: insert variable into string
on Sep 25, 2015 at 8:43: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!


Return to posts index



Dan Ebberts
Re: insert variable into string
on Sep 25, 2015 at 8:52:28 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



Return to posts index

Patrick Grossien
Re: insert variable into string
on Feb 11, 2017 at 3:25:47 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");


Return to posts index

Dan Ebberts
Re: insert variable into string
on Feb 11, 2017 at 4:11:57 pm

I think this is all you need to do:

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

Dan



Return to posts index


Patrick Grossien
Re: insert variable into string
on Feb 22, 2017 at 4:26:43 pm

YES! Thanks Dan!

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


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]