Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Script to add an effect to selected layers

  • Script to add an effect to selected layers

    Posted by Stuart Elith on April 8, 2009 at 1:45 am

    Hi all,

    In the general After Effects forum someone suggested that you can create hotkeys for applying effects by creating a script that applies the effect and then assigning that a hotkey. I think it’s a great idea, so wanted to try it out.

    I haven’t done scripting for AE before but have a little bit of general knowledge so thought I might be able to piece it together, but haven’t had much luck. Through amalgamating stuff from a few sites I came up with something like this, but it doesn’t work…

    var effectsGroup = app.project.activeItem.selectedLayers;
    myEffect = effectsGroup.Effects.addProperty(“ADBE Fast Blur”);

    I thought that by following the guide from redefinery I could combine the first 2 parts (check if a layer can have effects applied, then apply an effect), but that hasn’t worked either.

    https://www.redefinery.com/ae/fundamentals/effects/

    Thanks for any guidance, I think this should be pretty straightforward but i’m a bit stuck 🙂

    Cheers,
    Stuart

    Emre Mutlu replied 5 years, 11 months ago 5 Members · 6 Replies
  • 6 Replies
  • Dan Ebberts

    April 8, 2009 at 2:06 am

    One problem I see is that selectedLayers is an array. If you only have one layer selected, you would access it this way:

    selectedLayers[0]

    Dan

  • Stuart Elith

    April 9, 2009 at 2:11 am

    Ahh thanks for that Dan, i probably wouldn’t have picked it up!

    Well I’ve got it working after more trial and error… i’m not sure exactly why this particular code works but it does, which is good enough for me!
    It can only apply to one layer at a time but that’s not a big bother for me either… I thought i worked out how to do it for more but it’s not working and I don’t care enough to spend a few hours getting annoyed while I work it out!

    So for anyone else who’s interested :

    {
    // create an undo group
    app.beginUndoGroup(“AddEffect”);

    var curItem = app.project.activeItem;
    var selectedLayers = curItem.selectedLayers;

    // check if comp is selected
    if (curItem == null || !(curItem instanceof CompItem)){

    // if no comp selected, display an alert
    alert(“Please establish a comp as the active item and run the script again”);

    } else {

    // define the layer in the loop we’re currently looking at
    var curLayer = curItem.selectedLayers[0];

    // check if that layer is a footage layer
    if (curLayer.matchName == “ADBE AV Layer”){

    // add a slider and three keyframes
    var slider = curLayer.Effects.addProperty(“ADBE Fast Blur”);

    }
    }

    // close the undo group
    app.endUndoGroup();
    }

    Most of this code is based upon someone else’s from something, I’m sorry to say I don’t remember where it was from, so i can’t credit the person.

  • Hamid Rohi-bilverdy

    June 14, 2015 at 10:39 am

    Hi

    Sorry for jumping in here so late.
    But curItem.selectedLayers[0]; is selected from array, what if you wanted the actual selected layers??

    So if I wanted to apply an effect on several layers??

  • David Wigforss

    August 30, 2018 at 5:33 pm

    Since no one answered you….

    You need to run thru a loop.


    // define the layer in the loop we're currently looking at
    for (i=0; i<selectedLayers.length; i++){
    var curLayer = curItem.selectedLayers[i];

    // add effect
    var slider = curLayer.Effects.addProperty("ADBE Fast Blur");
    }

  • Emre Mutlu

    May 31, 2020 at 2:51 pm

    When i use this method in a variable i can’t use that var later. For example when i select a comp from timeline and run this script it says “undefined”. What is the proper way of doing this? What causes this problem?

    Like :

    var mainComp = app.project.item(1);
    var selection = mainComp.selectedLayers[0];
    alert(selection.numLayers);

    alert says “undefined”.

  • Emre Mutlu

    May 31, 2020 at 7:33 pm

    So i found what the problem is. Afx thinks it’s an AVLayer instead of CompItem when i use this line :

    var selection = mainComp.selectedLayers[0];

    I edited the script like this and it works :

    var mainComp = app.project.item(1);
    var selection = mainComp.selectedLayers[0].source;

    alert(selectedComp.numLayers);

    The result is exact number with this way. I’m not sure if it’s the correct way to do this but it works. If there’s an alternative way to do this, i would like to know. I hope it helps 🙂

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