PS动画序列精灵表脚本


搞游戏开发与影视特效的经常会遇到需要制作动画序列精灵表,在网上搜寻之后找到了这个ps脚本,方便快捷

微信图片_20221111212228


以下是脚本原作者原文


Photoshop: Sprite Sheet Generator Script

Last Updated - 1 January 0001

The Grimrock Model Toolkit is a mod-tool for the Legend of Grimrock. The GMT makes it easy to re-texture models in Legend of Grimrock Mods and a large number of the rextured and modder-created models on the Legend of Grimrock Mod Nexus were made using this tool.

This Photoshop Script has been put together to simplify the task of exporting a sprite sheet from a Photoshop file that contains Frame Animation data or a layer set containing 1 layer (or a sub-set) for each frame/sprite that you would like on the resulting sprite sheet.

Download

You can download the latest version of the Sprite Sheet Generator script below.

Sprite Sheet Generator - Version 0.6.0

Installation

If you simply want to test the script out, then there is no install necessary. Just extract the .jsx file from the .zip file above and you should just be able to double click the script and it will run in Photoshop as if you had installed it and selected the relevant action from the menu. If that fails for some reason, you can drag and drop the script file onto Photoshop to run it once.

If you like the script and want to install it, follow these steps.

  1. Extract the .jsx file from the zip file above.

  2. Copy and paste this file into your scripts presets folder:

  • OSX: /Applications/Adobe Photoshop /Presets/Scripts/
  • Windows: C:\Program Files\Adobe\Adobe Photoshop \Presets\Scripts\
  1. Close and restart Photoshop.

Once installed, you can run the script by selecting File -> Scripts -> Sprite Sheet Generator from the application’s menu.

Usage

Once installed, you can run this script by going to File -> Scripts -> Sprite Sheet Generator from the application menu. Whenever you launch the script you will be presented with the following dialog…

Sprite Sheet Generator Script Settings

This script opens a new document and copies all of your sprite data to that image. We try to leave your original document in the exact same state that we found it, but please do save your document before running this script just incase!

The settings serve the following purpose;

Atlas Size: You can fix the width and/or the height of the resulting sprite atlas. If you leave both set to ‘Auto’ then the smallest “power of two” texture size will be used. If only one setting is left to ‘Auto’ then the smallest POT value will be selected to fix the sprites. If you fix both, then your sprites might not fit - no warning will be issued!

Sprite Source: You can select one of the following options for generating your  sprite sheet;

  • “Frame Animation”: If you have setup your document as a Frame-based Animation then this will simply make each frame into a sprite and put them together on the resulting sprite sheet.
  • “Document Root Layers”: The script will go over every layer in the document root and make each layer a Sprite in the resulting Sprite sheet. If you have a background layer, this will be skipped. Note that only the visibility of the sprite layers will be toggled on/off during the process. If you have background layers, you must decide whether you want them on every sprite or not by tweaking their visibility.
  • “Specific Layer Set”: You can select a layer set from the document and have each layer from that set used as a sprite. Once again, the script will not just capture that layer, but the entire image (all layers) with that the Sprite Layers turned on/off. This gives you maximum flexibility as you can have effect layers above/below your frames that appear on every sprite if you desire.

Ignore Child Layer Sets: In “Document Root Layers” or “Specific Layer Set” mode you can choose whether you want the script to treat layer sets (folders) within the given set of layers as sprites. If this tick box is checked, then all child layer sets are ignored (although their visibility is not changed from it’s initial setting). If you uncheck this box, they are treated as artwork layers, meaning each sprite in your sprite sheet can actually be a complicated group of other layers if you need that.

Layer Set: In “Specific Layer Set” mode this is the set of layers to treat as your sprites.

Flatten Image: Checking this box will flatten the resulting image. Otherwise, each sprite will remain as a separate layer.

License

This script is released under a GPLv3 License. You’re free to use it for personal or commercial uses. Please just share any improvements that you make to the script with me and I’ll include them in future versions (and add your name to the credits of course!).


以下为脚本文件的解码(以防文件损失)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// This script will export a sprite sheet as a new document given either a frame animation or a layered document.
//
// Author: John Wordsworth <[email protected]>
// Version: 0.6.0
// Date: June 2014
// Homepage: http://www.johnwordsworth.com/
//
// This script is released under the GPLv3 Software License. You are free to use it for free or commercial purposes.
// If you make any modifications or improvements to this script, please do share them back with us.

// Allows double clicking and opening in Photoshop
#target photoshop

// debug level: 0-2 (0:disable, 1:break on error, 2:break at beginning)
//$.level = 1;
//debugger; // launch debugger on next line

// Constants
const kScriptTitle = "Sprite Sheet Generator 0.6.0";

const kUserOptionsKey = "userOptions";
const kUserOptionsDocNameKey = "documentName";
const kUserOptionsAtlasWidthKey = "atlasWidth";
const kUserOptionsAtlasHeightKey = "atlasHeight";
const kUserOptionsSourceKey = "frameSource";
const kUserOptionsFlattenKey = "flattenAtlas";
const kUserOptionsLayerSetKey = "layerSetIndex";
const kUserOptionsIgnoreChildSetsKey = "ignoreChildSets";

const kUserOptionsSourceFrameAnimation = 0;
const kUserOptionsSourceDocumentLayers = 1;
const kUserOptionsSourceLayerSet = 2;

// Script Result
var gScriptResult;

// Run the Script
try {
var result = main();

if ( result === false ) {
gScriptResult = 'cancel';
} else {
gScriptResult = 'ok';
}
}
catch( e ) {
if ( app.displayDialogs != DialogModes.NO )
{
alert(e, kScriptTitle);
}

gScriptResult = 'cancel';
}

gScriptResult;


// Main Method
function main()
{
entryPoint();

if ( app.documents.length == 0 ) {
throw "No open documents.";
}

// Preparation
var srcDoc = app.activeDocument;

// Get the user's input options
var userOptions = getUserOptions();

if ( userOptions == false ) {
return false;
}

// Create the output document
var destDoc = createDestDocWithOptions(userOptions, srcDoc);

// If no destination doc, we didn't make a sprite sheet
if ( destDoc === false ) {
throw "No destination document was created - invalid input options?";
}

// In the new document, we want to delete the white layer
app.activeDocument = destDoc;
destDoc.layers[destDoc.layers.length-1].remove();

// At this point the new document is a stack of frames...
layoutLayersAsSprites(userOptions, destDoc);

exitPoint();
return true;
}


function entryPoint()
{
prevRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;
}


function exitPoint()
{
app.preferences.rulerUnits = prevRulerUnits;
}


function toTypeID(stringID)
{
return app.stringIDToTypeID(stringID);
}


function putActionDescValue(desc, method, key, value)
{
if ( key == undefined || value == undefined ) {
return;
}

desc[method](toTypeID(key), value);
}


function getActionDescValue(desc, method, key, defaultValue)
{
if ( desc.hasKey(toTypeID(key)) ) {
return desc[method](toTypeID(key));
}

return defaultValue;
}


function storeLastUserOptions(userOptions)
{
var desc = new ActionDescriptor();

putActionDescValue(desc, "putString", kUserOptionsDocNameKey, userOptions[kUserOptionsDocNameKey]);
putActionDescValue(desc, "putInteger", kUserOptionsAtlasWidthKey, userOptions[kUserOptionsAtlasWidthKey]);
putActionDescValue(desc, "putInteger", kUserOptionsAtlasHeightKey, userOptions[kUserOptionsAtlasHeightKey]);
putActionDescValue(desc, "putInteger", kUserOptionsSourceKey, userOptions[kUserOptionsSourceKey]);
putActionDescValue(desc, "putBoolean", kUserOptionsFlattenKey, userOptions[kUserOptionsFlattenKey]);
putActionDescValue(desc, "putInteger", kUserOptionsLayerSetKey, userOptions[kUserOptionsLayerSetKey]);
putActionDescValue(desc, "putBoolean", kUserOptionsIgnoreChildSetsKey, userOptions[kUserOptionsIgnoreChildSetsKey]);

app.putCustomOptions( kUserOptionsKey, desc, true );
}


function retrieveLastUserOptions()
{
try {
var desc = app.getCustomOptions(kUserOptionsKey);
} catch( e ) { return { }; }

var result = { };

result[kUserOptionsDocNameKey ] = getActionDescValue(desc, "getString", kUserOptionsDocNameKey, "");
result[kUserOptionsAtlasWidthKey] = getActionDescValue(desc, "getInteger", kUserOptionsAtlasWidthKey, 0);
result[kUserOptionsAtlasHeightKey] = getActionDescValue(desc, "getInteger", kUserOptionsAtlasHeightKey, 0);
result[kUserOptionsSourceKey] = getActionDescValue(desc, "getInteger", kUserOptionsSourceKey, 0);
result[kUserOptionsFlattenKey] = getActionDescValue(desc, "getBoolean", kUserOptionsFlattenKey, false);
result[kUserOptionsLayerSetKey] = getActionDescValue(desc, "getInteger", kUserOptionsLayerSetKey, 0);
result[kUserOptionsIgnoreChildSetsKey] = getActionDescValue(desc, "getBoolean", kUserOptionsIgnoreChildSetsKey, true);

return result;
}


function indexOf(array, obj)
{
for( var k = 0; k < array.length; ++k )
{
if ( array[k] == obj ) {
return k;
}
}

return -1;
}


function getUserOptions()
{
// Note: When running from ExtendScript editor, editing the script soetimes destroys all previous options!
var lastUserOptions = retrieveLastUserOptions();

// If dialogs are turned off, we just return the last / default options
//if ( app.playbackDisplayDialogs != DialogModes.ALL ) {
// return lastUserOptions;
//}

var sizeItems = ["Auto", 32, 64, 128, 256, 512, 1024, 2048, 4096];
var sourceItems = ["Frame Animation", "Document Root Layers", "Specific Layer Set"];
var layerSetItems = [];

for( k = 0; k < app.activeDocument.layerSets.length; ++k ) {
var layerSet = app.activeDocument.layerSets[k];
layerSetItems[layerSetItems.length] = layerSet.name;
}

if ( layerSetItems.length == 0 ) { layerSetItems[0] = "No Layer Sets Available"; }

var dlg = new Window('dialog', kScriptTitle);

dlg.atlasSizeTitleGrp = dlg.add('group', undefined);
dlg.atlasSizeTitleGrp.alignment = 'left';
dlg.atlasSizeTitleGrp.label01 = dlg.atlasSizeTitleGrp.add('StaticText', undefined, "Atlas Size (Pixels)");

dlg.atlasSizeGrp = dlg.add('group', undefined, );
dlg.atlasSizeGrp.orientation = "row";
dlg.atlasSizeGrp.alignment = 'left';
dlg.atlasSizeGrp.dropDownWidth = dlg.atlasSizeGrp.add('DropDownList', undefined, sizeItems);
dlg.atlasSizeGrp.dropDownWidth.text = "Width: ";
dlg.atlasSizeGrp.dropDownWidth.selection = 0;
dlg.atlasSizeGrp.dropDownHeight = dlg.atlasSizeGrp.add('DropDownList', undefined, sizeItems);
dlg.atlasSizeGrp.dropDownHeight.text = "Height: ";
dlg.atlasSizeGrp.dropDownHeight.selection = 0;

dlg.sourceTitleGrp = dlg.add('group', undefined);
dlg.sourceTitleGrp.alignment = 'left';
dlg.sourceTitleGrp.label01 = dlg.sourceTitleGrp.add('StaticText', undefined, "Sprite Source");

dlg.sourceGrp = dlg.add('group', undefined);
dlg.sourceGrp.orientation = "column";
dlg.sourceGrp.alignment = "left";
dlg.sourceGrp.dropDownSource = dlg.sourceGrp.add('DropDownList', undefined, sourceItems);
dlg.sourceGrp.dropDownSource.text = "Source: ";
dlg.sourceGrp.dropDownSource.selection = 0;
dlg.sourceGrp.dropDownSource.onChange = function() {
userOptionsDlgUpdateLayerSetChildControls(dlg);
};

dlg.sourceGrp.dropDownLayerSet = dlg.sourceGrp.add('DropDownList', undefined, layerSetItems);
dlg.sourceGrp.dropDownLayerSet.text = "Layer Set: ";
dlg.sourceGrp.dropDownLayerSet.selection = 0;

dlg.sourceGrp.ignoreChildSetsCheck = dlg.sourceGrp.add('CheckBox', undefined, "Ignore Child Layer Sets");
dlg.sourceGrp.ignoreChildSetsCheck.value = true;

dlg.otherTitleGrp = dlg.add('group', undefined);
dlg.otherTitleGrp.alignment = 'left';
dlg.otherTitleGrp.label01 = dlg.otherTitleGrp.add('StaticText', undefined, "Other Options");

dlg.flattenGrp = dlg.add('group');
dlg.flattenGrp.orientation = 'row';
dlg.flattenGrp.alignment = 'left';
dlg.flattenGrp.flattenCheck = dlg.flattenGrp.add('CheckBox', undefined, "Flatten Image?");

dlg.buttonGrp = dlg.add('group');
dlg.buttonGrp.okButton = dlg.buttonGrp.add('button', undefined, 'OK');
dlg.buttonGrp.cancelButton = dlg.buttonGrp.add('button', undefined, 'Cancel');

// Before presenting, update from user options (if applicable)
if ( lastUserOptions[kUserOptionsAtlasWidthKey] > 0 ) { dlg.atlasSizeGrp.dropDownWidth.selection = indexOf(sizeItems, lastUserOptions[kUserOptionsAtlasWidthKey]); }
if ( lastUserOptions[kUserOptionsAtlasHeightKey] > 0 ) { dlg.atlasSizeGrp.dropDownHeight.selection = indexOf(sizeItems, lastUserOptions[kUserOptionsAtlasHeightKey]); }
if ( lastUserOptions[kUserOptionsSourceKey] > 0 ) { dlg.sourceGrp.dropDownSource.selection = lastUserOptions[kUserOptionsSourceKey]; }
if ( lastUserOptions[kUserOptionsFlattenKey] !== undefined ) { dlg.flattenGrp.flattenCheck.value = lastUserOptions[kUserOptionsFlattenKey]; }
if ( lastUserOptions[kUserOptionsIgnoreChildSetsKey] !== undefined ) { dlg.sourceGrp.ignoreChildSetsCheck.value = lastUserOptions[kUserOptionsIgnoreChildSetsKey]; }

if ( (lastUserOptions[kUserOptionsDocNameKey] == app.activeDocument.name) && (lastUserOptions[kUserOptionsLayerSetKey] < app.activeDocument.layerSets.length) ) {
dlg.sourceGrp.dropDownLayerSet.selection = lastUserOptions[kUserOptionsLayerSetKey];
}

userOptionsDlgUpdateLayerSetChildControls(dlg);
dlg.center();
var dlgResult = dlg.show(); // 1 = ok, 2 = cancel

// Note: .selection as a string is the value in the drop down, as an int it's the item's index!
var userOptions = { };
userOptions[kUserOptionsDocNameKey] = app.activeDocument.name;
userOptions[kUserOptionsAtlasWidthKey] = parseInt(sizeItems[parseInt(dlg.atlasSizeGrp.dropDownWidth.selection)]);
userOptions[kUserOptionsAtlasHeightKey] = parseInt(sizeItems[parseInt(dlg.atlasSizeGrp.dropDownHeight.selection)]);
userOptions[kUserOptionsSourceKey] = parseInt(dlg.sourceGrp.dropDownSource.selection);
userOptions[kUserOptionsFlattenKey] = dlg.flattenGrp.flattenCheck.value;
userOptions[kUserOptionsLayerSetKey] = parseInt(dlg.sourceGrp.dropDownLayerSet.selection);
userOptions[kUserOptionsIgnoreChildSetsKey] = dlg.sourceGrp.ignoreChildSetsCheck.value;

if ( isNaN(userOptions[kUserOptionsAtlasWidthKey]) ) { userOptions[kUserOptionsAtlasWidthKey] = 0; }
if ( isNaN(userOptions[kUserOptionsAtlasHeightKey]) ) { userOptions[kUserOptionsAtlasHeightKey] = 0; }

storeLastUserOptions(userOptions);

if ( dlgResult == 2 ) {
return false;
}

return userOptions;
}


function userOptionsDlgUpdateLayerSetChildControls(dlg)
{
if ( dlg.sourceGrp.dropDownSource.selection == kUserOptionsSourceLayerSet ) {
dlg.sourceGrp.dropDownLayerSet.visible = true;
} else {
dlg.sourceGrp.dropDownLayerSet.visible = false;
}

if ( dlg.sourceGrp.dropDownSource.selection == kUserOptionsSourceFrameAnimation ) {
dlg.sourceGrp.ignoreChildSetsCheck.visible = false;
} else {
dlg.sourceGrp.ignoreChildSetsCheck.visible = true;
}
}


function createDestDocWithOptions(userOptions, srcDoc)
{
if ( userOptions === false ) {
return false;
}

var sourceType = userOptions[kUserOptionsSourceKey];
var layerSetIndex = userOptions[kUserOptionsLayerSetKey];
var destDoc = false;

if ( sourceType == kUserOptionsSourceFrameAnimation )
{
// How many frames in the animation?
var frameCount = getFrameCount();

if ( frameCount == 0 ) {
throw "No animation frames were found in this file. In 'Frame Animation' mode this script requires a Photo Frame animation to create a sprite sheet.";
}

destDoc = app.documents.add(srcDoc.width, srcDoc.height, srcDoc.resolution, srcDoc.name + " Sheet");
copyAnimationFrames(userOptions, srcDoc, destDoc, frameCount);
}
else if ( sourceType == kUserOptionsSourceDocumentLayers )
{
destDoc = app.documents.add(srcDoc.width, srcDoc.height, srcDoc.resolution, srcDoc.name + " Sheet");
copyLayersAsSprites(userOptions, srcDoc, destDoc, srcDoc.layers);
}
else if ( sourceType == kUserOptionsSourceLayerSet )
{
if ( layerSetIndex >= srcDoc.layerSets.length ) {
throw "The 'Specific Layer Set' option was selected, but there is no selected layer set in the open document.";
}

if ( srcDoc.layerSets[layerSetIndex].layers.length == 0 ) {
throw "The selected layer set contains no layers.";
}

destDoc = app.documents.add(srcDoc.width, srcDoc.height, srcDoc.resolution, srcDoc.name + " Sheet");
copyLayersAsSprites(userOptions, srcDoc, destDoc, srcDoc.layerSets[layerSetIndex].layers);
}

app.activeDocument = destDoc;
return destDoc;
}


function copyAnimationFrames(userOptions, srcDoc, destDoc, frameCount)
{
// Copy Merged Frames across to new image
for( var k = 1; k <= frameCount; k++ )
{
app.activeDocument = srcDoc;
goToFrame(k);
selectAllLayers();
duplicateSelectedLayers();
mergeSelectedLayers();
var layer = srcDoc.activeLayer.duplicate(destDoc, ElementPlacement.PLACEATEND);
deleteSelectedLayers();

app.activeDocument = destDoc;
layer.name = "Frame " + k;
}
}


function copyLayersAsSprites(userOptions, srcDoc, destDoc, layers)
{
app.activeDocument = srcDoc;

if ( srcDoc == undefined || destDoc == undefined || layers == undefined ) {
throw "copyLayersAsSprites called with undefined parameter(s).";
}

var wasVisible = [];
var bgLayerIndex = -1;
var wasParentVisible = undefined;

// Pre process
for( var k = 0; k < layers.length; k++ ) {
wasVisible[k] = layers[k].visible;

if ( layers[k].isBackgroundLayer ) {
bgLayerIndex = k;
layers[k].isBackgroundLayer = false;
layers[k].visible = wasVisible[k]; // Toggling background layer affects visibility
}
}

if ( layers[0].parent.visible !== undefined ) {
wasParentVisible = layers[0].parent.visible;
layers[0].parent.visible = true;
}

// Do the work
for( var k = 0; k < layers.length; k++ )
{
app.activeDocument = srcDoc;

// Don't process background layer as it causes issues (probably don't want it as a sprite anyway)
if ( k == bgLayerIndex ) {
continue;
}

// If we are ignoring child sets, check and skip if necessary
if ( userOptions[kUserOptionsIgnoreChildSetsKey] && layers[k].layers !== undefined && layers[k].layers.length > 0 ) {
continue;
}

// Turn off all layers (but don't mess with the background layer)
for( var j = 0; j < layers.length; j++ ) {
if ( j != bgLayerIndex ) {
layers[j].visible = false;
}
}

// Turn on active layer
layers[k].visible = true;

// Turn into sprite!
selectAllLayers();
duplicateSelectedLayers();
mergeSelectedLayers();
var layer = srcDoc.activeLayer.duplicate(destDoc, ElementPlacement.PLACEATEND);
deleteSelectedLayers();

app.activeDocument = destDoc;
layer.name = "Sprite " + (k + 1);
}

// Undo Pre-Process
app.activeDocument = srcDoc;

for( var k = 0; k < layers.length; k++ ) {
if ( k == bgLayerIndex ) {
layers[k].isBackgroundLayer = true;
}
}

for( var k = 0; k < layers.length; k++ ) {
layers[k].visible = wasVisible[k];
}

if ( layers[0].parent.visible !== undefined ) {
layers[0].parent.visible = wasParentVisible;
}
}


function atlasSizeForFrames(userOptions, frameWidth, frameHeight, frameCount)
{
var atlasWidth = 16;
var atlasHeight = 16;
var didFit = false;
var fixedWidth = false;
var fixedHeight = false;

if ( userOptions[kUserOptionsAtlasWidthKey] > 0 ) { atlasWidth = userOptions[kUserOptionsAtlasWidthKey]; fixedWidth = true; }
if ( userOptions[kUserOptionsAtlasHeightKey] > 0 ) { atlasHeight = userOptions[kUserOptionsAtlasHeightKey]; fixedHeight = true; }

if ( fixedWidth && fixedHeight ) {
return {width: atlasWidth, height: atlasHeight};
}

while( didFit == false )
{
var x = 0;
var y = 0;

//$.writeln("Trying to fit " + frameCount + "x" + frameWidth + "x" + frameHeight + " onto: " + atlasWidth + " x " + atlasHeight);

var gridWidth = Math.floor(atlasWidth / frameWidth);
var requiredGridHeight = Math.ceil(frameCount / gridWidth);
var requiredPixelHeight = requiredGridHeight * frameHeight;

if ( requiredPixelHeight > atlasHeight )
{
if ( (atlasWidth <= atlasHeight) && (fixedWidth == false) ) { atlasWidth *= 2; }
else { atlasHeight *= 2; }

didFit = false;
}
else
{
didFit = true;
}
}

var result = {width: atlasWidth, height: atlasHeight};
//$.writeln("Atlas Size: " + result.width + " x " + result.height);

return result;
}


function layoutLayersAsSprites(userOptions, destDoc)
{
app.activeDocument = destDoc;

// Figure out atlas size
var frameWidth = parseInt(destDoc.width);
var frameHeight = parseInt(destDoc.height);
var frameCount = destDoc.layers.length;
var atlasSize = atlasSizeForFrames(userOptions, frameWidth, frameHeight, frameCount);
destDoc.resizeCanvas(atlasSize.width, atlasSize.height, AnchorPosition.TOPLEFT);

// Layout Layers
var x = 0;
var y = 0;

for( var k = 0; k < frameCount; ++k )
{
try {
destDoc.layers[k].translate(x, y);
} catch ( e ) { }

x += frameWidth;

if ( (x+frameWidth) > atlasSize.width )
{
x = 0;
y += frameHeight;
}
}

// Layout sprites
for( var k = 0; k < frameCount; k++ )
{
destDoc.layers[k]
}

if ( userOptions[kUserOptionsFlattenKey] == true ) {
selectAllLayers ();
mergeSelectedLayers ();
}
}


//
// Animation Actions
//


// Count the number of frames in the current frame animation (brute force, slow).
function getFrameCount()
{
for( var k = 1; k < 999; k++ )
{
if ( goToFrame(k) == false ) {
return k - 1;
}
}

return 0;
}


// Jump to the given frame in the frame animation in the active document
function goToFrame(jumpToFrame)
{
try {
var desc = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putIndex( stringIDToTypeID( "animationFrameClass" ), jumpToFrame);
desc.putReference( charIDToTypeID( "null" ), ref1 );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );

return true;

} catch(e) {
//$.writeln(e);
}

return false;
}


//
// Layer Editing Actions
//


// Select all layers in the active document
function selectAllLayers()
{
var idselectAllLayers = stringIDToTypeID( "selectAllLayers" );
var desc4 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref1.putEnumerated( idLyr, idOrdn, idTrgt );
desc4.putReference( idnull, ref1 );
executeAction( idselectAllLayers, desc4, DialogModes.NO );
}


// Duplicate all layers that are currently selected
function duplicateSelectedLayers()
{
var idDplc = charIDToTypeID( "Dplc" );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref2.putEnumerated( idLyr, idOrdn, idTrgt );
desc5.putReference( idnull, ref2 );
var idVrsn = charIDToTypeID( "Vrsn" );
desc5.putInteger( idVrsn, 2 );
executeAction( idDplc, desc5, DialogModes.NO );
}


// Merge all currently selected layers
function mergeSelectedLayers()
{
var idMrgtwo = charIDToTypeID( "Mrg2" );

try {
executeAction( idMrgtwo, undefined, DialogModes.NO );
} catch( e ) { }
}


// Delete all selected layers
function deleteSelectedLayers()
{
// Delete selected layers
var idDlt = charIDToTypeID( "Dlt " );
var desc8 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref6 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref6.putEnumerated( idLyr, idOrdn, idTrgt );
desc8.putReference( idnull, ref6 );

try{
executeAction( idDlt, desc8, DialogModes.NO );
} catch(e) { }
}

File Name: Sprite Sheet Generator 0.6.0.jsx