Skip to main content
Participant
September 4, 2021
Answered

Expression to search for layer name and toggle visibility

  • September 4, 2021
  • 2 replies
  • 1819 views

Hello,

I have comp which has layers like this:

1_LayerBlue

2_LayerBlue

1_LayerRed

2_LayerRed

2_LayerYellow

 

The number is basically id of which group layer belongs.

I would like to create a control on opacity:

if layer name has 2

100

else 0

 

And then I would create dropdown menu, what controls which layers are wanted.

But what I'm stuck at is how to make it search for only part of the name.

 

I'm not an expert on writing these, so help is really appreciated. Would really like learn more 🙂

This topic has been closed for replies.
Correct answer Mathias Moehl

The solution of @Paul Tuersley is probably what you want.
In your example I can see that you have the number always at the very beginning of the layer name. If you only want to look at numbers at the beginning, you can replace the "> -1" check by "==0". This code only returns 100, if the "2" occurs at the very beginning of the layer name (and I also search for "2_" instead of "2", such that a "21_Layer..." is not interpreted as a layer of group 2):

if (thisLayer.name.indexOf("2_") == 0) {100} else {0}

2 replies

Mathias Moehl
Community Expert
Mathias MoehlCommunity ExpertCorrect answer
Community Expert
September 4, 2021

The solution of @Paul Tuersley is probably what you want.
In your example I can see that you have the number always at the very beginning of the layer name. If you only want to look at numbers at the beginning, you can replace the "> -1" check by "==0". This code only returns 100, if the "2" occurs at the very beginning of the layer name (and I also search for "2_" instead of "2", such that a "21_Layer..." is not interpreted as a layer of group 2):

if (thisLayer.name.indexOf("2_") == 0) {100} else {0}

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Participant
September 6, 2021

Thank you for both! Especially for explaining the answer, it really helped me to learn more. 

Inspiring
September 4, 2021

Something like this maybe:

if (thisLayer.name.indexOf("2") > -1) {100} else {0}

 

.indexOf() will return the first character position if the search term is found in the string, or -1 if not found, so this is saying  'if the result of a search for 2 is greater than -1'.