ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Script to show/hide some layers

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Herby Herve
Script to show/hide some layers
on Nov 23, 2016 at 8:10:25 am


I'm working on a big multilingual AE project.
So far, I've managed the different language thanks to expression: I've created a controller layer in my top composition with 3 checkboxes (one per language) to control layers opacity. For instance here's the expression I have on a typical English title:
if(comp("Final").layer("Language").effect("English")("Checkbox") == 1) 100 else 0

It works as expected but now this project becomes too big for this method. I'm not sure what's going on in the background but now the render time for the whole project goes beyond 5 hours. If I manually disable the layers using the eyeball button in the timeline (instead of turning opacity to 0 with the expressions), my render time falls down to 1 hour.

So I'm wondering if a script could do the job.
Basically, I could rename the layers that need to be shown or hidden by adding the language code at the end of their names (EN for English, FR for French...). So the script will have to scan all the layers in the project and disable/enable the layers based on the last 2 characters of their names.

First big question: does a script can do that?

I have basic knowledge of javascript but never did any scripting in AE.
The first main problem I have is to find a way to get the script scanning through all the layers in all comps.

Return to posts index

Xavier Gomez
Re: Script to show/hide some layers
on Nov 23, 2016 at 9:50:12 am

Yes it's possible, scripts are designed exactly for this kind of things.
You need to loop throw all project items, identify those that are compositions, then for each comp loop again over all layers, and do your stuff with each layer.
You can try this (on a test project...):

var LANG_KEYS = "_EN,_DE,_FR,_IT";
function handleLayer(layer, lang_identifier){
var str =;
if (LANG_KEYS.indexOf(str)<=0){
// layer is localized: switch on or off
layer.enabled = (str===lang_identifier);
// layer not localized: do nothing
function handleProject(lang_identifier){
var M = app.project.numItems, m, comp;
var N, n, layer;

for (m=1; m<=M; m++){
comp = app.project.item(m);
if (comp.typeName!=="Composition") continue;
N = comp.numLayers;
for (n=1; n<=N; n++){
layer = comp.layer(n);
handleLayer(layer, lang_identifier);
// UI
var win = new Window("palette", "script name");
win.alignChildren = ['fill', 'top'];
win.spacing = 5;
var keyMENU = win.add("dropdownlist", undefined, LANG_KEYS.split(","));
var runBTN = win.add("button", undefined, "go");
keyMENU.selection = 0;
runBTN.onClick = function(){handleProject(keyMENU.selection.text);};


Return to posts index

Herby Herve
Re: Script to show/hide some layers
on Nov 23, 2016 at 11:20:50 am

Thanks Xavier. This helps a lot.
In the meantime, I've found this page where I found a lot of help as well:
The last script on the page (enable 3d switches for an entire hierarchy) is quite similar to what I want to do.

More than enough to start this script. I'll post news here...

Return to posts index

Herby Herve
Re: Script to show/hide some layers
on Nov 24, 2016 at 12:10:07 pm

Got it!!
I paste the script below.
I'm pretty sure that this script can be improved but it already does the job...

function hideLayer (layer, selectedLanguage){
// Hide/Show layers according to the last 2 digits of layer's name
if (layer.hasVideo && selectedLanguage == "All" || == selectedLanguage){layer.enabled = true};
else {layer.enabled = false};
if (layer.hasAudio && selectedLanguage == "All" || == selectedLanguage){layer.audioEnabled = true};
else {layer.audioEnabled = false};
// For layers used both in EN & FR version
if ( == "EN/FR" && selectedLanguage == "EN" && layer.hasVideo){layer.enabled = true};
if ( == "EN/FR" && selectedLanguage == "EN" && layer.hasAudio){layer.audioEnabled = true};
if ( == "EN/FR" && selectedLanguage == "CH" && layer.hasVideo){layer.enabled = false};
if ( == "EN/FR" && selectedLanguage == "CH" && layer.hasAudio){layer.audioEnabled = false};
// Scan all layers
function goThroughLayers (Comp, selectedLanguage){
for (var p = 1; p &lt;= Comp.numLayers; p++){
// Check if layer needs to be modified
if(Comp.layer(p).name.slice(-2) == "EN" || Comp.layer(p).name.slice(-2) == "FR" || Comp.layer(p).name.slice(-2) == "CH"){
hideLayer(Comp.layer(p), selectedLanguage);

var w =new Window ("dialog","Select language");
var radio_group = w.add ("panel");
radio_group.alignChildren ="center";
radio_group.add ("radiobutton", undefined, "EN");
radio_group.add ("radiobutton", undefined, "FR");
radio_group.add ("radiobutton", undefined, "CH");
radio_group.add ("radiobutton", undefined, "All");
var button_group = w.add ("group");
button_group.alignChildren ="center";
button_group.add ("button", undefined, "Cancel");
button_group.add ("button", undefined, "OK");

// set dialog defaults
radio_group.children[0].value =true;

if( ()== 1){
// Get selected language
for(var x = 0; x &lt; radio_group.children.length; x++){
if(radio_group.children[x].value ==true){var selectedLanguage = (radio_group.children[x].text)};

app.beginUndoGroup("Language selection");

// Search for all Compositions in the project
for (var i = 1; i &lt;= app.project.numItems; i++) {
if (app.project.item(i) instanceof CompItem) {
var Comp = app.project.item(i);
goThroughLayers(Comp, selectedLanguage);

Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 All Rights Reserved