PDA

View Full Version : Publish Each Layer With Layer Name


sheepfilms
03-02-2009, 09:29 PM
Hello all

I'm trying to create a JSFL that publishes all normal layers, not guide or mask layers, and using the layer name as the exported swf. I've found a script that's most of the way there, exporting PNGs from the layers but using an arbitrary name rather than the layer name. Found here (http://summitprojectsflashblog.wordpress.com/2008/11/03/jsfl-for-exporting-layers-as-individual-pngs/) BTW

Could anyone help me adapt it?

var doc = fl.getDocumentDOM();
var lyrs = doc.getTimeline().layers;

var len = lyrs.length;
var lyr;
var originalType;
var i;
var pngName;
var saveName;

// Get a save location.
var saveDir = fl.browseForFolderURL("Choose a folder in which to save your exported PNGs:");
if (saveDir) {
// Get the Flash document's name, and strip off the final ".fla", and build a base name for the exported files.
fl.outputPanel.clear();
var docName = doc.name;
var extensionIndex = docName.lastIndexOf(".fla");
if (extensionIndex == docName.length - 4) {
docName = docName.substring(0, extensionIndex);
}
saveName = saveDir + "/" + docName;

// Acceptable values are "normal", "guide", "guided", "mask", "masked", and "folder".
var originalTypes = new Array();

// Grab all original layer types. Need to do this before setting to guide, because otherwise a mask layer turned
// to guide would would turn a masked layer into a normal layer.
for (i=0; i < len; i++) {
lyr = lyrs[i];
originalTypes[i] = lyr.layerType;
};

// Guide all layers.
for (i=0; i < len; i++) {
lyr = lyrs[i];
lyr.layerType = "guide";
};

// Save layers that were originally "normal" as PNGs, one-by-one.
for (i=0; i < len; i++) {
lyr = lyrs[i];
originalType = originalTypes[i]
if (originalType == "normal" || originalType == "guided") {
lyr.layerType = "normal";
exportPng(i);
lyr.layerType = "guide";
}

// In this case, we need to loop backwards until we find the mask layer, then re-mask this layer, before we export.
if (originalType == "masked") {
for (var j=i; j >= 0; j--) {
if (originalTypes[j] == "mask") {
var maskLyrSearch = lyrs[j];
maskLyrSearch.layerType = "mask";
lyr.layerType = "masked";
exportPng(i);
maskLyrSearch.layerType = "guide";
lyr.layerType = "guide";
break;
}
};
}

};

// Reset all layers to their original types.
for (i=0; i < len; i++) {
lyr = lyrs[i];
lyr.layerType = originalTypes[i];
};


}

function exportPng(i) {
pngName = saveName + i + ".png";
doc.exportPNG(pngName, true, true);
fl.trace("Exported: " + pngName);
}

I've changed the end exportPNG funtion to create SWFs


function exportPng(i) {
pngName = saveName + ".swf";
doc.exportSWF(pngName, true, true);
fl.trace("Exported: " + pngName);
}

It's just I can't get the layer name from this, even though I've tried using the doc.getTimeline().getLayerProperty('name'); command

Thanks!

sheepfilms
03-05-2009, 09:31 PM
I got in touch with the person who wrote the original script and was given the solution

Change line 45 from

exportPng(i);

to this:

exportPng(lyr.name);

I also altered the line

saveName = saveDir + "/" + docName;

To not include the docName. Job done!