Advertisement
Guest User

Untitled

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