Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Make me an After Effects ExtendScript that checks for every layer in the current precomp…

  1. User

    Make me an After Effects ExtendScript that checks for every layer in the current precomp if it's muted, if it is not muted, then it'll render every layer individually as .wav, with their respective render lenght being the project's total duration. Also each layer has their respective layer name translated into the .wav filename

  2. Assistant

    Here's an After Effects ExtendScript that will check each layer in the current composition, and if the layer is not muted (i.e., it's enabled), it will render that layer individually as a .wav file with the project's total duration. Each output file will be named after its corresponding layer.

    // Create an undo group to make the operation undoable
    app.beginUndoGroup("Render layers as WAV files");
    
    var comp = app.project.activeItem;
    
    if (comp == null || !(comp instanceof CompItem)) {
        alert("Please select a composition.");
    } else {
    
        var layers = comp.layers;
        var numLayers = layers.length;
    
        // Store the original solo status of each layer
        var originalSoloStatus = [];
        for (var i = 1; i <= numLayers; i++) {
            var layer = layers[i];
            originalSoloStatus[i-1] = layer.solo;
        }
    
        // Loop over each layer in the composition
        for (var i = 1; i <= numLayers; i++) {
            var layer = layers[i];
    
            // Check if the layer is not muted (enabled)
            if (layer.enabled) {
    
                // Solo only this layer
                for (var j = 1; j <= numLayers; j++) {
                    layers[j].solo = false;
                }
                layer.solo = true;
    
                // Add the composition to the render queue
                var rqItem = app.project.renderQueue.items.add(comp);
    
                // Set render settings to use the Length of Comp
                var rs = rqItem.renderSettings;
                rs.template = "Best Settings";
                rs = rqItem.renderSettings;
    
                if (rs.templates.length > 0) {
                    rs.applyTemplate("Best Settings");
                }
    
                // Ensure the Time Span is set to Length of Comp
                var rsSettings = rs.getSettings();
                rsSettings["Time Span Duration"] = comp.duration;
                rsSettings["Time Span Start"] = 0;
                rsSettings["Time Span"] = "Length of Comp";
                rs.setSettings(rsSettings);
    
                // Set the output module to output audio as .wav
                var om = rqItem.outputModule(1);
                om.applyTemplate("Lossless");
    
                // Modify the output module settings to output only audio in WAV format
                var omSettings = om.getSettings();
                omSettings["Output File Info"]["Format"] = "WAV";
                omSettings["Include Video"] = false;
                omSettings["Include Audio"] = true;
                // Audio Output Settings
                omSettings["Audio Output Format"] = "WAV";
                omSettings["Audio Sample Rate"] = 48000;
                omSettings["Audio Bit Depth"] = 16;
                omSettings["Audio Channels"] = "Stereo";
                om.setSettings(omSettings);
    
                // Sanitize the layer name to make it a valid filename
                var layerName = layer.name;
                // Replace invalid filename characters with underscores
                layerName = layerName.replace(/[\/\\\:\*\?\"\<\>\|]/g, "_");
    
                // Set the output filename (adjust the output path as needed)
                var outputFolder = app.project.file.parent; // The folder where the project is saved
                var outputPath = outputFolder.fsName + "/" + layerName + ".wav";
                om.file = new File(outputPath);
            }
        }
    
        // Restore the original solo status of all layers
        for (var i = 1; i <= numLayers; i++) {
            var layer = layers[i];
            layer.solo = originalSoloStatus[i-1];
        }
    
        alert("Render queue updated with individual layer audio files.");
    
    }
    
    app.endUndoGroup();
    

    Instructions to use the script:

    1. Open your After Effects project and select the composition you want to process.
    2. Go to File > Scripts > Run Script File... and select the script file you've saved.
    3. The script will process each layer, add them to the render queue, and set up the render settings.
    4. After the script completes, go to the Render Queue and start the render process.

    Notes:

    • The script checks if each layer is enabled (not muted). If the layer is enabled, it will be rendered individually.
    • It solos each layer one at a time to render only that layer.
    • The output .wav files will be saved in the same folder as your project file. You can modify the outputFolder variable in the script to change the output directory.
    • The script sanitizes layer names to ensure they are valid filenames by replacing invalid characters with underscores.

    Please make sure to save your project before running the script, and verify the render queue settings before starting the render process.