ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Loading an expression with a script

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Ben Frank
Loading an expression with a script
on Nov 4, 2009 at 10:19:55 pm

I have a script that does a few things, and upon completion i need it to add an expression to the position of a layer in a comp. the expression is saved on its own in a .js file and the script is supposed to load it. however, this isn't working at all the way i expect it to. here's what i have now. any help would be GREATLY appreciated.

var positionExpression = new File("R10_PositionExpression.js");
controlComp.layer("Bounce Variable Controls").position.expression = positionExpression;


Thanks!


ben

--
Check out my food blog: http://iAteThat.com
--
benfRank
art direction / broadcast, print & web design / photography
WEB: http://www.behance.net/benfrank/Frame
WEB: http://www.benfrankdesign.com
WEB: http://www.benfrankphotography.com


Return to posts index

Xinlai Ni
Re: Loading an expression with a script
on Nov 4, 2009 at 10:34:50 pm

positionExpression is of File type, yet used as string.
Why not hard-code your expression in the same jsx as this script? File I/O can be expensive.

Xinlai Ni
Software Engineer, Google Inc.


Return to posts index

Ben Frank
Re: Loading an expression with a script
on Nov 4, 2009 at 11:01:32 pm

yeah, i realised that. i modified it ad got it working this way:

controlComp.layer("Bounce Variable Controls").position.expression = "#include 'file:///R10-PositionExpression.txt'"

It works perfectly. Only issue now is figuring out how to get the script to apply it to every layer of every comp.

ben

--
Check out my food blog: http://iAteThat.com
--
benfRank
art direction / broadcast, print & web design / photography
WEB: http://www.behance.net/benfrank/Frame
WEB: http://www.benfrankdesign.com
WEB: http://www.benfrankphotography.com


Return to posts index


Dan Ebberts
Re: Loading an expression with a script
on Nov 4, 2009 at 11:25:22 pm

I think you need to open the file, read the file into a string variable, close the file, and store the string into the position expression. See the JavaScript Tools doc if you don't know how to do this.


Dan



Return to posts index

Ben Frank
Re: Loading an expression with a script
on Nov 4, 2009 at 11:34:29 pm

I have the expression being pulled in, but i am having trouble figuring out the number of comps in a project. here's the code that isnt' working:

for (i = 0, i <= numComps, i++) {
myComp = app.project.item[i];
for (j = 0, j <= numLayers, j++) {
mycomp.layer[j].position.expression = "#include 'file:///Volumes/adtxraid_09/R10_REBRAND/01_AE_projects/00_AE_PRESETS/_scripts/R10-PositionExpression.txt'";
mycomp.layer[j].rotation.expression = "#include 'file:///Volumes/adtxraid_09/R10_REBRAND/01_AE_projects/00_AE_PRESETS/_scripts/R10-PositionExpression.txt'";
mycomp.layer[j].scale.expression = "#include 'file:///Volumes/adtxraid_09/R10_REBRAND/01_AE_projects/00_AE_PRESETS/_scripts/R10-PositionExpression.txt'";
}
}


ben

--
Check out my food blog: http://iAteThat.com
--
benfRank
art direction / broadcast, print & web design / photography
WEB: http://www.behance.net/benfrank/Frame
WEB: http://www.benfrankdesign.com
WEB: http://www.benfrankphotography.com


Return to posts index

Dan Ebberts
Re: Loading an expression with a script
on Nov 5, 2009 at 12:06:25 am

You need to loop through app.project.items (from 1 thru numItems) looking for items that are instanceof CompItem.

Dan



Return to posts index


Ben Frank
Re: Loading an expression with a script
on Nov 5, 2009 at 11:10:09 pm

thanks so much, dan! i finally got this working.

// create new comp named 'Bounce Variable Controls'
var compW = 720; // comp width
var compH = 540; // comp height
var compL = 1; // comp length (seconds)
var compRate = 29.97; // comp frame rate
var compBG = [0/255,0/255,0/255] // comp background color

var myItemCollection = app.project.items;
var controlComp = myItemCollection.addComp('Bounce Variable Controls',compW,compH,1,compL,compRate);
controlComp.bgColor = compBG;


// create new 50X50 yellow solid named 'Bounce Variable Controls'
mySolid = controlComp.layers.addSolid([255/255,255/255,0/255], "Bounce Variable Controls", 50, 50, 1);


// Add "Amplitude" Slider and set value to 10
Slider1 = mySolid.property("Effects").addProperty("Slider Control");
Slider1.name = "Amplitude";
controlComp.layer("Bounce Variable Controls")("Effects")("Amplitude")("Slider").setValue(10);

// Add "Frequency" Slider and set value to 40
Slider2 = mySolid.property("Effects").addProperty("Slider Control");
Slider2.name = "Frequency";
controlComp.layer("Bounce Variable Controls")("Effects")("Frequency")("Slider").setValue(40);

// Add "Decay" Slider and set value to 100
Slider3 = mySolid.property("Effects").addProperty("Slider Control");
Slider3.name = "Decay";
controlComp.layer("Bounce Variable Controls")("Effects")("Decay")("Slider").setValue(100);


// Add Expressions to Position, Rotation & Scale of All Layers in project
for (var i = 1; i <= app.project.numItems; i++) {
if (app.project.item(i) instanceof CompItem && app.project.item(i).name != "Bounce Variable Controls") {
for (var j = 1; j <= app.project.item(i).numLayers; j++) {
app.project.item(i).layer(j).position.expression = "// Auto Easing with Retract for Positionn n = 0;n if (numKeys > 0){n n = nearestKey(time).index;n if (key(n).time > time){n n--;n }n }n if (n == 0){n t = 0;n }else{n t = time - key(n).time;n }n n if (n > 0){n v = velocityAtTime(key(n).time - thisComp.frameDuration/10);n amp = .01*comp("Bounce Variable Controls").layer("Bounce Variable Controls").effect("Amplitude")("Slider");n freq = .1*comp("Bounce Variable Controls").layer("Bounce Variable Controls").effect("Frequency")("Slider");n decay = .1*comp("Bounce Variable Controls").layer("Bounce Variable Controls").effect("Decay")("Slider");n value + v*amp*Math.sin(freq*t)/Math.exp(decay*t*2);n }else{n value;n }";

app.project.item(i).layer(j).rotation.expression = "// Auto Easing with Retract for Rotationn n = 0;n if (numKeys > 0){n n = nearestKey(time).index;n if (key(n).time > time){n n--;n }n }n if (n == 0){n t = 0;n }else{n t = time - key(n).time;n }n n if (n > 0){n v = velocityAtTime(key(n).time - thisComp.frameDuration/10);n amp = .005*comp("Bounce Variable Controls").layer("Bounce Variable Controls").effect("Amplitude")("Slider");n freq = .1*comp("Bounce Variable Controls").layer("Bounce Variable Controls").effect("Frequency")("Slider");n decay = .1*comp("Bounce Variable Controls").layer("Bounce Variable Controls").effect("Decay")("Slider");n value + v*amp*Math.sin(freq*t)/Math.exp(decay*t*2);n }else{n value;n }";

app.project.item(i).layer(j).scale.expression = "// Auto Easing with Retract for Scalen n = 0;n if (numKeys > 0){n n = nearestKey(time).index;n if (key(n).time > time){n n--;n }n }n if (n == 0){n t = 0;n }else{n t = time - key(n).time;n }n n if (n > 0){n v = velocityAtTime(key(n).time - thisComp.frameDuration/10);n amp = .05*comp("Bounce Variable Controls").layer("Bounce Variable Controls").effect("Amplitude")("Slider");n freq = .1*comp("Bounce Variable Controls").layer("Bounce Variable Controls").effect("Frequency")("Slider");n decay = .1*comp("Bounce Variable Controls").layer("Bounce Variable Controls").effect("Decay")("Slider");n value + v*amp*Math.sin(freq*t)/Math.exp(decay*t*2);n }else{n value;n }";
}
}
}


ben

--
Check out my food blog: http://iAteThat.com
--
benfRank
art direction / broadcast, print & web design / photography
WEB: http://www.behance.net/benfrank/Frame
WEB: http://www.benfrankdesign.com
WEB: http://www.benfrankphotography.com


Return to posts index

Adam forbes
Re: Loading an expression with a script
on Nov 12, 2009 at 7:04:04 am

Hey Ben

I need something very similar, I need a script that will go to every layer in the project and add this to the anchorpoint and position

tempx = value[0];
tempy = value[1];

tempxwhole = Math.round(tempx);
tempywhole = Math.round(tempy);
[tempxwhole, tempywhole]

It seems very similar to what you just did. I need it for a project ASAP. If you know how to do it could you send me the jsx. I don't know much about scripting just expressions.


Return to posts index

Ben Frank
Re: Loading an expression with a script
on Nov 12, 2009 at 2:58:22 pm

Copy and past the following into a plain text editor and save it as a .jsx file. when you run the script is should do what you are wanting. hope this helps!

// Add Expressions to Position, Rotation & Scale of All Layers in project
for (var i = 1; i <= app.project.numItems; i++) {
if (app.project.item(i) instanceof CompItem && app.project.item(i).name != "Bounce Variable Controls") {
for (var j = 1; j <= app.project.item(i).numLayers; j++) {
app.project.item(i).layer(j).position.expression = "tempx = value[0];ntempy = value[1];nntempxwhole = Math.round(tempx);ntempywhole = Math.round(tempy);n[tempxwhole, tempywhole]n";
app.project.item(i).layer(j).anchorPoint.expression = "tempx = value[0];ntempy = value[1];nntempxwhole = Math.round(tempx);ntempywhole = Math.round(tempy);n[tempxwhole, tempywhole]n";
}
}
}


ben

--
Check out my food blog: http://iAteThat.com
--
benfRank
art direction / broadcast, print & web design / photography
WEB: http://www.behance.net/benfrank/Frame
WEB: http://www.benfrankdesign.com
WEB: http://www.benfrankphotography.com


Return to posts index

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