Starling Sprite Sheet XML to libGDX Texture Atlas Converter Script (Python)

Starling Sprite Sheet XML to libGDX Texture Atlas Converter Script (Python)

I'm trying to start making games in libGDX (well, technically I'm using Flixel-GDX) and I needed some quick art, so I jumped onto OpenGameArt.org and found "Puzzle Stones" by 1001.com. "Ah, this looks fun" I think to myself. But then I realize that libGDX requires a texture atlas and it is very, very fussy about the format it accepts. It doesn't like the XML that 1001.com's sprite sheet comes with. Kenny's art uses that same XML format in his sprite sheets apparently. All this great art, but not easy to import into libGDX! Oh noes!!

(later edit) Apparently, this XML format is used in sprite sheets for Starling.

Well, there are probably better ways to do this, but I wrote this Python 3 script to let you use their sprite sheet in libGDX by generating an atlas in libGDX's format from the XML provided without needing to repack the sprite sheet with TexturePacker. The first command line argument for this script is the XML file, and the second command line argument is the atlas file you want to create, so in the "Puzzle Stones" case, the arguments were, "sprites.xml sprites.atlas"

import sys

import xml.etree.ElementTree as ET

tree = ET.parse(sys.argv[1])

root = tree.getroot()

f = open(sys.argv[2], 'w')

f.write('\n' + root.get('imagePath'))

f.write('\nformat: RGBA8888\nfilter: Nearest,Nearest\nrepeat: none\n')

for texture in root:

    f.write(texture.get('name') + '\n  rotate: false\n')

    f.write('  xy: ' + texture.get('x') + ', ' + texture.get('y') + '\n')

    f.write('  size: ' + texture.get('width') + ', ' + texture.get('height') + '\n')

    f.write('  orig: ' + texture.get('width') + ', ' + texture.get('height') + '\n')

    f.write('  offset: 0, 0\n  index: -1\n')

f.close

 

It should work for all of 1001.com and Kenny's art and any other art which uses that XML format. By the way, I tested it in Flixel-GDX using this tutorial. Remember that all assets (this includes art and atlas files) in libGDX-based projects need to go in your Android project's assets folder even if you don't plan to deploy on Android!