Skip to content

Instantly share code, notes, and snippets.

@jessefreeman
Created March 15, 2011 01:15
Show Gist options
  • Save jessefreeman/870172 to your computer and use it in GitHub Desktop.
Save jessefreeman/870172 to your computer and use it in GitHub Desktop.
This is a PS script to make Sprite Sheets out of layers. Modified from http://www.garagegames.com/community/blogs/view/11527
// 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);
}
}
@theinteresthunter
Copy link

Nice work. Thank you, JesseFreeman.

@spajus
Copy link

spajus commented Jan 7, 2019

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);
                }
            }
        }
    }
} 

@szegheo
Copy link

szegheo commented Apr 29, 2021

Thank you @jessefreeman and @jsonacob (I was needed a vertical solution) !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment