File size: 3,842 Bytes
45b4835
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import json
import os

def load_metadata():
    """Load metadata from metadata.jsonl"""
    metadata_path = "metadata.jsonl"
    metadata = {}
    
    if not os.path.exists(metadata_path):
        print(f"Error: '{metadata_path}' file not found!")
        return metadata
    
    # Parse JSONL file and map descriptions by sprite ID
    try:
        with open(metadata_path, 'r') as f:
            for line in f:
                if line.strip():
                    entry = json.loads(line)
                    # Extract sprite ID from file_name (e.g., "images/23.png" -> "23")
                    sprite_id = entry["file_name"].split("/")[1].split(".")[0]
                    metadata[sprite_id] = entry["text"]
    except Exception as e:
        print(f"Error reading metadata file: {str(e)}")
    
    print(f"Loaded {len(metadata)} entries from metadata file")
    return metadata

def extract_info_safely(description):
    """Extract information from description with error handling"""
    info = {
        "frames": "unknown",
        "action": "unknown",
        "direction": "unknown",
        "character": "unknown"
    }
    
    try:
        # Standard format: "X-frame sprite animation of: CHARACTER, that: ACTION, facing: DIRECTION"
        if "-frame sprite animation of:" in description:
            # Extract frame count
            info["frames"] = description.split("-frame")[0].strip()
            
            # Extract character
            if "of:" in description and ", that:" in description:
                character_part = description.split("of:")[1].split(", that:")[0].strip()
                info["character"] = character_part
            
            # Extract action
            if ", that:" in description and ", facing:" in description:
                action_part = description.split(", that:")[1].split(", facing:")[0].strip()
                info["action"] = action_part
            elif ", that:" in description:
                # Handle case where facing might not be present
                action_part = description.split(", that:")[1].strip()
                info["action"] = action_part
            
            # Extract direction
            if ", facing:" in description:
                direction_part = description.split(", facing:")[1].strip()
                info["direction"] = direction_part
    except Exception as e:
        print(f"Warning: Error parsing description: '{description}'")
        print(f"Error details: {str(e)}")
    
    return info

def create_sprite_metadata():
    """Create proper sprite metadata from metadata.jsonl"""
    # Load metadata
    metadata = load_metadata()
    if not metadata:
        print("No metadata entries found. Check if metadata.jsonl exists and has content.")
        return {}
    
    # Create metadata mapping
    sprite_info = {}
    
    # Process each entry in the metadata
    for sprite_id, description in metadata.items():
        # Extract information safely
        info = extract_info_safely(description)
        
        # Create folder name using the sprite ID in the correct format
        folder_name = f"spritesheet_{sprite_id}"
        
        # Create organized structure with folder name
        sprite_info[sprite_id] = {
            "frames": info["frames"],
            "action": info["action"],
            "direction": info["direction"],
            "character": info["character"],
            "folder_name": folder_name,
            "full_description": description
        }
    
    # Save metadata to JSON file
    metadata_path = "sprite_metadata.json"
    with open(metadata_path, "w") as f:
        json.dump(sprite_info, f, indent=4)
            
    print(f"Created sprite metadata for {len(sprite_info)} sprites")
    return sprite_info

if __name__ == "__main__":
    sprite_info = create_sprite_metadata()