Created
October 7, 2013 06:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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