ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

selectLayers script

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Scott McGee
selectLayers script
on Jul 31, 2018 at 10:39:54 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?


Return to posts index

James Ronan
Re: selectLayers script
on Aug 1, 2018 at 4:38:49 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!


Return to posts index

Scott McGee
Re: selectLayers script
on Aug 2, 2018 at 12:06: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.


Return to posts index


Scott McGee
Re: selectLayers script
on Aug 2, 2018 at 12:07:31 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.


Return to posts index

Scott McGee
Re: selectLayers script
on Aug 2, 2018 at 12:13:29 pm

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

:)


Return to posts index

James Ronan
Re: selectLayers script
on Aug 2, 2018 at 2:27:17 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();



Return to posts index


James Ronan
Re: selectLayers script
on Aug 2, 2018 at 2:31:01 pm

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


Return to posts index

Scott McGee
Re: selectLayers script
on Aug 2, 2018 at 3:47:04 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


Return to posts index

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