Exporting graph editor data as JSON from Adobe After Effects

Recently I've designed a splash screen animation for my Chroma game development framework in After Effects and wanted to export animation data directly to JSON, so I could procedurally replicate it without having to export the entire thing to a 10MB-large sprite sheet. My Google searches proved fruitless, so I set out to write my own script.

Catch, maybe it'll be of use to you.
Note: make sure the file writing permissions are set in your scripting settings.

function ExportGraphCurve() {
    var comp = app.project.activeItem;

    if (!comp || !(comp instanceof CompItem)) {
        alert("Select a composition.");
        return;
    }

    var duration = comp.workAreaDuration;

    var fps = 60;
    var timeStep = 1 / fps;
    var frameData = [];

    for (var i = 1; i <= comp.numLayers; i++) {
        var layer = comp.layer(i);
        frameData[i - 1] = [];

        var currentTime = 0;
        var rotation = layer.rotation;
        var opacity = layer.opacity;
        var scale = layer.scale;

        while (currentTime < duration) {
            frameData[i - 1].push({
                r: rotation.valueAtTime(currentTime, false),
                o: parseFloat((opacity.valueAtTime(currentTime, false) / 100).toFixed(4)),
                s: parseFloat((scale.valueAtTime(currentTime, false)[0] / 100).toFixed(4)) // we only care about uniform scales here
            })

            currentTime += timeStep;
        }
    }

    var jFile = new File("J:\\exported_animation.json");
    var content = JSON.stringify(frameData);

    jFile.encoding = "utf-8";
    jFile.open("w");
    jFile.write(content);
    jFile.close();
}

ExportGraphCurve();

vdd - February 13, 2021 • general