File size: 4,953 Bytes
e60cbb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os, ast, hashlib
from pathlib import Path
import json

ASSETS_PATH = Path(__file__).parent
ASSETS_PATH_ABS = Path(__file__).parent.resolve()


def extract_item_dict(file_path):
    try:
        with open(file_path, "r", encoding="utf-8") as f:
            content = f.read()

        tree = ast.parse(content)

        if len(tree.body) == 1 and isinstance(tree.body[0], ast.Expr) and isinstance(tree.body[0].value, ast.Dict):
            return ast.literal_eval(tree.body[0].value)

        return None

    except Exception as e:
        print(f"Error processing {file_path}: {e}")
        return None


def collect_all_items(item_file_list, base_path="."):
    """Collect ITEM dicts from all item.py files"""

    combined_dict = {}
    num = 0
    visualization_path = ASSETS_PATH / "extra" / "visualization"
    has_visualization = visualization_path.exists()
    for item_path in item_file_list:
        # Assuming item_path is a file path like "objects/carton/benchmark_carton_003/item.py"
        file_path = Path(base_path) / item_path
        usda_path = (Path(item_path).parent / "Aligned.usda").relative_to(ASSETS_PATH).as_posix()
        desc_path = (Path(base_path) / item_path).parent / "description.py"

        # Use the directory name as key, or customize as needed
        key = file_path.parent.name
        interaction_path = ASSETS_PATH / "interaction" / key / "interaction.json"
        # visualization paths (similar to interaction)
        visualization_video_path = visualization_path / key / "video" / "merged.mp4"
        visualization_snapshot_dir_path = visualization_path / key / "snapshot"
        if file_path.exists():
            item_dict = extract_item_dict(file_path)
            if item_dict:
                if key in combined_dict:
                    print("ERROR!!! Duplicated object detected!", file_path)
                    print("                                    ", usda_path)
                combined_dict[key] = item_dict
                combined_dict[key]["url"] = str(usda_path)
                if desc_path.exists():
                    desc_dict = extract_item_dict(desc_path)
                    if desc_dict:
                        combined_dict[key]["description"] = desc_dict
                else:
                    combined_dict[key]["description"] = {}
                    print("WARN !!! Empty LLM description!", file_path)
                # video path - from visualization folder
                if visualization_video_path.exists():
                    combined_dict[key]["video"] = str(visualization_video_path)
                else:
                    combined_dict[key]["video"] = ""
                    if has_visualization:
                        print("WARN !!! Empty video!", visualization_video_path)
                # snapshot dir path - from visualization folder
                if visualization_snapshot_dir_path.exists():
                    snapshot_files = list(visualization_snapshot_dir_path.glob("*"))
                    combined_dict[key]["snapshot"] = snapshot_files
                else:
                    combined_dict[key]["snapshot"] = []
                if len(combined_dict[key]["snapshot"]) == 0:
                    if has_visualization:
                        print("WARN !!! Empty snapshot!", visualization_snapshot_dir_path)
                # interaction
                if interaction_path.exists():
                    try:
                        interaction_dict = json.load(open(interaction_path))
                        combined_dict[key]["interaction"] = interaction_dict["interaction"]
                    except Exception as e:
                        # print(f"Error loading interaction.json: {e}")
                        combined_dict[key]["interaction"] = {}
                else:
                    combined_dict[key]["interaction"] = {}
                    # print("WARN !!! Empty interaction.json!", interaction_path)
            else:
                print(f"No ITEM dict found in {file_path}")
        else:
            print(f"File not found: {file_path}")

        num += 1

    if num != len(combined_dict):
        error = f"ERROR! there're items using the same name!!! {num} != {len(combined_dict)}"
        print(error)
        # raise Exception(error)

    return combined_dict


def find_all_items():
    base = Path(__file__).parent / "objects"
    for root, dirs, files in os.walk(base, followlinks=True):
        if "object_parameters.json" in files:
            yield Path(root) / "item.py"


########

print(f"Initializing GenieSim Asset Lib at", Path(__file__).parent)

# Collect all ITEM dicts
ASSETS_INDEX = collect_all_items(find_all_items())
ASSETS_PAYLOAD = "".join(f"{k}={v}\n" for k, v in sorted(ASSETS_INDEX.items())).encode("utf-8")
ASSETS_INDEX_HASH = hashlib.sha256(ASSETS_PAYLOAD).hexdigest()


print(f"Initialized GenieSim Asset Lib, Found {len(ASSETS_INDEX)} items, hash: {ASSETS_INDEX_HASH}")