ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

AE Script for creating/adding layers

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
James Lex
AE Script for creating/adding layers
on Mar 29, 2018 at 6:55:37 pm

I'm creating a script that creates a solid layer, applies a Fractal Noise effect, then precomposes the layer.



var proj = app.project;
var comp = proj.items.addComp(name, 1920, 1080, 1.0, 30.0, 29.97);

var distortionLayer = comp.layers.addSolid([0, 0, 0], 'Distortion', 1920, 1080, 1.0);
distortionLayer.Effects.addProperty("ADBE Fractal Noise");
distortionLayer.enabled = false;
comp.layers.precompose([1], 'jj_Distortion', true);



Now this is fine for one comp, but if the script is run again, it will simply create another "jj_Distortion" comp. Instead, I want to check if the "jj_Distortion" comp exists. If it does, then add it to the current comp. Else, create the comp.

I know the example below is not code, but I'm thinking it could look like this:



if(layer("jj_Distortion").exists()){
//Add the jj_Distortion comp to this comp
} else {
// Create it
var distortionLayer = comp.layers.addSolid([0, 0, 0], 'Distortion', 1920, 1080, 1.0);
distortionLayer.Effects.addProperty("ADBE Fractal Noise");
distortionLayer.enabled = false;
comp.layers.precompose([1], 'jj_Distortion', true);
}



I've gone through the CS6 Scripting guide, but I can't find the correct command(s) there or through Google.

Any help would be greatly appreciated.


Return to posts index

Dan Ebberts
Re: AE Script for creating/adding layers
on Mar 29, 2018 at 7:49:18 pm

A rough outline of how I'd do it (not tested at all):

function getComp(theName){
for (var i = 1; i <= app.project.numItems; i++){
if ((app.project.item(i).name == theName) && (app.project.item(i) instanceof CompItem)){
return app.project.item(i);
}
}
return null;
}

var distortionComp = getComp("jj_Distortion");
if (distortionComp == null){
//
// doesn't exist -- create it
}else{
comp.layers.add(distortionComp);
}


Dan



Return to posts index

James Lex
Re: AE Script for creating/adding layers
on Mar 29, 2018 at 8:38:05 pm

Dan Ebberts—legend himself—has saved me. Thank you!

Ultimately, the for loop I wrote wasn't giving me the desired result; yours works perfectly.


Return to posts index

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