February 23, 2012
by Chris
Comments Off
Quick (and not too pretty) python script I threw together for generating animation frame plists. For a tutorial about using the plist to drive your cocos2d animation check out this great tutorial from Ray Wenderlich’s site.
I had a list of 208 separate frames of animation that were added to a sprite map as part of the build in XCode using TexturePacker.
There are a number of assumptions made about the animations but at least the script should save some time on the initial creation.
* Each animation frame file starts with a name that will become the name of the plist.
* Frames have a sequence number after a ‘_’ that is used to determine the order.
* The sequence does not have to be continuous i.e 00002, 00003, 00006 is fine
* Don’t put anything in the directory that you are loading the images from that is not an image.
import sys
import os
def writePlist(fileName, content):
# Create a file object:
# in "write" mode
FILE = open(fileName,"w")
header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
header += "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
header += "<plist version=\"1.0\">\n<array>\n"
content = header + content + "</array>\n</plist>"
# Write all the lines at once:
FILE.writelines(content)
FILE.close()
# If no database is specified, drop out
if len(sys.argv) < 2:
sys.exit('Usage: %s directoryOfImages' % sys.argv[0])
# Get the path to the directory of images
imageDir = sys.argv[1] + '/'
if not os.path.exists(imageDir):
sys.exit('Directory: %s does not exist' % imageDir)
fileName=""
fileContents=""
listing = os.listdir(imageDir)
for infile in listing:
fileParts = infile.split("_")
fileStub = fileParts[0]
if fileStub != fileName:
if fileName != "":
writePlist(fileName + ".plist", fileContents)
fileName = fileStub
fileContents = "\t<string>" + infile + "</string>\n"
fileContents+= "\t<string>" + infile + "</string>\n"
#write the last file
writePlist(fileName + ".plist", fileContents) |
To use it (assuming you named it plist.py)
>ls -la safe_png
drwxr-xr-x 210 abitofcode staff 7.0K 23 Feb 21:54 ./
drwxr-xr-x 27 abitofcode staff 918B 23 Feb 15:50 ../
-rw-r–r–@ 1 abitofcode staff 17K 23 Feb 13:11 hookDead_00001.png
-rw-r–r–@ 1 abitofcode staff 22K 23 Feb 13:11 hookDead_00002.png
-rw-r–r–@ 1 abitofcode staff 21K 23 Feb 13:11 hookDead_00003.png
-rw-r–r–@ 1 abitofcode staff 22K 23 Feb 13:11 hookDead_00004.png
-rw-r–r–@ 1 abitofcode staff 21K 23 Feb 13:11 hookDead_00005.png
-rw-r–r–@ 1 abitofcode staff 21K 23 Feb 13:11 hookDead_00006.png
-rw-r–r–@ 1 abitofcode staff 22K 23 Feb 13:11 hookDead_00007.png
-rw-r–r–@ 1 abitofcode staff 22K 23 Feb 13:11 hookDead_00008.png
-rw-r–r–@ 1 abitofcode staff 22K 23 Feb 13:11 hookDead_00009.png
-rw-r–r–@ 1 abitofcode staff 22K 23 Feb 13:11 hookDead_00010.png
-rw-r–r–@ 1 abitofcode staff 22K 23 Feb 13:11 hookDead_00011.png
-rw-r–r–@ 1 abitofcode staff 21K 23 Feb 13:11 hookDead_00012.png
>python plist.py safe_png
The above example would generate a single plist called hookDead.plist with 12 entries but the script can be run against whole directories of files.
Download the files here: Python animation Plist builder