Copy link to clipboard
Copied
In short, I'm trying to see if there is a way in expressions to find the next layer up the stack that shares a parent. The caveat is that the only thing I can reliably know is that those layers (and only those layers) will share parent. They might not share any part of their name and they might not be next to each other in the layer stack.
I sort of figure it would be something like:
"Look one layer up. Does that layer have the same parent? Yes - look at property x. No - go to next layer up. Does that layer have the same parent..." (and so on). I'm not sure how one would code "keep looking till you find something or reach the top" but I think there has to be a way to do this.
For context: I'm making a template of sorts for the other motion graphic designers on my team. The template is several boxes that can have their Y-size animated on or off based on two sliders of their parented layer: one for animation percentage and one for time offset. The offset is based on an artificial 'index' of sorts (just a slider that looks one layer up and adds 1 to that layer's 'index' slider). I'm trying to make the template as "user error" proof as possible, so I'm anticipating that the box layers will likely be renamed and moved around in the layer stack. As such, I'd like the expression to not just look one layer up, but to find the next layer up in the stack that has the same parent (since only the box layers would be parented to the control layer).
After playing around some more, I found the following to work (in case anyone else should need something like this as well). It may not be the simplest solution, but it works - which certainly counts for something:
let i = (index > 1) ? index - 1 : index;
while (i > 1) {
if(thisComp.layer(i).hasParent && (parent == thisComp.layer(i).parent)) { break; }
i--;
}
(thisComp.layer(i).hasParent && (thisComp.layer(i).parent == thisLayer.parent)) ? thisComp.layer(i).effect("Box Scale")("Index")+1 : 1;
Let
Copy link to clipboard
Copied
Perhaps something related to a while loop?
let i = index - 1;
while (i > 0) {
if(parent == thisComp.layer(i).parent) { break; }
i--;
}
But I'm unsure how exactly to apply that to what I need. I need it to say "IF there is a layer like that, add 1 to its 'index' value. If not, set value to 1."
Copy link to clipboard
Copied
I'm not sure if this is what you mean, but maybe like this:
val = 1;
for (i = index-1; i > 0; i--){
if (thisComp.layer(i).hasParent && (thisComp.layer(i).parent == parent)){
val = i+1;
break;
}
}
val
Copy link to clipboard
Copied
Thank you, Dan! I have another question which seems like it should have a similar solution, but I can make it a separate post if need be:
In short, I have several shape layers parented to a NULL object (“Box CTRL”). The NULL has several expression controls attached to it to drive the animation of the child layers. One of the controllers is a slider to set the number of layers that it controls (number of children). Currently that number has to be set manually, but it seems like there should be a way to calculate that automatically. There are a few caveats to this, which is why I’m a little stumped:
I know it’s possible to count the number of layers, but I can’t figure out how to count the number of layers whose parent is ‘thisLayer’
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I think this will work:
n = 0;
for (i = 1; i <= thisComp.numLayers; i++){
if (i == index) continue;
if (thisComp.layer(i).hasParent && thisComp.layer(i).parent.index == index)n++;
}
n
Copy link to clipboard
Copied
That worked perfectly! Thank you!
Copy link to clipboard
Copied
After playing around some more, I found the following to work (in case anyone else should need something like this as well). It may not be the simplest solution, but it works - which certainly counts for something:
let i = (index > 1) ? index - 1 : index;
while (i > 1) {
if(thisComp.layer(i).hasParent && (parent == thisComp.layer(i).parent)) { break; }
i--;
}
(thisComp.layer(i).hasParent && (thisComp.layer(i).parent == thisLayer.parent)) ? thisComp.layer(i).effect("Box Scale")("Index")+1 : 1;
Let me know if you know of an easier way!