Skip to main content
sergiopop75
Inspiring
June 8, 2024
Answered

Export layers to PNG

  • June 8, 2024
  • 2 replies
  • 496 views

I have this code, which allows me to export each visible layer as a sequence of PNGs. But I'm trying to export only the first frame, and I can't find where to change it in the code. any help?

 

var doc = fl.getDocumentDOM();
var lyrs = doc.getTimeline().layers;
var len = lyrs.length;
var saveDir = fl.browseForFolderURL("Choose a folder in which to save your exported PNGs:");

if (saveDir) {
    // Crear carpeta con el nombre del archivo
    var docFolderName = doc.name;
    var exportDest = saveDir + "/" + docFolderName + "_PNG";
    FLfile.createFolder(exportDest);
    
    fl.outputPanel.clear();
    
    var originalTypes = new Array(len);

    // Guardar el tipo de capa original y cambiar todas las capas a guía
    for (var i = 0; i < len; i++) {
        var lyr = lyrs[i];
        originalTypes[i] = lyr.layerType;
        lyr.layerType = "guide";
    }

    var count = 0;

    // Establecer el fotograma actual al primer fotograma
    doc.getTimeline().currentFrame = 0;

    // Iterar sobre las capas, exportando solo las capas visibles del primer fotograma
    for (var i = 0; i < len; i++) {
        var lyr = lyrs[i];

        if (originalTypes[i] == "normal" && lyr.visible) {
            lyr.layerType = "normal";

            // Padding 0s
            var padding = count < 10 ? "0" + count : count;

            // Establecer el fotograma actual al primer fotograma nuevamente para asegurar
            doc.getTimeline().currentFrame = 0;

            // Exportar solo el primer fotograma
            doc.exportPNG(exportDest + "/" + padding + "_" + lyr.name + ".png", true);

            fl.trace("Exported: " + lyr.name + ".png");

            lyr.layerType = "guide";
            count++;
        }
    }

    // Restaurar los tipos de capa originales
    for (var i = 0; i < len; i++) {
        lyrs[i].layerType = originalTypes[i];
    }
}

 

Thanks! 

    This topic has been closed for replies.
    Correct answer Vladin M. Mitov

    Hi!

    According to the documentation of the function

    document.exportPNG([fileURI [, bCurrentPNGSettings [, bCurrentFrame]]])

    it takes three parameters, the latter two indicating:
    - whether to use the current export settings
    - whether to export only the current frame.


    Therefore, to export only the first frame, you need to modify the line:

    // Exportar solo el primer fotograma
    doc.exportPNG( exportDest + "/" + padding + "_" + lyr.name + ".png", true );

    and call the function with the last parameter set to true, like this:

    // Exportar solo el primer fotograma
    doc.exportPNG( exportDest + "/" + padding + "_" + lyr.name + ".png", true, true );



    2 replies

    sergiopop75
    Inspiring
    June 10, 2024

    It works! thanks for the help.

    ____2D vector animator since 2000 &amp; PhD
    Vladin M. Mitov
    Vladin M. MitovCorrect answer
    Inspiring
    June 9, 2024

    Hi!

    According to the documentation of the function

    document.exportPNG([fileURI [, bCurrentPNGSettings [, bCurrentFrame]]])

    it takes three parameters, the latter two indicating:
    - whether to use the current export settings
    - whether to export only the current frame.


    Therefore, to export only the first frame, you need to modify the line:

    // Exportar solo el primer fotograma
    doc.exportPNG( exportDest + "/" + padding + "_" + lyr.name + ".png", true );

    and call the function with the last parameter set to true, like this:

    // Exportar solo el primer fotograma
    doc.exportPNG( exportDest + "/" + padding + "_" + lyr.name + ".png", true, true );



    - Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation