cfox04

dump_bonetags.py

Dec 21st, 2022 (edited)
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. import os
  2. import sys
  3. from xml.etree.ElementTree import ElementTree
  4. from typing import Generator
  5. from collections import defaultdict
  6.  
  7. XML_DIR = "./test"  # path to folder with xmls
  8. OUTPUT_PATH = "./bonetags.txt"
  9. CONFLICTS_PATH = "./conflicts.txt"
  10.  
  11.  
  12. def walk_dir_xml(dir: str) -> Generator[ElementTree, None, None]:
  13.     """Produce a generator of all .xml files as ET.ElementTree in a directory"""
  14.     files = os.listdir(dir)
  15.     for filename in files:
  16.         f = os.path.join(dir, filename)
  17.         # checking if it is a file
  18.         if os.path.isfile(f) and os.path.splitext(filename)[1] == ".xml":
  19.             doc = ElementTree()
  20.             doc.parse(f)
  21.  
  22.             yield os.path.basename(f), doc
  23.  
  24.  
  25. def print_status(i, n):
  26.     j = (i + 1) / n
  27.     sys.stdout.write('\r')
  28.     # the exact output you're looking for:
  29.     sys.stdout.write("[%-20s] %d%%" % ('='*int(20*j), 100*j))
  30.     sys.stdout.flush()
  31.  
  32.  
  33. with open(OUTPUT_PATH, "w") as f:
  34.     num_files = len(os.listdir(XML_DIR))
  35.  
  36.     print(f"\nTesting {XML_DIR}... ({num_files} file(s))\n")
  37.  
  38.     bone_tags = {}
  39.     conflicts = []
  40.  
  41.     num_files = len(os.listdir(XML_DIR))
  42.     current_file = 0
  43.  
  44.     for filename, doc in walk_dir_xml(XML_DIR):
  45.         bones = doc.find(".//Bones")
  46.         for item in bones:
  47.             bone_name = item.find("Name").text
  48.             bone_tag = item.find("Tag").get("value")
  49.  
  50.             if bone_name in bone_tags and bone_tags[bone_name] != bone_tag:
  51.                 conflicts.append(
  52.                     f"Bone tag conflict in '{filename}' for bone '{bone_name}': {bone_tag} != {bone_tags[bone_name]}\n")
  53.  
  54.                 continue
  55.  
  56.             bone_tags[bone_name] = bone_tag
  57.  
  58.         print_status(current_file, num_files)
  59.         current_file += 1
  60.  
  61.     f.write("BONE_TAGS = {\n")
  62.     for name, tag in bone_tags.items():
  63.         f.write(f"    {name} = {tag},\n")
  64.     f.write("}")
  65.  
  66.     print("\nBone tag dump complete!")
  67.  
  68.     if conflicts:
  69.         with open(CONFLICTS_PATH, "w") as f2:
  70.             for conflict in conflicts:
  71.                 f2.write(conflict)
  72.  
Advertisement
Add Comment
Please, Sign In to add comment