FIFE forums

Please login or register.

Login with username, password and session length
Advanced search  

News:

FIFE 0.4.0 has been released on 15th of January, 2017!

Author Topic: Object XML Generator  (Read 1976 times)

Drakekin

  • Newbie
  • Posts: 7
    • View Profile
Object XML Generator
« on: December 22, 2010, 05:26:26 pm »

As part of my work on my game Expansion, I have written a small shell script to help ease the creation of the XML files for objects. It's released under the beerware license, which is a fun name for public domain. Have fun with it, and it would be nice to know if anyone found it useful.

objectGenerator.py:
Code: [Select]
####################################################################
#                                                                    #
#   This code is released under the beerware license. It is to all   #
#    intents and purposes public domain, but if you ever meet me     #
#  (Connor Shearwood), it would be nice if you would buy me a beer.  #
#                                                                    #
#            Please retain this notice unless you've                 #
#         edited the code, then please change it to your             #
#                     name instead of mine                           #
#                                                                    #
 ####################################################################

from xml.etree import ElementTree as xml

def generateObject():
    print "FIFE Object File Generator"
    print "Written by Connor Shearwood."
    print "Relased under the Beerware License"
    print ""

    # Get the basic information needed
    filename = raw_input("Object File Location: ")
    namespace = raw_input("Object Namespace: ")
    name = raw_input("Object Name: ")
    static = raw_input("Static (true/false): ")
    blocking = raw_input("Blocking (true/false): ")

    # Convert the static and blocking values into the values FIFE is
    # expecting. Assume non "true" values are false
    if static.lower() == "true":
        static = "1"
    else:
        static = "0"
    if blocking.lower() == "true":
        blocking = "1"
    else:
        blocking = "0"

    # Generate the object xml tag
    obj = xmlElement("object", {"id": name, "namespace": namespace, "blocking": blocking, "static": static}, text=" ")

    # Ask if it is a single image or series of images at different angles
    single = raw_input("Is this object a single, named image (true/false): ")
    if single.lower() == "true":
        # If so, don't ask for angle and move onto location and offset
        img = raw_input("Image Location: ")
        x_off = raw_input("X Offset: ")
        y_off = raw_input("Y Offset: ")

        # Then add the single image tag inside the object tag
        xmlSubElement(obj, "image", {"source": img, "direction": "0", "x_offset": x_off, "y_offset": y_off}, text=" ")
    else:
        # Otherwise ask for the direction or if the loop should end.
        print "Type the angle of the current image below, or END to end."
        cont = True

        # Look whilst the user hasn't quit
        while cont:
            direction = raw_input("Image Direction: ")
            if direction.lower() == "end":
                cont = False
            else:
                # Ask for the image location and offset
                img = raw_input("Image Location: ")
                x_off = raw_input("X Offset: ")
                y_off = raw_input("Y Offset: ")

                # Add the image tag inside the object tag
                xmlSubElement(obj, "image", {"source": img, "direction": direction, "x_offset": x_off, "y_offset": y_off}, text=" ")

    # Convert the tag structure to a string
    indent(obj)
    objtext = xml.tostring(obj)

    # Open the file for writing
    objfile = open(filename, "w")

    # Write the xml declaration and the tag structure to the file
    objfile.writelines(['<?fife type="object"?>', objtext])

    # Close the file
    objfile.close()

    # Tell the user you've finished
    print filename, "written."

    # Ask the user if they want to create another file
    new = raw_input("Create another file? (true/false): ")

    # Check their input, again assuming anything that isn't "true" is
    # automatically false
    if new.lower() == "true":
        # Print a divider then start again
        print ""
        print "======================================"
        print ""
        generateObject()

def xmlElement(name, attribdict, text="", tail=""):
    element = xml.Element(name, attribdict)
    element.text = text
    element.tail = tail
    return element

def xmlSubElement(parent, name, attribdict, text="", tail=""):
    element = xml.SubElement(parent, name, attribdict)
    element.text = text
    element.tail = tail
    return element

def indent(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

# And start the function when run
generateObject()
« Last Edit: December 23, 2010, 08:51:36 am by Drakekin »
Logged
Expansion Developer/Artist/Writer

prock

  • Developer
  • Full Member
  • *
  • Posts: 236
    • View Profile
Re: Object XML Generator
« Reply #1 on: December 23, 2010, 09:13:27 am »

Awesome... Thanks Drakekin for the contribution! 
Logged