Skip to content

Instantly share code, notes, and snippets.

@strawbryjam
Forked from NathanSweet/Gimp-gimp_spine.py
Last active February 14, 2021 19:03
Show Gist options
  • Save strawbryjam/b9ba862f9ff808d6493f to your computer and use it in GitHub Desktop.
Save strawbryjam/b9ba862f9ff808d6493f to your computer and use it in GitHub Desktop.
Gimp script to export to Esoteric Software's Spine: http://esotericsoftware.com/
#!/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
=> Edits to fix issues where draw order is backwards.
=> Added options to autocrop layers.
=> Added options to export only visible layers.
--Strawbry_jam
'''
import json
import math
import os.path
import gimpfu
from gimp import pdb
def spine_export(img, active_layer, compression, vis, strip, dir_name):
''' Plugin entry point
'''
# Set up the initial JSON format
output = {
'bones': [{'name': 'root'}],
'slots': [],
'skins': {'default': {}},
'animations': {}
}
slots = output['slots']
attachments = output['skins']['default']
# Set undo group
pdb.gimp_image_undo_group_start(img)
# Fix layer order
pdb.script_fu_reverse_layers(img, active_layer)
# Iterate through the layers, extracting their info into the JSON output
# and saving the layers as individual images
for layer in img.layers:
#check if layer is visible
if (layer.visible or not vis):
# Autocrop layers
if (strip):
pdb.gimp_image_set_active_layer(img, layer)
pdb.plug_in_autocrop_layer(img, layer)
to_save = process_layer(img, layer, slots, attachments)
save_layers(img, to_save, compression, dir_name)
# Write the JSON output
name = os.path.splitext(os.path.basename(img.filename))[0]
with open(os.path.join(dir_name, '%s.json' % name), 'w') as json_file:
json.dump(output, json_file)
# Reset layer order
pdb.script_fu_reverse_layers(img, active_layer)
#End undo group
pdb.gimp_image_undo_group_end(img)
def process_layer(img, layer, slots, attachments):
''' 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))
else:
layer_name = layer.name
slots.append({
'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_TOGGLE, "vis", "Visible Only:", True),
(gimpfu.PF_TOGGLE, "strip", "Strip Whitespace:", True),
(gimpfu.PF_DIRNAME, "dir", "Directory", "/tmp")
],
# results
[],
# function
spine_export
)
gimpfu.main()
@8beat0405
Copy link

This works fine.
Thanks for update.

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