ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Remove all fills in a shape layer?

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
linus nystrom
Remove all fills in a shape layer?
on Sep 2, 2015 at 4:58:47 pm

Hi!

Im trying to make a little script where I basically want to select a shape layer, press a button, and that will make it duplicate and delete all the fills from all the shapes inside.

I've succeeded with the duplication bit, and I've figured out how to ADD properties (like fills or strokes) via the script, but I cant figure out how to access the fill attribute without knowing the exact path (which I dont since I dont know how many shapes the layer contain).

Im thinking there must be a short way of searching through the layer, and if the shape contains a fill, delete it (or make it 0% opacity).

Thanks a lot!


Return to posts index

Miguel de Mendoza
Re: Remove all fills in a shape layer?
on Sep 2, 2015 at 8:41:22 pm

Try this:

var proj = app.project;
var layer = proj.activeItem.selectedLayers[0];
var contents = layer.property("ADBE Root Vectors Group");

function removeFill(){
if(!layer || !(layer instanceof ShapeLayer)){
alert("Select a shape layer");
return;
}
for(i = 1; i &lt;= contents.numProperties ; i++){
var shape = contents.property(i).property("Contents");
if(shape.property("ADBE Vector Graphic - Fill")){
shape.property("ADBE Vector Graphic - Fill").remove();
}
}
}

removeFill();


Return to posts index

linus nystrom
Re: Remove all fills in a shape layer?
on Sep 3, 2015 at 7:47:25 am

Oh wow, That works perfectly, and a much fewer lines than mine :D
Thanks a lot!

This works awesome if all shapes are placed directly in the contents level. If If I would like to step this up a notch, to make it search deeper, would the easiest way be to make an if statement for each possible level?


Return to posts index


Miguel de Mendoza
Re: Remove all fills in a shape layer?
on Sep 3, 2015 at 8:38:43 am

For acomplish that you need to edit removeFill() function to be recursive, in pseudo pseudo-code something like this:

function removeFill(node):
if node[i].numProperties > 0 do->
for nodes in node do->
if node[i] is shape do-> remove fill
else if do -> removeFill(node[i])

removeFill(contents)

For sure you will need to work on it, because accesing properties on shape layer can be tricky. I recomend you to make a "prototype layer" with saveral shapes on diferent levels(as messy as you can to check the "worst case") and start accesing manualy to each property by index to see the structure .


Return to posts index

Xavier Gomez
Re: Remove all fills in a shape layer?
on Sep 3, 2015 at 9:15:20 am

The following will work in your case.
Choose handler=myHandler1 to remove all fills, and myHandler2 to set opacity to 0.
It's fairly generic, but you dont need to write it that way (with a separate handler function) if you only have one thing to do with those fills.

Xavier.

// handler : g should be a property group that can be added to a shape (empty group / path /stroke / fill / trim path etc)
function myHandler1(g){
if (g.matchName === "ADBE Vector Graphic - Fill") g.remove();
};
function myHandler2(g){
if (g.matchName === "ADBE Vector Graphic - Fill") g.opacity.setValue(0);
};

// main function : g is expected to be a shape layer or a shape group (shape "sublayer")
// handler : what to do with the addable content of g
// recursive, and recurses backwards, in case some children are removed...
// doesnt scout the transform group, only addable things
function scoutContent(g, handler){
var n, child;
if (!g.content) return;
g = g.content;
for (n=g.numProperties; n>0; n--){
child = g.property(n);
handler(child);
if (Object.isValid(child) && child.content) scoutContent(child, handler);
};
};

scoutContent(app.project.activeItem.selectedLayers[0], myHandler2);


Return to posts index

linus nystrom
Re: Remove all fills in a shape layer?
on Sep 3, 2015 at 2:21:46 pm

Ouff!

I will have try to get this to work with my script, thanks a lot mate!


Return to posts index


John Colombo
Re: Remove all fills in a shape layer?
on Nov 8, 2016 at 12:26:14 am
Last Edited By John Colombo on Nov 11, 2016 at 8:14:13 am

Give this one a try! I was looking for the same feature to quickly remove fills. With that in mind, I took some snippets from Miguel's code and made it loop through all the selected layers.

This script will toggle the fills without removing them.

Also, holding Alt (or Option) will toggle strokes instead of fills. Watch out for the "<=" in the second for(){} loop, the code box seems to mess it up.

Cheers!

// Define variables
app.beginUndoGroup("Remove Fill on Shape Layer");
var myComp = app.project.activeItem;
var myLayer = myComp.selectedLayers;
var myKeyState = ScriptUI.environment.keyboardState;

try {

for (var i=0;i<myLayer.length;i++){

if (myLayer[i] instanceof ShapeLayer){

//alert("This is a shape");
var contents = myLayer[i].property("ADBE Root Vectors Group");

for(s = 1; s <= contents.numProperties ; s++){
var shape = contents.property(s).property("Contents");
if (myKeyState.altKey == true){
if(shape.property("ADBE Vector Graphic - Stroke").active == false){
shape.property("ADBE Vector Graphic - Stroke").enabled = true;
}else{shape.property("ADBE Vector Graphic - Stroke").enabled = false}
}else{
if(shape.property("ADBE Vector Graphic - Fill").active == false){
shape.property("ADBE Vector Graphic - Fill").enabled = true;
}else{shape.property("ADBE Vector Graphic - Fill").enabled = false;}
}
}
}else{

continue //skip non-shape layers
}
}
}catch(err){
alert(err.line + "n" + err.toString());
}
app.endUndoGroup();
app.activeViewer.setActive();


John Colombo
Lead Creator
1Resonant Creation
http://www.1resonant.com


Return to posts index

Arun Chaudhary
Re: Remove all fills in a shape layer?
on Sep 11, 2017 at 1:52:09 pm

Hi, I need a little help
John Colombo script works fine for active comp. if this script works for selected comp. in a project
and add a stroke in all shape layer stroke width can be set.

Thank you
Arun


Return to posts index

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