I created a simple yet very usefull plug-in to GIMP.
It generates a sprite sheet plist that can be loaded by cocos2d.
To use it create cocos2dSpriteSheet.py in the 'plug-ins' directory inside of gimp. For me, on osx, that is located at /Gimp.app/Contents/Resources/lib/gimp/2.0/plug-ins/
once you create the file past this code into it.
#!/usr/bin/env python
from gimpfu import *
from xml.dom.minidom import Document
import os
import time
gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
def addDictionaryEntry(document, element, name, plistType, content = None):
key = document.createElement("key")
key.appendChild(document.createTextNode(name))
element.appendChild(key);
result = document.createElement(plistType)
element.appendChild(result)
if content is not None:
result.appendChild(document.createTextNode(content))
return result
def cocos2dSpritesheet(img, drawable):
gimp.context_push()
doc = Document()
plist = doc.createElement("plist")
plist.setAttribute("version", "1.0")
doc.appendChild(plist)
rootDict = doc.createElement("dict")
plist.appendChild(rootDict)
#add image atrubutes
texture = addDictionaryEntry(doc, rootDict, "texture", "dict")
addDictionaryEntry(doc, texture, "width", "integer", str(img.width))
addDictionaryEntry(doc, texture, "height", "integer", str(img.width))
#add frames
frames = addDictionaryEntry(doc, rootDict, "frames", "dict")
for layer in img.layers:
#add frame
frame = addDictionaryEntry(doc, frames, layer.name, "dict")
addDictionaryEntry(doc, frame, "x", "integer", str(layer.offsets[0]))
addDictionaryEntry(doc, frame, "y", "integer", str(layer.offsets[1]))
addDictionaryEntry(doc, frame, "width", "integer", str(layer.width))
addDictionaryEntry(doc, frame, "height", "integer", str(layer.height))
addDictionaryEntry(doc, frame, "offsetX", "integer", "0")
addDictionaryEntry(doc, frame, "offsetY", "integer", "0")
addDictionaryEntry(doc, frame, "originalWidth", "integer", str(layer.width))
addDictionaryEntry(doc, frame, "originalHeight", "integer", str(layer.height))
xmlString = doc.toxml()
file = open(os.path.splitext(img.filename)[0] + ".plist", 'w')
file.write(xmlString)
file.close()
gimp.context_pop()
register(
"python-fu-cocos2dSpritesheet",
N_("Exports a cocos2d spritesheet"),
"Exports a plist file with each layer as a sprite frame",
"James Lambert",
"James Lambert",
"2011",
N_("_Cocos2D Spritesheet..."),
"RGB*, GRAY*",
[
(PF_IMAGE, "image", "Input image", None),
(PF_DRAWABLE, "drawable", "Input drawable", None),
],
[],
cocos2dSpritesheet,
menu="<Image>/File/Export",
domain=("gimp20-python", gimp.locale_directory)
)
main()
Run gimp and open or create a new image
Note: if you create a new image you will need to save that as a file somewhere before the script can export the plist
Each layer in the image will be exported as a sprite frame. The name in gimp will be the exported name.
To export the file select File->Export->Cocos2D Spritesheet
When selected it will automatically output the plist file in the same directory as the image replacing the image extension with .plist
It is very basic and could use some improvements but I find it very usefull and hope it could be of help to any of you. Also, any feedback would be accecpted as well.