import os import xml.etree.ElementTree as ET # Define the folder containing .cmap files folder_path = '/path/to/your/folder/' # Verbosity flag verbose = True # Check if the folder exists if not os.path.isdir(folder_path): raise FileNotFoundError(f"The folder {folder_path} does not exist.") # Iterate through each .cmap file in the folder for filename in os.listdir(folder_path): if filename.endswith('.cmap'): file_path = os.path.join(folder_path, filename) try: # Load and parse the XML file tree = ET.parse(file_path) root = tree.getroot() # Extract color elements and handle dynamic attribute order colors = [] for color in root.findall('color'): r = round(float(color.get('r')), 6) g = round(float(color.get('g')), 6) b = round(float(color.get('b')), 6) a = round(float(color.get('a')), 6) colors.append((r, g, b, a)) # Save the colormap as a .txt file output_file = os.path.splitext(file_path)[0] + '_colormap.txt' with open(output_file, 'w') as f: for color in colors: f.write(f"{color[0]:.6f} {color[1]:.6f} {color[2]:.6f} {color[3]:.6f}\n") if verbose: print(f"Colormap saved as {output_file}") except ET.ParseError as e: print(f"Failed to parse {file_path}: {e}") except Exception as e: print(f"An error occurred with file {file_path}: {e}")