• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Find the next layer in stack that has the same parent

Explorer ,
Apr 29, 2024 Apr 29, 2024

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

TOPICS
Expressions

Views

225

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Apr 29, 2024 Apr 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

...

Votes

Translate

Translate
Explorer ,
Apr 29, 2024 Apr 29, 2024

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 29, 2024 Apr 29, 2024

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 03, 2024 May 03, 2024

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:

  • 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’

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

Here is a screenshot of the layer stack with 4 box groups (the shape layer and the image parented to it).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 03, 2024 May 03, 2024

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

LATEST

That worked perfectly! Thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 29, 2024 Apr 29, 2024

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines