Skip to content

Instantly share code, notes, and snippets.

@antont
Created October 7, 2013 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antont/6863384 to your computer and use it in GitHub Desktop.
Save antont/6863384 to your computer and use it in GitHub Desktop.
read 3d ob positions from txml in python and output them as json for easier use in e.g. web clients
from xml.etree.ElementTree import ElementTree
import xml.etree.ElementTree as ET
import sys
if len(sys.argv) != 2:
print "Giev filename!"
sys.exit(1)
t = ElementTree()
s = t.parse(sys.argv[1])
ents = s.getiterator("entity")
def attrval(el, attrname):
attr = el.find(".//attribute[@name='%s']" % attrname)
return attr.attrib['value']
def floatlist(el, attrname):
return [float(v) for v in attrval(el, attrname).split(',')]
def conv(coords):
a, b, c = coords
return list((a, b, -c))
posses = []
idx2pos = {}
for e in ents:
n = e.find("component[@type='EC_Name']")
name = attrval(n, "name")
idx = int(name.split('_')[-1])
p = e.find("component[@type='EC_Placeable']")
tr = floatlist(p, "Transform")
pos = tr[:3]
idx2pos[idx] = pos
keys = idx2pos.keys()
keys.sort()
posses = [conv(idx2pos[k]) for k in keys]
print posses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment