Skip to main content
Known Participant
November 4, 2022
Question

Is there a way to return an index value from a precomp or parent comp?

  • November 4, 2022
  • 3 replies
  • 1087 views

I've brainstormed a way to make my project easier to customize, but I don't know if it's possible.


I want to have one comp with multiple images in it. I want to make an expression for each layer's opacity so that it knows what it's index is in the parent comp. 

 

If this comp is in the parent comp at index 1, all images are 0 opacity except image 1, which is 100 opacity.

Repeat for all image layers.


Thanks!

This topic has been closed for replies.

3 replies

Community Expert
November 4, 2022

Returning the index of a nested comp is easy. If the comp name is Red Comp 1, this will do it:

I = comp("Parent Comp").layer("Red Comp 1").index;

 Put "I" in a property like Ellipse/Size or Opacity expression like this:

// Ellipse Size
I = comp("Parent Comp").layer("Red Comp 1").index;
if (I == 1)
	v = 400;
else if (I == 2)
	v = 300;
else if (I == 3)
	v = 200;
else if (I == 4)
	v = 100;
else
	v = 50;
[v, v]

//Opacity
I = comp("Parent Comp").layer("Red Comp 1").index;
if (I == 1)
	20;
else if (I == 2)
	40;
else if (I == 3)
	60;
else if (I == 4)
	80;
else
	100;

And you can get this:

Is something like that what you are looking for?

NotSeanTAuthor
Known Participant
November 4, 2022

Thanks for the reply! It's very helpful to know that I can reference other comps and layers within my project.

I've hit a snag, however. I'm hoping you can help me find a solution.

Within "Red Comp 1", I'll have 4 imaged, each one a different layer, but occupying the same position within the comp.

In the "Parent Comp", I hope to duplicate "Red Comp 1" four times (resulting in Red Comps 1-4 - perhaps it is noteworthy that each of those duplicates has a number matching it's index within the parent comp)

i = thisLayer.index;

p = comp("Parent Comp").layer(i).index;

if (p == i) {

 100;

} else {

 0;

}

 

But it would seem everything I try ends up a version of "3 = 3" or something, so when I go back to the Parent Comp, each version of Red Comp has full opacity on the top image (It'll always meet the check)

The part I'm struggling with is:

Can I turn

comp("Parent Comp).layer("Red Comp 1").index;

into something like 

comp("Parent Comp).layer(whatever layer this precomp is sitting on).index;

 

Is the fact that I'm duplicating Red Comp a problem? Can each copy of Red Comp do something slightly different depending on what layer it's on?

Mylenium
Legend
November 4, 2022
quote

Is the fact that I'm duplicating Red Comp a problem? Can each copy of Red Comp do something slightly different depending on what layer it's on?

 

No, it can't. I explained the why and how in my original reply, which everyone chose to ignore. You can't avoid creating four independent comps in the project window and substituting their usage, but then of course the whole song and dance with the expressions makes even less sense since you could just design the comps accordingly outright.

 

Mylenium

Mylenium
Legend
November 4, 2022

Your idea makes no sense. Unless you duplicate the pre-comp a gazillion times in the project panel so it could function as a standalone unit, none of this would matter. The layers would simply turn off in every instance of the pre-comp, regardless. That's something you can't even avoid with essential graphics, as you're creating a causality loop. So whatever you have in mind would be best handled by tweaking the opacity of the actual duplicates in the parent comp, not the pre-comp.

 

Mylenium

Roland Kahlenberg
Legend
November 4, 2022

Actually, the Parent Comp is the originating comp - this should be the way to describe the parent-child comp hierarchy. It will be awkward if the originating comp is the child. This is a common error among the majority of users. 

On your specific issue, I suggest you describe how the other layers/images will be displayed so that a clear solution can be provided for you - you've only described for a single layer/image. It will also be good if you are able to provide a screen capture of both the Parent and Child comps. 


Very Advanced After Effects Training | Adaptive & Responsive Toolkits | Intelligent Design Assets (IDAs) | MoGraph Design System DEV