// ISO_RenderQueueSplitIntoSeperateProjects.jsx
// Artist: https://www.jasonfletcher.info/
// Code generated by ChatGPT-5.5
// Date: June 7, 2026
// Description: This After Effects script takes all selected comps in the Project panel, splits them into fixed-size batches (N per batch), adds each batch to the Render Queue, and saves a separate After Effects project file for each batch.
// Why: This is useful because the AE render engine can gradually slow down during long uninterrupted renders. My theory is that heavy RAM pressure, especially when running multiple command-line render instances and consistently maxing out available memory, may lead to inefficient memory behavior over time. The hardware load balancer in the AE render engine is quite good but sometimes a bit shaky when running x4 instances concurrently. So by reloading the whole AE render engine every so often, it stays refreshed and maintains consistent performance and avoid slow downs.
// If you’re rendering somewhere over 600+ comps, then you’ll run into a known bug with After Effects 2026 (and prior versions) where if you try to add too many comps into the render queue then the GUI will overflow and glitch out. This is because the current UI framework only supports a panel being 30,000 pixels wide or long. I've reported this bug to Adobe. - https://community.adobe.com/bug-reports-528/too-many-comps-will-glitch-gui-1554202

(function () {

    app.beginUndoGroup("Create Render Queue Batches");

    // Ask user for batch size
    var batchSizeInput = prompt(
        "Enter batch size:",
        "10"
    );

    if (batchSizeInput === null) {
        app.endUndoGroup();
        return;
    }

    var BATCH_SIZE = parseInt(batchSizeInput, 10);

    if (isNaN(BATCH_SIZE) || BATCH_SIZE < 1) {
        alert("Please enter a valid batch size greater than 0.");
        app.endUndoGroup();
        return;
    }

    // Gather selected comps
    var comps = [];
    var sel = app.project.selection;

    for (var i = 0; i < sel.length; i++) {
        if (sel[i] instanceof CompItem) {
            comps.push(sel[i]);
        }
    }

    if (comps.length === 0) {
        alert("No comps selected.");
        app.endUndoGroup();
        return;
    }

    var outputFolder = Folder.selectDialog(
        "Select folder for batch AEP files"
    );

    if (!outputFolder) {
        app.endUndoGroup();
        return;
    }

    var originalFile = app.project.file;

    var baseName = originalFile
        ? originalFile.displayName.replace(/\.aep$/i, "")
        : "BatchProject";

    var batchNum = 1;

    for (var start = 0; start < comps.length; start += BATCH_SIZE) {

        // Clear previous batch from render queue
        while (app.project.renderQueue.numItems > 0) {
            app.project.renderQueue.item(1).remove();
        }

        // Add current batch
        for (
            var j = start;
            j < Math.min(start + BATCH_SIZE, comps.length);
            j++
        ) {
            app.project.renderQueue.items.add(comps[j]);
        }

        var batchName =
            baseName +
            "_Batch_" +
            ("000" + batchNum).slice(-3) +
            ".aep";

        var saveFile = new File(
            outputFolder.fsName + "/" + batchName
        );

        app.project.save(saveFile);

        // If another batch remains, clear the queue now.
        // If this is the final batch, leave it intact.
        var nextStart = start + BATCH_SIZE;

        if (nextStart < comps.length) {
            while (app.project.renderQueue.numItems > 0) {
                app.project.renderQueue.item(1).remove();
            }
        }

        batchNum++;
    }

    alert(
        (batchNum - 1) +
        " .AEP project files exported\n\n" +
        BATCH_SIZE + " comps per render queue"
    );

    app.endUndoGroup();

})();