Advertisement
Guest User

Untitled

a guest
Jul 31st, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. import xml.etree.ElementTree as ET
  2. import xml.dom.minidom
  3. import os
  4.  
  5. DEBUGOUTPUT = False
  6.  
  7. def update_xml(xmlin, xmlout):
  8.  
  9.     tree = ET.parse(xmlin)
  10.     root = tree.getroot()    
  11.  
  12.     mats = root.findall('Material')
  13.  
  14.     if not mats:
  15.         print(xmlin + ' skipped (no Material section)')
  16.         return
  17.  
  18.     for mat in mats:
  19.         texpat = None
  20.  
  21.         for param in mat:
  22.             if param.attrib['Name'] in ['DiffuseTexture', 'NormalTexture', 'Texture']:
  23.                 texpat = param.text
  24.                 break
  25.  
  26.         if texpat == None:
  27.             print(xmlin + ' skipped (no base textures)')
  28.             return
  29.  
  30.         generate = [('ColorMetalTexture', 'cm'), ('NormalGlossTexture', 'ng'), ('AddMapsTexture', 'add')]
  31.         pattern = [x[0] for x in generate]
  32.         exists = filter(lambda x: x.attrib['Name'] in pattern , mat)
  33.         exists = map(lambda x: x.attrib['Name'], exists)
  34.  
  35.         path, ext = os.path.splitext(texpat)
  36.         texname = ('_'.join(path.split('_')[:-1]))
  37.  
  38.         last = None
  39.         for name, suf in generate:
  40.             if name not in exists:
  41.                 p = ET.SubElement(mat, 'Parameter')
  42.                 p.attrib['Name'] = name
  43.                 p.text = '%s_%s.dds' % (texname, suf)
  44.                 p.tail = '\n\t\t'
  45.                 last = p
  46.  
  47.         if last != None:
  48.             last.tail = '\n\t'
  49.  
  50.             xmlf = xml.dom.minidom.parseString(ET.tostring(root))
  51.             xmlf.writexml(open(xmlout,'w'))
  52.             print('"%s" processed and saved as "%s"' % (xmlin, xmlout))
  53.         else:
  54.             print('"%s" skipped (textures already declared)' % xmlin)
  55.  
  56.  
  57. for root, dirs, filenames in os.walk('.'):
  58.     for f in filenames:
  59.        
  60.         x = os.path.join(root, f)
  61.         xout = os.path.join(root, "out"+f) if DEBUGOUTPUT else x
  62.         if os.path.splitext(f)[1] == '.xml' and not f.startswith("out"):
  63.             update_xml(x, xout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement