After Effects 表达式(如果图层是 CompItem)

After Effects Expression (if layer is a CompItem)

我正在尝试对以下 After Effects 表达式的第 5 行进行轻微修改。第 5 行检查图层是否可见且处于活动状态,但我试图添加额外的检查以确保图层不应该是合成项目。 (在我的项目中,层是文本层或图像层,我认为图像层意味着合成项目)。不知何故,'instanceof' 确保图层不应该是合成项目的方法不起作用。请告知如何解决此错误,谢谢。

   txt = "";
   for (i = 1; i <= thisComp.numLayers; i++){
      if (i == index) continue;
         L = thisComp.layer(i);
           if ((L.hasVideo && L.active) && !(thisComp.layer(i) instanceof CompItem)){
          txt = i + " / " + thisComp.numLayers + " / " + L.text.sourceText.split(" ").length;
       break;
       }
    }
    txt

您混淆了表达式和 Extendscript。 compItem class 是一个 Extendscript class,我很确定它不适用于表达式。

我建议阅读文档:https://helpx.adobe.com/after-effects/user-guide.html?topic=/after-effects/morehelp/automation.ug.js

虽然 compItem 仅在 ExtendScript 中可用,但您实际上可以检查 {my_layer}.source 对象中可用的属性。

这是一个工作示例(AE CC2018、CC2019 和 CC2020):layer_is_comp.aep

表达式类似于:

function isComp (layer)
{
  try
  {
    /*
    - used for when the layer doesn't have a ['source'] key or layer.source doesn't have a ['numLayers'] key
    - ['numLayers'] is an object key available only for comp objects so it's ok to check against it
    - if ['numLayers'] is not found the expression will throw an error hence the try-catch
    */
    if (layer.source.numLayers) return true;
    else return false;
  }
  catch (e)
  {
    return false;
  }
}

try
{
  // prevent an error when no layer is selected
  isComp(effect("Layer Control")(1)) ? 'yes' : 'no';
}
catch (e)
{
  'please select a layer';
}

对于第二个问题,您可以通过验证图层是否具有 text.sourceText 属性.

来检查图层是否为 TextLayer

示例:

function isTextLayer (layer)
{
  try
  {
    /*
    - prevent an expression error if the ['text'] object property is not found
    */
    var dummyVar = layer.text.sourceText;

    return true;
  }
  catch (e)
  {
    return false;
  }
}