Copy link to clipboard
Copied
Hey,
I can't find how to check if a layer has a certain proprety from this list
In my case, I want to check if the selected layer contains a rectangle, here's my code:
thisComp = app.project.activeItem;
curLayer = thisComp.selectedLayers[0];
iconbutton1.onClick = function() {
if (curLayer instanceof ShapeLayer) { //check if it's a shape layer
if () { // check if it contains a rectangle
alert("this layer has rectangle");
} else {
alert("this layer doesn't has rectangle");
}
} else {
alert("Please select a shape layer");
}
}
Thanks in advance,
RedyMotion
There are probably a lot of ways you could accomplish this, and it could get fairly complex to do a comprehensive check, but here I just for looped through the "Contents" property of the layer to see if it contains a property called "Rectangle 1" which is the default name when a new rectangle layer is created.
var thisComp = app.project.activeItem;
var curLayer = thisComp.selectedLayers[0];
var rectProp = false;
iconbutton1.onClick = function() {
if (curLayer instanceof ShapeLayer) {
...
Copy link to clipboard
Copied
You simply iterate through the properties and check against the type. Not sure what else you need to know here.
Mylenium
Copy link to clipboard
Copied
Thanks!
How do I do that tho ( like a line of code )? I tried with multiple ways but none worked.
RedyMotion
Copy link to clipboard
Copied
There are probably a lot of ways you could accomplish this, and it could get fairly complex to do a comprehensive check, but here I just for looped through the "Contents" property of the layer to see if it contains a property called "Rectangle 1" which is the default name when a new rectangle layer is created.
var thisComp = app.project.activeItem;
var curLayer = thisComp.selectedLayers[0];
var rectProp = false;
iconbutton1.onClick = function() {
if (curLayer instanceof ShapeLayer) {
for (i = 1; i <= curLayer.numProperties; i++) {
if (curLayer.property("Contents").property(i).name == "Rectangle 1") {
alert("This layer does have a rectangle!");
rectProp = true;
}
}
if (rectProp == false) {
alert("This layer doesn't have a rectangle.");
}
}
}
Copy link to clipboard
Copied
Thank you so much! Exactly what I needed
Copy link to clipboard
Copied
For some reason, this doesn't work...
If it finds a rectangle, it will alert: "This layer does have a rectangle!"
But if it doesn't find any rectangles, it doesn't alert anything even if I put an alert between the for loop and the " if (rectProp == false) " as if it wouldn't get out of the for loop...
thisComp = app.project.activeItem;
curLayer = thisComp.selectedLayers[0];
//curLayerIndex = thisComp.selectedLayers[0].index;
//curLayerAnchorPoint = [thisComp.width/2, thisComp.height/2]-curLayer.transform.position.value-curLayer.transform.anchorPoint.value
var rectProp = false;
iconbutton1.onClick = function() {
if (curLayer instanceof ShapeLayer) {
for (i = 1; i <= curLayer.numProperties; i++) {
if (curLayer.property("Contents").property(i).name == "Rectangle 1") {
alert("This layer does have a rectangle!");
rectProp = true;
}
}
if (rectProp == false) {
alert("This layer doesn't have a rectangle.");
}
} else {
alert("Please select a shape layer")
}
}
Copy link to clipboard
Copied
Found the issue, the curLayer and rectProp variables either need to be initialized at the beginning of your onClick or you need to update it with an onChange.
Right now those variables are being set when the script is loaded, not updated when something changes. (i.e. thisComp.selectedLayers.onChange = function () { curLayer = thisComp.selectedLayers[0];}
This is working for me now:
var proj = app.project;
var thisComp = proj.activeItem;
iconbutton1.onClick = function() {
var rectProp = false;
var curLayer = thisComp.selectedLayers[0];
contentsProp = null;
if (curLayer instanceof ShapeLayer) {
for (i = 1; i <= curLayer.numProperties; i++) {
if (curLayer.property("Contents").property(x).name == "Rectangle 1") {
alert("This layer does have a rectangle!");
rectProp = true;
break;
}
}
if (rectProp == false) {
alert("This layer doesn't have a rectangle.");
}
} else {
alert("Please select a shape layer")
}
}
Copy link to clipboard
Copied
ignore "contentsProp", was messing around trying to find the issue. Doesn't actually do anything
Copy link to clipboard
Copied
Ohhh thank you so much! Also found another issue, the for loop should be :
for (i = 1; i <= curLayer.property("Contents").numProperties; i++)
instead of:
for (i = 1; i <= curLayer.numProperties; i++)
And how can I make it work with the actual property from this list ( in this case, ADBE Vector Shape - Rect ) and not the name the user put in ( in this case, "Rectangle 1" )
I tried that but it didn't work:
if (curLayer.property("Contents").property(i) == "ADBE Vector Shape - Rect")
Again, thank you so much!
Copy link to clipboard
Copied
Haven't tested it, but I believe it's like this:
if (curLayer.property("Contents").property(i).matchName == "ADBE Vector Shape - Rect")
Copy link to clipboard
Copied
Tested that yesterday, it didn't work
I realised that the "ADBE Vector Shape - Rect" isn't the matchName of the folder called " Rectangle 1"
The matchName of the folder is "ADBE Vector Group" and the rectangle property inside is called "ADBE Vector Shape - Rect"
so the for loop should be running thru every "ADBE Vector Group" and check if there's a property called "ADBE Vector Shape - Rect"