Lee, Andrew Hickinbottom (3D) Tank Girl, Pedro Conti (2D)

View Full Version : [PYTHON] deal with list and cElemenTree (XML)

07-07-2011, 12:05 PM
hi everyone !
I m working on animation exporter and importer .
Until now i was able to export animation without any problem .
Now i experted the animation creating a root called animation and a child for every object or controll i have exported the animation and for ever y object i made a child for each attribute was keyframed and and added to this attributes with the info i needed . the structure will look like that

-----------------and here all the attribute needed for recreating the curve .
------object 2
-----------------and here all the attribute needed for recreating the curve .

so now i exported my data as list for example the keyframe position will be like

keyFramePosition = [0,15,26,45 etc ]

now the problem is when i m going to read that value python wont read it as a list but as a string like this

keyFramePosition = "[0,15,26,45 etc ]"

so i cant go through each item of the list and that s bug me a lot . is it there a way to achieve this?

if not i will modify my script in order to print out single values in the XML .

thank you for help

07-07-2011, 04:07 PM
you can export them just as a sequence with some separator token like ',', and read it back with string.spli(','), which will give you the values as list:
str = "keyFramePosition = 0,15,26,45"
key, vals = str.split('=')
print '# Key', key
print '# Vals', vals.strip().split(',')
# Result:
# Key keyFramePosition
# Vals ['0', '15', '26', '45']

# Or turn them to floats/ints on the fly
key, vals = str.split('=')
vals = [float(v) for v in vals.strip().split(',')]
print '# Key', key
print '# Vals', vals
# Result:
# Key keyFramePosition
# Vals [0.0, 15.0, 26.0, 45.0]

07-07-2011, 05:31 PM
yeah that is what i did dealing with splitting and such . but what i was looking for is if there is a way to handle straight out the list from XML without some kind of formatting . so i m just gonna change a little the output XML instead giving attributes to the channel ( translateX etc etc ) for each channel i m gonna create a subElement with single values for each keyframe for example


etc etc so i will have single values for each attribute

07-07-2011, 07:14 PM
so this is how it looks like my new XML file with the animation of 2 spheres .
so right now it s still pretty rough like the name of the root is fixed etc etc but when the main core will be solid and running as a charm i will start adding stuff as name given from user and so on

http://img535.imageshack.us/img535/6412/xmlu.jpg (http://imageshack.us/photo/my-images/535/xmlu.jpg/)

CGTalk Moderation
07-07-2011, 07:14 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.