forked from KFredberg/gimp-spine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gimp_spine.py
executable file
·204 lines (170 loc) · 6.75 KB
/
gimp_spine.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
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
#!/usr/bin/env python
'''
Get the latest gimp_spine.py script:
https://github.com/clofresh/gimp-spine
Copyright (c) 2014, Carlo Cabanilla <carlo.cabanilla@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ''AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''
'''
Exports layers to images and outputs a Spine JSON file
http://esotericsoftware.com/spine-json-format
Run from the GIMP menu option: File -> Export to Spine
'''
import json
import math
import os.path
import gimpfu
from gimp import pdb
def spine_export(img, active_layer, compression, dir_name, json_filename, export_visible_only, reverse_draw_order, autocrop_layers, padding_amt):
''' Plugin entry point
'''
orig_img = img
orig_active_layer = pdb.gimp_image_get_active_layer(orig_img)
img = pdb.gimp_image_duplicate(orig_img)
# Set up the initial JSON format
output = {
'bones': [{'name': 'root'}],
'slots': [],
'skins': {'default': {}},
'animations': {}
}
slots = output['slots']
attachments = output['skins']['default']
# Iterate through the layers, extracting their info into the JSON output
# and saving the layers as individual images
for layer in img.layers:
if (layer.visible or not(export_visible_only)):
to_save = process_layer(img, layer, slots, attachments, reverse_draw_order, autocrop_layers, padding_amt)
save_layers(img, to_save, compression, dir_name)
# Write the JSON output
if not json_filename:
json_filename = os.path.splitext(os.path.basename(orig_img.filename))[0]
with open(os.path.join(dir_name, '%s.json' % json_filename), 'w') as json_file:
json.dump(output, json_file)
pdb.gimp_image_delete(img)
pdb.gimp_image_set_active_layer(orig_img, orig_active_layer)
def process_layer(img, layer, slots, attachments, reverse_draw_order, autocrop_layers, padding_amt):
''' Extracts the Spine info from each layer, recursing as necessary on
layer groups. Returns all the layers it processed in a flat list.
'''
processed = []
# If this layer is a layer has sublayers, recurse into them
if hasattr(layer, 'layers'):
for sublayer in layer.layers:
processed.extend(process_layer(img, sublayer, slots, attachments, reverse_draw_order, autocrop_layers, padding_amt))
else:
pdb.gimp_image_set_active_layer(img, layer)
if autocrop_layers:
pdb.plug_in_autocrop_layer(img, layer)
if padding_amt > 0:
pdb.gimp_layer_resize(layer, layer.width + 2*padding_amt, layer.height + 2*padding_amt, padding_amt, padding_amt)
layer_name = layer.name
if reverse_draw_order:
slots.append({
'name': layer_name,
'bone': 'root',
'attachment': layer_name,
})
else:
slots.insert(0, {
'name': layer_name,
'bone': 'root',
'attachment': layer_name,
})
x, y = layer.offsets
# Compensate for GIMP using the top left as the origin, vs Spine
# using the center.
x += math.floor(layer.width / 2)
y += math.floor(layer.height / 2)
# Center the image on Spine's x origin,
x -= math.floor(img.width / 2)
# Compensate for GIMP's y axis going from top to bottom, vs Spine
# going bottom to top
y = img.height - y
attachments[layer_name] = {layer_name: {
'x': x,
'y': y,
'rotation': 0,
'width': layer.width,
'height': layer.height,
}}
processed.append(layer)
return processed
def save_layers(img, layers, compression, dir_name):
''' Takes a list of layers and saves them in `dir_name` as PNGs,
naming the files after their layer names.
'''
for layer in layers:
tmp_img = pdb.gimp_image_new(img.width, img.height, img.base_type)
tmp_layer = pdb.gimp_layer_new_from_drawable(layer, tmp_img)
tmp_layer.name = layer.name
tmp_img.add_layer(tmp_layer, 0)
filename = '%s.png' % layer.name
fullpath = os.path.join(dir_name, filename)
tmp_img.resize_to_layers()
pdb.file_png_save(
tmp_img,
tmp_img.layers[0],
fullpath,
filename,
0, # interlace
compression, # compression
1, # bkgd
1, # gama
1, # offs
1, # phys
1 # time
)
gimpfu.register(
# name
"spine-export",
# blurb
"Spine export",
# help
"Exports layers to images and outputs a Spine JSON file",
# author
"Carlo Cabanilla",
# copyright
"Carlo Cabanilla",
# date
"2014",
# menupath
"<Image>/File/Export/Export to Spine",
# imagetypes
"*",
# params
[
(gimpfu.PF_ADJUSTMENT, "compression", "PNG Compression level:", 0, (0, 9, 1)),
(gimpfu.PF_DIRNAME, "dir", "Directory", "/tmp"),
(gimpfu.PF_STRING, "json_filename", "JSON filename", ""),
(gimpfu.PF_TOGGLE, "export_visible_only", "Export visible layers only", True),
(gimpfu.PF_TOGGLE, "reverse_draw_order", "Reverse draw order", False),
(gimpfu.PF_TOGGLE, "autocrop_layers", "Autocrop layers", False),
(gimpfu.PF_ADJUSTMENT, "padding_amt", "Padding pixels", 0, (0,100,1))
],
# results
[],
# function
spine_export
)
gimpfu.main()