Thursday, 26 May 2011

Converting MonkeyJam Files to Spreadsheets with Python

I've timed out the 2D blocking for all 42 shots of my 'Crows' short in MonkeyJam.  Today I realised it would be great to have this accessible as real paper X-sheets which I could print and annotate as I work through the shots in 3D.  MonkeyJam beta's print feature seems pretty unstable and sometimes just generates junk, so I managed to hack together this quick python 3 script which will read the MonkeyJam '.xps' file and spit out a .csv file which you can then open in OpenOffice or Excel (or just open in notepad and copy and paste into a pre-formatted spreadsheet with all the right font sizes, column widths etc).



from xml.etree import ElementTree as ET


#remember to use double backslashes here
in_file='H:\\work\\experimental\\forthecrows2\\software\\xml_parser\\test.xps'


print("running")
cels=0
tree=ET.parse(in_file)
rootElement = tree.getroot()
for subelement in rootElement:
    if subelement.tag=='layers':
        num_layers=int(subelement.attrib['cols'])
        
        #create a list of layers
        layer_output=[]
        layer_captions=[]
        layer_dirs=[]


        for layer_index,each_layer in enumerate(subelement):
            if each_layer.tag=='layer':
                layer_output.append([])
                layer_captions.append(each_layer.attrib['caption'])
                layer_dirs.append(each_layer.attrib['dir'])


                for cel_index,each_cel in enumerate(each_layer):
                    if each_cel.tag=='cel':
                        cel_first=int(each_cel.attrib['row'])-1
                        cel_last=(int(each_cel.attrib['duration'])+cel_first)-1


                        while len(layer_output[layer_index])<(cel_last+1):
                            layer_output[layer_index].append('')


                        layer_output[layer_index][cel_first]=each_cel.text


                        temp_cel=cel_first+1
                        while temp_cel<cel_last:
                            layer_output[layer_index][temp_cel]='|'
                            temp_cel+=1


                        if cel_last>cel_first:
                            layer_output[layer_index][cel_last]='--'


                        if cel_last>cels:
                            cels=cel_last
                        
#gen output
with open(in_file[:len(in_file)-3]+'csv', mode='w') as output_file:
    output_file.write('"'+in_file+'"\n')


    temp_text='"Layer Name",'
    for each_layer_caption in layer_captions:
        temp_text+='"'+each_layer_caption+'",'
    temp_text=temp_text.rstrip(',')+'\n'
    output_file.write(temp_text)


    temp_text='"Layer Path",'
    for each_layer_dir in layer_dirs:
        temp_text+='"'+each_layer_dir+'",'
    temp_text=temp_text.rstrip(',')+'\n'
    output_file.write(temp_text)


    for each_cel_index in range(0,cels+1):
        temp_text='"'+str(each_cel_index+1)+'",'
        for each_layer_index in range(0,len(layer_output)):
            if each_cel_index<len(layer_output[each_layer_index]):
                temp_text+='"'+layer_output[each_layer_index][each_cel_index]+'",'
            else:
                temp_text+='"",'
        temp_text=temp_text.rstrip(',')+'\n'
        output_file.write(temp_text)

output_file.close()

1 comment:

  1. How does this work? I've never heard of phyton before...

    ReplyDelete