Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PSSpriteExporter : Export to .gif and a collection of .png files a photoshop animation, with layer naming.
/*
______________________________________________________________________________
How to Use:
1) Place this .jsx file into
C:\Program Files\Adobe\Adobe Photoshop CS4\Presets\Scripts\
2) Restart Photoshop if it was already open
3) Run the script: File > Scripts > SpriteExporter
______________________________________________________________________________
Tested with CS5
*/// =======================================================
// Assign the active document to currentDoc
currentDoc = app.activeDocument;
// The save destination of the compelted sprite sheet
var pngDestination = "~/Desktop/";
// Saves the sprite sheet using the same name as the PSD
var fileName = currentDoc.name;
fileName = fileName.substr(0,fileName.length-4);
function selectAnimationFrame( frameIdx ){
var idslct = charIDToTypeID( "slct" );
var desc = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref3 = new ActionReference();
var idanimationFrameClass = stringIDToTypeID( "animationFrameClass" );
ref3.putIndex( idanimationFrameClass, frameIdx );
desc.putReference( idnull, ref3 );
try{
executeAction( idslct, desc, DialogModes.NO );
}catch( e ){
return false;
}
return true;
}
function exportPNGFrame( outputFile ){
var saveOptions = new ExportOptionsSaveForWeb();
saveOptions.format = SaveDocumentType.PNG;
saveOptions.PNG8 = false;
//currentDoc.exportDocument( File(pngDestination + fileName + ".png"),ExportType.SAVEFORWEB,saveOptions);
var rv = app.activeDocument.exportDocument( File( outputFile ),ExportType.SAVEFORWEB,saveOptions);
return rv;
}
function exportAsGIF( outputFile ){
var saveOptions = new ExportOptionsSaveForWeb();
saveOptions.format = SaveDocumentType.COMPUSERVEGIF;
//currentDoc.exportDocument( File(pngDestination + fileName + ".png"),ExportType.SAVEFORWEB,saveOptions);
var rv = app.activeDocument.exportDocument( File( outputFile ),ExportType.SAVEFORWEB,saveOptions);
return rv;
}
function countAnimationFrames( ){
var i = 1;
while( selectAnimationFrame( i ) ){
i = i + 1;
}
return i - 1;
}
function selectAllFrames(){
// Selects all frames
var idanimationSelectAll = stringIDToTypeID( "animationSelectAll" );
var desc = new ActionDescriptor();
executeAction( idanimationSelectAll, desc, DialogModes.NO );
}
function selectOutputDir(){
var outDir = "~/Desktop/";
fileDir = Folder( outDir ).selectDlg("Select output dir");
return fileDir.fullName;
}
function redisplayLayers( layers )
{
/* Restores the visibility of previously hidded layers */
for( var i = 0; i < layers.length; ++i ){
layers[ i ].visible = true;
}
}
function collectAllLayers( layerHolder, allLayers, onlyVisibles )
{
for( var i = 0; i < layerHolder.artLayers.length; ++i ){
layer = layerHolder.artLayers[i];
if( layer.visible || !onlyVisibles ){
allLayers.push( layer );
}
}
for( var j = 0; j < layerHolder.layerSets.length; ++j ){
collectAllLayers( layerHolder.layerSets[j], allLayers, onlyVisibles );
}
return allLayers;
}
function hideLayersBeginningWith( prefix, layers, inverseComp )
{
var modifiedLayers = [];
for( var i = 0; i < layers.length; ++i ){
var l = layers[i];
if( l.visible ){
var layerPrefix = l.name.substr(0,prefix.length);
var shouldHide = ( prefix == layerPrefix );
if( inverseComp ){
shouldHide = !shouldHide;
}
if( shouldHide ){
modifiedLayers.push( l );
l.visible = false;
}
}
}
return modifiedLayers;
}
function hideBackgroundLayers( layers ){
return hideLayersBeginningWith( "bg__" , layers, false );
}
function hideExtraTextures( layers, extraPrefixes ){
var extraLayers = [];
for( var i = 0; i < extraPrefixes.length; ++i ){
var tmp = hideLayersBeginningWith (extraPrefixes[i], layers, false);
extraLayers = extraLayers.concat( tmp );
}
return extraLayers;
}
function findExtraTextures( layers ){
var extraTextures = [];
for( var j = 1; j < 10; ++j ){
var prefix = "ly" + j + "_";
for( var i = 0; i < layers.length; ++i ){
var nm = layers[i].name;
if( nm.length >= prefix.length && nm.substr(0, prefix.length ) == prefix ){
extraTextures.push( prefix );
break;
}
}
}
return extraTextures;
}
function exportFrame(outputFile){
var allLayers = [];
collectAllLayers( app.activeDocument , allLayers , true );
var bgLayers = hideBackgroundLayers ( allLayers );
/// check if we want sprites with multiple textures
var extraTextures = findExtraTextures( allLayers );
var additionalHide = hideExtraTextures( allLayers, extraTextures );
exportPNGFrame (outputFile + "_ly0_.png" );
if( additionalHide.length != 0 ){
redisplayLayers ( additionalHide );
for( var i = 0; i < extraTextures.length; ++i ){
var otherLayers = hideLayersBeginningWith (extraTextures[i], allLayers, true );
exportPNGFrame (outputFile + '_' + extraTextures[i] + ".png" );
redisplayLayers( otherLayers );
}
}
redisplayLayers( bgLayers );
}
function promptFilename(){
fileName = prompt("Enter base file name",fileName,"Base file name");
}
function main()
{
var outDir = selectOutputDir();
promptFilename();
var outputFile = outDir + "/" + fileName;
exportAsGIF( outputFile + ".gif" );
var i = 1;
while( selectAnimationFrame( i ) ){
exportFrame( outputFile + '_' + (i-1) );
i = i +1;
}
/*
var saveOptions = new ExportOptionsSaveForWeb();
saveOptions.format = SaveDocumentType.PNG;
saveOptions.PNG8 = false;
currentDoc.exportDocument( File(pngDestination + fileName + ".png"),ExportType.SAVEFORWEB,saveOptions);
*/
// Revert to the original document
var idRvrt = charIDToTypeID( "Rvrt" );
executeAction( idRvrt, undefined, DialogModes.NO );
alert("Export Complete");
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.