Skip to main content
Inspiring
August 24, 2024
Question

experssion

  • August 24, 2024
  • 3 replies
  • 571 views

How can expression to a layer comp that when have image in comp opacity 100 and when doesn't have any image in comp opacity 0?

This topic has been closed for replies.

3 replies

Community Expert
August 24, 2024

You would have to reference every layer in every comp. The calculations could be recursive, slowing things down a lot.

 

The only other option would be to put all of the other layers in a nested comp (pre-comp) and then do a point sample of the alpha channel to look for transparency in a specific area of the comp. A simple if/else statement would generate an opacity switch.

 

I can only offer those suggestions without a screenshot of your comps' structure and a detailed explanation of the design goals.

 

 

Legend
August 24, 2024
At worst, you can check if the name of a layer in the composition contains an image extension, and in that case,
return 100 or 0.

 

var img = false;
for (var i = 1; i <= thisComp.numLayers; i++) {
  var layer = thisComp.layer(i);
  if (layer.name.match(/\.(jpg|jpeg|png|bmp|gif|tiff|tga|psd|eps|ai|dpx)$/i)) {
    img = true;
    break;
  }
}
img * 100;

 

 

 

 

Mylenium
Legend
August 24, 2024
quote
At worst, you can check if the name of a layer in the composition contains an image extension, and in that case,
return 100 or 0.

 

Won't work if you rename stuff, though, whether that's the layer or source item...

 

Mylenium

Legend
August 24, 2024

If he wants to rename his images, he can add the prefix IMG and modify the expression like this:

 

var img = false;
for (var i = 1; i <= thisComp.numLayers; i++) {
  var layer = thisComp.layer(i);
  if (layer.name.match(/^IMG/)) {
    img = true;
    break;
  }
}
img * 100;

 

If he knows that he will use an expression that checks if the layer name contains an image extension,

it would be foolish to rename his image layers and then be surprised that the expression doesn’t work.
It’s just a matter of organization.

 

Mylenium
Legend
August 24, 2024

You can't. Expressions cannot check whether there is content in a sub-comp or not. Only scripts can do that. Whwtever you want will require a different approach like always having a layer in the pre-comp and checking its visibility state with isActive() or similar.

 

Mylenium