Skip to main content
simon cederholm
Inspiring
March 13, 2020
Answered

Reference a layer with index

  • March 13, 2020
  • 1 reply
  • 22863 views

I have a working project that I'm revisiting. It is a simple box (shape layer) that adjusts its position and size to a text layer. I’m thinking about ways to make it more efficient. In the expression I reference the layers by name, which is a hassle if I want to duplicate the layers. What I want is the shape layer to reference the layer above it, i.e. the text layer. But I despite my efforts I can't seem to get it to work. If anyone could please take a look at this, it would be very much appreciated. 


  1. //TextLayer.transform.Anchor point
    s=thisLayer;
    a=s.sourceRectAtTime(time-s.inPoint);
  2. Height=s.sourceRectAtTime(outPoint).height;
    Width=s.sourceRectAtTime(time-s.inPoint).width;
  3. Top=s.sourceRectAtTime(outPoint).top;
    Left=s.sourceRectAtTime(time-s.inPoint).left;
  4. x=Left+Width/2;
    y=Top+Height/2;
    [x,y]


//ShapeLayer.transform.Anchor Point;
s=thisComp.layer("Text"); 
a=s.sourceRectAtTime(time-s.inPoint);

Height=s.sourceRectAtTime(outPoint).height;
Width=s.sourceRectAtTime(time-s.inPoint).width;

Top=s.sourceRectAtTime(outPoint).top;
Left=s.sourceRectAtTime(time-s.inPoint).left;

pCheck=s.sourceRectAtTime(time-s.inPoint).width;
if (pCheck>0){
p=50;
}else{
p=0;
}

x=Width/-2-Left;
y=Height/-2-Top;
[x,y]

 

//ShapeLayer.contents.Rectangle1.Rectangle Path1.size
s=thisComp.layer("Text");

Height=s.sourceRectAtTime(outPoint).height;
Width=s.sourceRectAtTime(time-s.inPoint).width;

pCheck=s.sourceRectAtTime(time-s.inPoint).width;
if (pCheck>0){
p=50;
}else{
p=0;
}

x=Width+p;
y=Height+p;
[x,y]

This topic has been closed for replies.
Correct answer Martin_Ritter

"index" gives you the index of the current layer. It's the number you see in the timeline in front of the layer. It starts by 1 and goes to as much as layers you have.

 

To refer to another layer, you can use "index + n" or "index - n" where n is the step you will take.

 

The layer above is index - 1, the layer beneath is index + 1.

 

So, basically, if the textlayer is layer 1 and the shape layer with the expression is layer 2, you change this:

 

s=thisComp.layer("Text");

 

to this:

 

s = thisComp.layer(index-1);

 

and you are done.

 

*Martin

1 reply

Martin_Ritter
Martin_RitterCorrect answer
Legend
March 13, 2020

"index" gives you the index of the current layer. It's the number you see in the timeline in front of the layer. It starts by 1 and goes to as much as layers you have.

 

To refer to another layer, you can use "index + n" or "index - n" where n is the step you will take.

 

The layer above is index - 1, the layer beneath is index + 1.

 

So, basically, if the textlayer is layer 1 and the shape layer with the expression is layer 2, you change this:

 

s=thisComp.layer("Text");

 

to this:

 

s = thisComp.layer(index-1);

 

and you are done.

 

*Martin

simon cederholm
Inspiring
March 13, 2020

That is so just what i tried to do...and failed. Must have been a typo because it works like a charm. Thanks for your help.