Skip to main content
Inspiring
April 29, 2024
Answered

Find the next layer in stack that has the same parent

  • April 29, 2024
  • 2 replies
  • 1269 views

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).

This topic has been closed for replies.
Correct answer Brynocerous

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!

2 replies

BrynocerousAuthorCorrect answer
Inspiring
April 29, 2024

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!

Inspiring
April 29, 2024

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."

Dan Ebberts
Community Expert
Community Expert
April 29, 2024

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
Inspiring
May 3, 2024

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:

  • The number of shape layers can vary; between 2 and 10
  • The shape layers have images parented to them (so right-clicking on the parent layer and choosing “Select Children” also selects the grandchildren layers)
  • The shape layers may be above or below the control
  • The shape layers won’t necessarily have consistent names

 

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’