Created
April 7, 2011 04:42
-
-
Save zaphire/907054 to your computer and use it in GitHub Desktop.
Exports photoshop layers as individual sprites, by Tuba
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Maximum spriteSheet width -> FIXED*/ | |
var maxWidth = 256; | |
var filename = activeDocument.name.replace(".psd",""); | |
var file = new File(activeDocument.fullName.path+"/"+filename+".xml"); | |
var AD = activeDocument; | |
var output = new File(activeDocument.fullName.path+"/"+filename+".png"); | |
var pngOptions = new PNGSaveOptions(); | |
/*Global control variables*/ | |
var finalWidth = maxWidth; | |
var finalHeight = 0; | |
var nextWidth = 0; | |
var nextHeight = 0; | |
function getSpriteFromLayer(layer) | |
{ | |
if (layer.typename == "LayerSet") //Layer is a Folder | |
{ | |
for (var i=0; i < layer.artLayers.length; i++) | |
{ | |
layer.artLayers[i].visible = true; | |
getSpriteFromLayer(layer.artLayers[i]); | |
} | |
} | |
else | |
{ | |
var spriteWidth = layer.bounds[2].value - layer.bounds[0].value; | |
var spriteHeight = layer.bounds[3].value - layer.bounds[1].value; | |
if (nextWidth + spriteWidth > maxWidth) | |
{ //maximum width achieved, start a new row | |
nextWidth = 0; | |
nextHeight = finalHeight; | |
layer.translate(nextWidth - layer.bounds[0].value,nextHeight - layer.bounds[1].value); | |
} | |
else | |
{ | |
layer.translate(nextWidth - layer.bounds[0].value ,nextHeight - layer.bounds[1].value); | |
} | |
if (nextHeight + spriteHeight > finalHeight) | |
finalHeight = nextHeight + spriteHeight; | |
//write the sprite data | |
file.writeln("<sprite>"); | |
file.writeln("<name>"+layer.name+"</name>"); | |
file.writeln("<x>"+(layer.bounds[0].value)+"</x>"); | |
file.writeln("<y>"+(layer.bounds[1].value)+"</y>"); | |
file.writeln("<width>"+spriteWidth+"</width>"); | |
file.writeln("<height>"+spriteHeight+"</height>"); | |
file.writeln("</sprite>"); | |
nextWidth += spriteWidth + 1; | |
} | |
} | |
preferences.rulerUnits = Units.PIXELS; | |
pngOptions.formatOptions = FormatOptions.STANDARDBASELINE; | |
file.open("w"); | |
file.writeln("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); | |
file.writeln("<spritesheet>"); | |
file.writeln("<sheetfile>"+filename+".png</sheetfile>"); | |
for (var i=0; i < AD.layers.length; i++) | |
{ | |
AD.layers[i].visible = true; //Make sure that the layer is visible | |
getSpriteFromLayer(AD.layers[i]); | |
} | |
file.writeln("</spritesheet>"); | |
file.close(); | |
AD.resizeCanvas(finalWidth, finalHeight, AnchorPosition.TOPLEFT); | |
AD.saveAs(output ,pngOptions ,true, Extension.LOWERCASE); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment