// Isosceles_RenderQueueSplitIntoSeperateProjects.jsx
// 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 batches of 10, adds each batch to the Render Queue, and saves a separate After Effects project file for each batch.
// Why: This is useful because the After Effects render engine can slow down after rendering continously for a long time. My theory is that the RAM is getting fragmented due to the fact that I'm frequently maxing out the available RAM. This happens especially since I'm often running multiple instances of the command line rendering via aerender. The hardware load balancer in After Effects is quite good, but sometimes a bit shaky when running x4 instances of aerender concurrrently. So by reloading the whole After Effects render engine, it stays refreshed and renders without slow downs. I often render multiple instances of the command line rendering via aerender because if I have thousands of comps that need render out, then I will experience a known bug in After Effects where the current UI framework only supports a panel being 30,000 pixels wide or long. Any panel which overflows that will glitch. I've reported this bug to Adobe - https://community.adobe.com/bug-reports-528/too-many-comps-will-glitch-gui-1554202

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

var BATCH_SIZE = 10;

// 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) {

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

    if (outputFolder) {

        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) {

            while (app.project.renderQueue.numItems > 0) {
                app.project.renderQueue.item(1).remove();
            }

            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);

            batchNum++;
        }

        while (app.project.renderQueue.numItems > 0) {
            app.project.renderQueue.item(1).remove();
        }

        alert((batchNum - 1) + " batch project(s) created.");

    }

} else {
    alert("No comps selected.");
}

app.endUndoGroup();