-
-
Save jessefreeman/870172 to your computer and use it in GitHub Desktop.
// Put this file in Program Files\Adobe\Photoshop\Presets\Scripts\ | |
// In PhotoShop menu File > Automate > Scripts: layersToSprite.js | |
// Arrange layers into a sprite sheet. | |
if (documents.length > 0) | |
{ | |
// -------------------------- | |
docRef = activeDocument; | |
var activeLayer = docRef.activeLayer; | |
numLayers = docRef.artLayers.length; | |
var cols = docRef.width; | |
var spriteX = docRef.width; | |
// put things in order | |
app.preferences.rulerUnits = Units.PIXELS; | |
// resize the canvas | |
newX = numLayers * spriteX; | |
docRef.resizeCanvas( newX, docRef.height, AnchorPosition.TOPLEFT ); | |
// move the layers around | |
for (i=0; i < numLayers; i++) | |
{ | |
docRef.artLayers[i].visible = 1; | |
var movX = spriteX*i; | |
docRef.artLayers[i].translate(movX, 0); | |
} | |
} |
Looks great! Shoot, it seems that it doesn't work with PSD CS6. It resizes the canvas fine, but then gets a transform-error.
Thanks! It worked for me in CS6 despite the transform error.
if i want to make the sprite sheet go from top to bottom as oppose to left to right, what should i adjust in the script?
@Jaytt What'd you change with your remake?
//I made a little modification to make it generate image from top to bottom
`if (documents.length > 0)
{
// --------------------------
docRef = activeDocument;
var activeLayer = docRef.activeLayer;
numLayers = docRef.artLayers.length;
var rows = docRef.height;
var spriteY = docRef.height;
// put things in order
app.preferences.rulerUnits = Units.PIXELS;
// resize the canvas
newY = numLayers * spriteY;
docRef.resizeCanvas( docRef.width, newY, AnchorPosition.TOPLEFT );
// move the layers around
for (i=0; i < numLayers; i++)
{
docRef.artLayers[i].visible = 1;
var movY = spriteY*i;
docRef.artLayers[i].translate(0, movY);
}
} `
I made something similar here. https://github.com/JesseLeite/photoshop-sprite-sheet-from-coordinates
cool......
Nice work. Thank you, JesseFreeman.
An improved version which takes sprite count per row as input to break sheet into columns, counts empty spaces, is background aware and allows putting sprites in reverse order:
(latest version in this gist)
// Put this file in Program Files\Adobe\Photoshop\Presets\Scripts\
// In PhotoShop menu File > Automate > Scripts: Layers To Sprite Sheet.js
// Arrange layers into a sprite sheet.
if (documents.length > 0) {
var docRef = activeDocument;
var numLayers = docRef.artLayers.length;
var cols = docRef.width;
var spriteX = docRef.width;
var spriteY = docRef.height;
app.preferences.rulerUnits = Units.PIXELS;
var hasBackground = false;
var layerNumbers = [];
for (i = 0; i < numLayers; i++) {
if (docRef.artLayers[i].isBackgroundLayer) {
docRef.artLayers[i].visible = false;
hasBackground = true;
} else {
layerNumbers.push(i);
}
}
var numSprites = layerNumbers.length;
var rowSize = prompt("How many sprites per row?", "8", "Configure stylesheet");
var rowCount = Math.ceil(numSprites / rowSize);
var emptySpaces = (rowSize * rowCount) - numSprites;
while (rowSize && !confirm("Is " + rowSize + "x" + rowCount + " sheet OK? There will be " + emptySpaces + " empty spaces.")) {
rowSize = prompt("How many sprites per row?", "8", "Configure stylesheet");
rowCount = Math.ceil(numSprites / rowSize);
emptySpaces = (rowSize * rowCount) - numSprites;
}
if (rowSize) { // cannot return from function in photoshop :/
var reverse = confirm("Use reverse order? Photoshop creates new sprites on top, with reverse order first sprite will be the most bottom layer");
if (reverse) {
layerNumbers.reverse();
}
var newX = rowSize * spriteX;
var newY = rowCount * spriteY;
docRef.resizeCanvas(newX, newY, AnchorPosition.TOPLEFT);
var startOffset = hasBackground ? 2 : 1;
startOffset += emptySpaces;
for (i = 0; i < rowSize; i++) {
for (j = 0; j < rowCount; j++) {
var layer = rowSize * j + i;
if (layer >= numSprites) {
continue;
}
layer = layerNumbers[layer];
try {
if (docRef.artLayers[layer].isBackgroundLayer) {
docRef.artLayers[layer].visible = 0;
} else {
docRef.artLayers[layer].visible = 1;
var movX = spriteX * i;
var movY = spriteY * j
docRef.artLayers[layer].translate(movX, movY);
}
} catch (e) {
alert("Layer " + layer + " at row:col " + j + ":" + i + " failed: " + e.message);
}
}
}
}
}
Thank you @jessefreeman and @jsonacob (I was needed a vertical solution) !
Very cool. Thanks!