| from pathlib import Path |
| import json |
| import re |
|
|
| name_uuid = {} |
| uuid_map = {} |
| for dir in Path(__file__).parent.iterdir(): |
| if not dir.is_dir(): |
| continue |
| |
| old_name = dir.name |
| new_name = re.sub(r'(?<!^)(?=[A-Z])', '_', old_name).lower() |
| new_name = new_name.replace(' ', '_') |
| |
| if old_name != new_name: |
| new_dir = dir.with_name(new_name) |
| dir.rename(new_dir) |
| current_dir = new_dir |
| else: |
| current_dir = dir |
| |
| obj_dirs = sorted([d for d in current_dir.iterdir() if d.is_dir()], key=lambda x: x.name) |
| |
| all_correct = all(d.name == f"{i:05d}" for i, d in enumerate(obj_dirs)) |
| if not all_correct: |
| print(f"Renaming directories in {current_dir} to ensure correct order...") |
| temp_dirs = [] |
| for d in obj_dirs: |
| temp_dir = d.with_name(d.name + "_temp") |
| d.rename(temp_dir) |
| temp_dirs.append(temp_dir) |
| |
| obj_dirs = [] |
| for i, d in enumerate(temp_dirs): |
| final_dir = d.with_name(f"{i:05d}") |
| d.rename(final_dir) |
| obj_dirs.append(final_dir) |
| |
| for obj_dir in obj_dirs: |
| metadata_path = obj_dir / "metadata.json" |
| if metadata_path.exists(): |
| try: |
| with open(metadata_path, 'r', encoding='utf-8') as f: |
| data = json.load(f) |
| if "uuid" in data: |
| name_uuid[f'/Rigid/{current_dir.name}/{obj_dir.name}'] = data["uuid"] |
| if data["uuid"] in uuid_map: |
| print(f"Warning: Duplicate UUID {data['uuid']} found in {metadata_path}.") |
| else: |
| uuid_map[data["uuid"]] = f'/Rigid/{current_dir.name}/{obj_dir.name}' |
| else: |
| print(f"Warning: 'uuid' key not found in {metadata_path}.") |
| except json.JSONDecodeError: |
| print(f"Error: {metadata_path} contains invalid JSON.") |
| else: |
| print(f"Warning: {metadata_path} does not exist.") |
|
|
| output_path = Path(__file__).parent / "uuid_map.json" |
| with open(output_path, 'w', encoding='utf-8') as f: |
| json.dump(name_uuid, f, indent=4, ensure_ascii=False) |
| print('Format OK! UUID map saved to uuid_map.json') |