File size: 2,309 Bytes
42efb62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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')