123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import argparse
- import sys
- from pathlib import Path
- # Add the utils directory to the Python path
- utils_path = Path(__file__).parent.parent / 'utils'
- sys.path.append(str(utils_path))
- from file_utils import find_files_by_extension
- from deep_parser import parse_scene_or_prefab
- from json_utils import write_json
- from yaml_utils import load_unity_yaml, convert_to_plain_python_types
- from hierarchy_utils import HierarchyParser
- def main():
- parser = argparse.ArgumentParser(description="Parses scenes and prefabs into a per-GameObject breakdown.")
- parser.add_argument("--input", type=str, required=True, help="The root directory of the target Unity project.")
- parser.add_argument("--output", type=str, required=True, help="The directory where the output will be saved.")
- parser.add_argument("--indent", type=int, default=None)
- args = parser.parse_args()
- input_dir = Path(args.input).resolve()
- output_dir = Path(args.output).resolve()
- assets_dir = input_dir / "Assets"
- if not assets_dir.is_dir():
- return
- scene_files = find_files_by_extension(str(assets_dir), '.unity')
- prefab_files = find_files_by_extension(str(assets_dir), '.prefab')
- files_to_process = scene_files + prefab_files
- if not files_to_process:
- print("No scene or prefab files found.")
- return
-
- print(f"\n--- Starting Scene/Prefab Parsing ---")
- print(f"Found {len(files_to_process)} files to process.")
- for file_path_str in files_to_process:
- file_path = Path(file_path_str)
- print(f"\nProcessing: {file_path.name}")
- gameobject_list = parse_scene_or_prefab(str(file_path))
- relative_path = file_path.relative_to(input_dir)
- asset_output_dir = output_dir / relative_path
- asset_output_dir.mkdir(parents=True, exist_ok=True)
- if gameobject_list:
- print(f"Saving {len(gameobject_list)} GameObjects to {asset_output_dir}")
- for go_data in gameobject_list:
- file_id = go_data.get('fileID')
- if file_id:
- output_json_path = asset_output_dir / f"{file_id}.json"
- write_json(go_data, output_json_path, indent=args.indent)
- else:
- print(f"Skipped deep parsing for {file_path.name}.")
- try:
- documents = load_unity_yaml(file_path)
- if not documents:
- continue
- raw_object_map = {int(doc.anchor.value): doc for doc in documents if hasattr(doc, 'anchor') and doc.anchor is not None}
- object_map = {file_id: convert_to_plain_python_types(obj) for file_id, obj in raw_object_map.items()}
- parser = HierarchyParser(object_map)
- root_object_ids = parser.get_root_object_ids()
-
- root_ids_list = [file_id for file_id, _ in root_object_ids]
- if root_ids_list:
- roots_output_path = asset_output_dir / "root_objects.json"
- write_json(root_ids_list, roots_output_path, indent=args.indent)
- print(f"Successfully saved root object list to {roots_output_path}")
- except Exception as e:
- print(f"Error during hierarchy parsing for {file_path.name}: {e}", file=sys.stderr)
- print("Scene and prefab parsing complete.")
- if __name__ == "__main__":
- main()
|