Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions selectLayers script

  • selectLayers script

    Posted by Scott Mcgee on July 31, 2018 at 10:39 am

    I understand that selected layers, requires a bit more brain hurt, but to get a selected item

    for(var i = 0; i <= 3 ; i++ ){
    b[i].onClick =
    (function(i){
    return function() {
    app.beginUndoGroup(“AddEffect”);
    var curItem = app.project.activeItem;
    var selectedLayers = curItem.selectedLayers;

    try{
    var curRem = curItem.selectedLayers[0].effect.property(“Fill”).remove();
    var curLayer = curItem.selectedLayers[0].Effects.addProperty(“ADBE Fill”)(“Color”).setValue(pcolor[i])
    }catch(err){
    var curLayer = curItem.selectedLayers[0].Effects.addProperty(“ADBE Fill”)(“Color”).setValue(pcolor[i]);
    }

    app.endUndoGroup();
    }
    })(i);
    }

    This works brilliantly, but I’ve tried a few different ways to select multiple layers and get it to work, but the

    })(i);

    part towards the end, keeps flagging errors with me.

    Any ideas?

    Scott Mcgee replied 8 years ago 2 Members · 7 Replies
  • 7 Replies
  • James Ronan

    August 1, 2018 at 4:38 pm

    I’m not 100% following your code, but the “curItem.selectedLayers[0]” parts will mean it will only affect the first layer selected.

    You could try adding a for loop, going through your selected layers to affect all of them:

    var selectedLayers = curItem.selectedLayers;
    var curRem, curLayer;

    for (var j = 0; j < selectedLayers.length; j++) {

    try {
    curRem = selectedLayers[j].effect.property("Fill").remove();
    curLayer = selectedLayers[j].Effects.addProperty("ADBE Fill")("Color").setValue(pcolor[i]);
    }
    catch (err) {
    curLayer = selectedLayers[j].Effects.addProperty("ADBE Fill")("Color").setValue(pcolor[i]);
    }
    }

    Hope that helps!

  • Scott Mcgee

    August 2, 2018 at 12:06 pm

    Nearly there.

    I can highlight multiple layers, but the first one I selected. Is the only one getting the fill added.

    my first loop

    for(var i = 0; i <= 3 ; i++ ){
    b[i].onClick = (function(i){

    I have 25 buttons, I couldn’t be bothered to have 25 buttons written individually. So I created a loop (Test above I did on four) which in the above case has four buttons . So that part of the loop is to recognise which button I pushed.

    I’ve assigned each button to an array. Button1 is [0] button2[1] …etc

    The issue was without (i) at the bottom of the expression, it would add the fill but would reference the array to button and add that colour.

    What I want to do now is upgrade it to select several layers. Hit the button and [i] then gets added to all. If that helps make a bit more sense.

  • Scott Mcgee

    August 2, 2018 at 12:07 pm

    I say my first loop. I mean my only loop.

    But I know I need to add a second loop and that’s where I’m struggling.

  • Scott Mcgee

    August 2, 2018 at 12:13 pm

    Ignore everything I said. I can’t have deleted one little something. That’s all working. Thank you very much.

    🙂

  • James Ronan

    August 2, 2018 at 2:27 pm

    Hey Scott.

    Here’s a rough example of a window with 3 buttons, that adds or replaces the fill effect across all selected layers:

    Hope that helps!

    var win = new Window('dialog', "Test");

    var btnAry = [];
    var colAry =
    [
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
    ];

    var clickHandlerFill = function(i) {

    return function() {

    app.beginUndoGroup("AddEffect");
    var curItem = app.project.activeItem;
    var selectedLayers = curItem.selectedLayers;
    var curRem, curLayer;

    for (var j = 0; j < selectedLayers.length; j++) {

    try {
    curRem = selectedLayers[j].effect.property("Fill").remove();
    curLayer = selectedLayers[j].Effects.addProperty("ADBE Fill")("Color").setValue(colAry[i]);
    }
    catch (err) {
    curLayer = selectedLayers[j].Effects.addProperty("ADBE Fill")("Color").setValue(colAry[i]);
    }
    }

    app.endUndoGroup();

    };
    }

    // Create buttons and assign clickHandler
    for (var i = 0; i < 3; i++) {

    btnAry[i] = win.add('button', undefined, i);
    btnAry[i].onClick = clickHandlerFill(i);

    }

    win.show();

  • James Ronan

    August 2, 2018 at 2:31 pm

    Ah, hadn’t seen your later messages.
    No worries, glad you got it working!

  • Scott Mcgee

    August 2, 2018 at 3:47 pm

    That is much tidier than what I have and makes a lot more sense.

    Mine still works, but I’ll adapt it to be more like yours as it looks less clunky. I think I’ve just gone an arse uppard way of doing it haha.

    cheers for the help

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