Page 1 of 1

Remove All Expressions from Selected Layers

Posted: Wed Apr 25, 2018 1:56 am
by CoonKunsen
Hi,

I found a script online which removes all Expressions from all layers within a composition. I tried to change it up so it only removes all expressions from selected layers and it works, but I always get an Alert after I run it that says "Unable to execute script at line 14. undefined is not an object". I was able to get rid of this error but than it only works on the last selected layer.

Also I don't really know what the recurse_children function does. Maybe someone knows a better way to do this?

Thanks in advance
Christian

app.beginUndoGroup("Remove all Expressions");
{
    var comp = app.project.activeItem;
    var layerNumber = comp.selectedLayers.length;
    var chosenLayers = comp.selectedLayers;
    
    if (layerNumber >= 1){

       for (var i = 0; i <= layerNumber; i++)
       {
          var layerNames = chosenLayers[i].index;
          recurse_children(comp.layers[layerNames]);
       }
    }    
    else{    
       alert('no layer selected');
    }

    function recurse_children(propParent)
    {
       if (propParent != null)
       {
          var prop;
          
          for (var i=1; i<=propParent.numProperties; i++)
          {
             prop = propParent.property(i);
             switch (prop.propertyType)
             {
                case PropertyType.PROPERTY:
                   // do action
                    if (prop.canSetExpression && prop.expression) prop.expression = '';
                   break;
                case PropertyType.INDEXED_GROUP:
                   recurse_children(prop);
                   break;
                case PropertyType.NAMED_GROUP:
                   recurse_children(prop);
                   break;
                default:
                   break;
             }
          }
       }
    }
    }
app.endUndoGroup();

Re: Remove All Expressions from Selected Layers

Posted: Wed Apr 25, 2018 2:33 am
by wysee
Hi,
your loop is wrong. Selected Layers being an Array starting at index 0,  your loop should be ( i < layerNumber NOT <= ) like so:
 if (layerNumber >= 1){

       for (var i = 0; i < layerNumber; i++)
       {
          var layerNames = chosenLayers[i].index;
          recurse_children(comp.layers[layerNames]);
       }
    }    
    else{    
       alert('no layer selected');
    }

Re: Remove All Expressions from Selected Layers

Posted: Wed Apr 25, 2018 4:14 am
by CoonKunsen
wysee wrote:
Hi,
your loop is wrong. Selected Layers being an Array starting at index 0,  your loop should be ( i < layerNumber NOT <= ) like so:

Awesome thanks :)