-
Notifications
You must be signed in to change notification settings - Fork 4
/
export_spritesheet.py
112 lines (93 loc) · 3.77 KB
/
export_spritesheet.py
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
#!/usr/bin/env python
import os
from gimpfu import *
import gimpenums
from pygimplib import pgpdb
#http://doc.starling-framework.org/v2.1/starling/textures/TextureAtlas.html
def export_spritesheet(image, layer, outputFolder):
''' Save the current layer into a PNG file, a JPEG file and a BMP file.
Parameters:
image : image The current image.
layer : layer The layer of the image that is selected.
outputFolder : string The folder in which to save the images.
'''
spritesheetName = os.path.splitext(image.name)[0]
atlasImageFilename = "%s.png" % (spritesheetName)
outputPath = outputFolder + os.sep + atlasImageFilename
atlasPath = outputFolder + os.sep + "%s.xml" % (spritesheetName)
atlas = open(atlasPath,"w")
atlas.write("<TextureAtlas imagePath='"+atlasImageFilename+"'>\n")
spritesheet = pgpdb.duplicate(image, metadata_only=True)
spriteCount = 0
for i in range(len(image.layers)):
if not image.layers[i].visible:
# ignore invisible layers
continue
if not(image.layers[i].name.startswith('[') and image.layers[i].name.endswith(']')):
spriteCount += 1
cols = math.ceil(math.sqrt(spriteCount))
x = 0
y = 0
imageWidth = 0
imageHeight = 0
exportedLayers = 0
spritesInRow = 0
for i in range(len(image.layers)):
if not image.layers[i].visible:
# ignore invisible layers
continue
if not(image.layers[i].name.startswith('[') and image.layers[i].name.endswith(']')):
layer_copy = pdb.gimp_layer_new_from_drawable(image.layers[i], spritesheet)
pdb.gimp_image_insert_layer(spritesheet, layer_copy, None, exportedLayers)
# This is necessary for file formats which flatten the image (such as JPG).
pdb.gimp_item_set_visible(layer_copy, True)
pdb.gimp_layer_set_offsets(spritesheet.layers[exportedLayers],x,y) #offset layers
layerWidth = image.layers[i].width
layerHeight = image.layers[i].height
layerName = image.layers[i].name
atlas.write(" <SubTexture name='%s' x='%d' y='%d' width='%d' height='%d'/>\n" % (
layerName, x, y, layerWidth, layerHeight
))
x += layerWidth
imageWidth = max(imageWidth,x)
imageHeight = max(imageHeight,y+layerHeight)
exportedLayers += 1
spritesInRow += 1
if spritesInRow >= cols:
spritesInRow = 0
x = 0
y += layerHeight
exportedBackgrounds = 0
for i in range(len(image.layers)):
if not image.layers[i].visible:
# ignore invisible layers
continue
if image.layers[i].name.startswith('[') and image.layers[i].name.endswith(']'):
for j in range(exportedLayers):
layer_copy = pdb.gimp_layer_new_from_drawable(image.layers[i], spritesheet)
pdb.gimp_image_insert_layer(spritesheet, layer_copy, None, exportedLayers + exportedBackgrounds)
offsets = spritesheet.layers[exportedBackgrounds].offsets
pdb.gimp_layer_set_offsets(spritesheet.layers[exportedLayers + exportedBackgrounds],offsets[0],offsets[1]) #offset layers
exportedBackgrounds += 1
atlas.write("</TextureAtlas>\n")
atlas.close()
pdb.gimp_image_resize(spritesheet,imageWidth,imageHeight,0,0)
# flattening would kill the alpha
layer = pdb.gimp_image_merge_visible_layers(spritesheet, gimpenums.CLIP_TO_IMAGE)
pdb.file_png_save(spritesheet, spritesheet.layers[0], outputPath, outputPath, 0, 9, 0, 0, 0, 0, 0)
pdb.gimp_image_delete(spritesheet)
register(
"python_fu_export_spritesheet",
"Export spritesheet",
"Export spritesheet.",
"jarnik",
"MIT",
"2017",
"<Image>/File/Export/Export Spritesheet",
"*",
[
(PF_DIRNAME, "outputFolder", "Output directory", "/tmp"),
],
[],
export_spritesheet)
main()