Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions AE Script for creating/adding layers

  • AE Script for creating/adding layers

    Posted by James Lex on March 29, 2018 at 6:55 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.

    James Lex replied 8 years, 1 month ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    March 29, 2018 at 7:49 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

  • James Lex

    March 29, 2018 at 8:38 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.

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