Activity › Forums › Adobe After Effects Expressions › Finding color property of a shape layer.
-
Finding color property of a shape layer.
Posted by Joshua Faget on September 22, 2016 at 3:43 amHello,
Is there an efficient way to find the “Fill” color of a shape layer. As a shape layer can have multiple shapes inside, I want my script to be able to find all fills and replace them. I can do the replacement part, but I can’t figure out a way to efficiently find the fills. Also Im running into trouble when there are fills that are grouped a couple of times.
Thank you.
Santi Agustí replied 6 years, 5 months ago 4 Members · 5 Replies -
5 Replies
-
Xavier Gomez
September 22, 2016 at 7:41 amTo scout a shape layer, you can use a generic recursive function like the one below:
function recurseShape(g, groupHandler){
// g shape layer or group inside a shape ( should have a content property )
// groupHandler : what do do with each groupif (!g.content) return; // doesnt apply
groupHandler(g);
if (!g.content.property("ADBE Vector Group")) return;
for (var n=1; n<=g.content.numProperties; n++){
if (g.content.property(n).matchName==="ADBE Vector Group") recurseShape(g.content.property(n), groupHandler);
};
};The argument ‘groupHandler’ is itself a function and tells what to do with the immediate content of the shape layer or group inside it.
In case the ‘thing to do’ is remove or reorganize children, it should be written in a way no error is thrown (!)
For instance, to change fills to gradient fills, it could be:function myGroupHandler_changeFillToGFill(g){
// g shape layer or group inside a shape ( should have a content property )if (!g.content || !g.content.property("ADBE Vector Graphic - Fill")) return;
for (var n=1; n<=g.content.numProperties; n++){
if (g.content.property(n).matchName==="ADBE Vector Graphic - Fill"){
g.content.property(n).remove();
g.content.addProperty("ADBE Vector Graphic - G-Fill").moveTo(n);
};
};
};And apply this way:
var comp = app.project.activeItem;
app.beginUndoGroup("Change Fills to G-Fills");
recurseShape(comp.layer(1), myGroupHandler_changeFillToGFill);
app.endUndoGroup();That’s one way to do, you can rewrite your own, but hopefully you get the spirit.
Xavier
-
Joshua Faget
September 22, 2016 at 9:06 pmWow, thank you very much, this is really useful.
But unfortunately I am running into an issue. I got it to change the fill with a value in a predefined variable, BUT it seems to be breaking the recursion when I try to slip some more information in the functions.
var rgb = [0.5, 1, 1];ChangeColorButton.onClick = function() {
var Color = rgb;//TheSelectedLayer
var Selection = app.project.activeItem.selectedLayers[0];//RECURSION FUNCTION
function recurseShape(g, groupHandler, rgb) {
// g shape layer or group inside a shape ( should have a content property )
// groupHandler : what do do with each groupif (!g.content) return; // doesnt apply
groupHandler(g, rgb);
if (!g.content.property("ADBE Vector Group")) return;for (var n = 1; n <= g.content.numProperties; n++) {
if (g.content.property(n).matchName === "ADBE Vector Group") recurseShape(g.content.property(n), groupHandler);
};
};//WHAT TO DO
function myGroupHandler_changeFillToGFill(g, rgb) {
// g shape layer or group inside a shape ( should have a content property )
if (!g.content || !g.content.property("ADBE Vector Graphic - Fill")) return;for (var n = 1; n <= g.content.numProperties; n++) {
if (g.content.property(n).matchName === "ADBE Vector Graphic - Fill") {
g.content.property(n).property(4).setValue(rgb);
};
};
};//EXECUTION
app.beginUndoGroup("Change Fills to G-Fills");
recurseShape(Selection, myGroupHandler_changeFillToGFill, Color);
app.endUndoGroup();}
-
Joshua Faget
September 22, 2016 at 9:12 pmDisregard my previous comment… I overly complicated it for no reason.
alert('Thanks Xavier') // :D -
Juanluis Vich
November 5, 2019 at 2:58 amHi!
love this snippet!
Is there a way to get these colors from the shape layer and add a color control for every found color?
That would be amazing!
Thanks! -
Santi Agustí
November 7, 2019 at 5:09 pmGive this a try , this works for me (sorry to make such a mess on the Xavier’s awesome code! I surely added a lot of redundant and messy lines out there, but it works for now)
the extra code I added looks for any stroke color in the layer and adds a color control and links the stroke color to that color control
And the name given to the color control effect is the name of the group the stroke belongs to (only the 1st level). If the stroke is outside a group, it has the name of the stroke (usually Stroke 1).
Ah, it works on all selected shape layers, so you can select a bunch of them.
function recurseShape(g, groupHandler){
// g shape layer or group inside a shape ( should have a content property )
// groupHandler : what do do with each groupif (!g.content) return; // doesnt apply
groupHandler(g);
if (!g.content.property("ADBE Vector Group")) return;
for (var n=1; n<=g.content.numProperties; n++){
if (g.content.property(n).matchName==="ADBE Vector Group") recurseShape(g.content.property(n), groupHandler);
};
};function myHandler1(g){
// g shape layer or group inside a shape ( should have a content property )if (!g.content || !g.content.property("ADBE Vector Graphic - Stroke")) return;
for (var n=1; n<=g.content.numProperties; n++){
if (g.content.property(n).matchName==="ADBE Vector Graphic - Stroke"){
cu=g.content.property(n).property("Color")
strokename=g.content.property(n).name
///
cc = myLayers[i].Effects.addProperty("ADBE Color Control");
cc.property("Color").setValue(cu.value);
cc.name =strokename;if (g.matchName==="ADBE Vector Group"){
krupname=g.name;
cc.name = krupname + " " + strokename
}
cu.expression="effect(\""+cc.name+"\")(\"Color\")";
//};
};
};app.beginUndoGroup("Collect Colors");
var comp = app.project.activeItem;
var myLayers = comp.selectedLayers;for (var i = 0; i < myLayers.length; i++){
myLayers[i].selected = false;
recurseShape(myLayers[i], myHandler1);
}app.endUndoGroup();
Now, if any real coder here could give a clean, and more important, could add a function to prevent the code adding the same color control over and over each time you execute the script (in case you add a new group, or stroke, or have updated anyhing inside the shape layer and just want to update the color controls or add the new ones and keep the old ones without adding them again and again), that will be also more than welcome 🙂
Reply to this Discussion! Login or Sign Up