|
@@ -1,6 +1,7 @@
|
|
|
import argparse
|
|
|
import sys
|
|
|
import json
|
|
|
+import shutil
|
|
|
from pathlib import Path
|
|
|
|
|
|
# Add the utils directory to the Python path
|
|
@@ -13,6 +14,28 @@ from json_utils import write_json
|
|
|
from yaml_utils import load_unity_yaml, convert_to_plain_python_types
|
|
|
from hierarchy_utils import HierarchyParser
|
|
|
|
|
|
+def copy_scripts(assets_dir, output_assets_dir):
|
|
|
+ """
|
|
|
+ Copies all C# scripts (.cs) to the target directory.
|
|
|
+ """
|
|
|
+ print("\n--- Starting Script Handling ---")
|
|
|
+ cs_files = find_files_by_extension(str(assets_dir), '.cs')
|
|
|
+ print(f"Found {len(cs_files)} C# script files to copy.")
|
|
|
+
|
|
|
+ for script_path_str in cs_files:
|
|
|
+ script_path = Path(script_path_str)
|
|
|
+ relative_path = script_path.relative_to(assets_dir)
|
|
|
+ destination_path = output_assets_dir / relative_path
|
|
|
+
|
|
|
+ destination_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
+
|
|
|
+ try:
|
|
|
+ shutil.copy(script_path, destination_path)
|
|
|
+ except IOError as e:
|
|
|
+ print(f"Error copying {script_path} to {destination_path}: {e}", file=sys.stderr)
|
|
|
+
|
|
|
+ print("Script copying complete.")
|
|
|
+
|
|
|
def main():
|
|
|
"""
|
|
|
Main function to run the low-level data extraction process.
|
|
@@ -49,8 +72,9 @@ def main():
|
|
|
|
|
|
# Create the main output folder, named "LowLevel"
|
|
|
low_level_output_dir = output_dir / "LowLevel"
|
|
|
+ output_assets_dir = low_level_output_dir / "Assets"
|
|
|
try:
|
|
|
- low_level_output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
+ output_assets_dir.mkdir(parents=True, exist_ok=True)
|
|
|
print(f"Output will be saved to: {low_level_output_dir}")
|
|
|
except OSError as e:
|
|
|
print(f"Error: Could not create output directory '{low_level_output_dir}'. {e}", file=sys.stderr)
|
|
@@ -62,6 +86,10 @@ def main():
|
|
|
print(f"Error: 'Assets' directory not found in '{input_dir}'.", file=sys.stderr)
|
|
|
return
|
|
|
|
|
|
+ # --- Task 1: Copy C# Scripts ---
|
|
|
+ copy_scripts(assets_dir, output_assets_dir)
|
|
|
+
|
|
|
+ # --- Task 2: Process Scenes and Prefabs ---
|
|
|
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
|
|
@@ -70,7 +98,7 @@ def main():
|
|
|
|
|
|
for file_path_str in files_to_process:
|
|
|
file_path = Path(file_path_str)
|
|
|
- print(f"\nProcessing: {file_path.name}")
|
|
|
+ print(f"\nProcessing Scene/Prefab: {file_path.name}")
|
|
|
|
|
|
# --- Deep Parsing for Individual GameObjects ---
|
|
|
gameobject_list = parse_scene_or_prefab(str(file_path))
|
|
@@ -110,7 +138,6 @@ def main():
|
|
|
parser = HierarchyParser(object_map)
|
|
|
root_object_ids = parser.get_root_object_ids()
|
|
|
|
|
|
- # Extract just the fileIDs for the root_objects.json file
|
|
|
root_ids_list = [file_id for file_id, _ in root_object_ids]
|
|
|
|
|
|
if root_ids_list:
|
|
@@ -121,6 +148,51 @@ def main():
|
|
|
except Exception as e:
|
|
|
print(f"Error during hierarchy parsing for {file_path.name}: {e}", file=sys.stderr)
|
|
|
|
|
|
+ # --- Task 3: Process .asset files ---
|
|
|
+ asset_files = find_files_by_extension(str(assets_dir), '.asset')
|
|
|
+ print(f"\nFound {len(asset_files)} .asset files to process.")
|
|
|
+
|
|
|
+ for file_path_str in asset_files:
|
|
|
+ file_path = Path(file_path_str)
|
|
|
+ print(f"\nProcessing Asset: {file_path.name}")
|
|
|
+
|
|
|
+ relative_path = file_path.relative_to(input_dir)
|
|
|
+ asset_output_dir = low_level_output_dir / relative_path
|
|
|
+ try:
|
|
|
+ asset_output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
+ except OSError as e:
|
|
|
+ print(f"Error creating directory {asset_output_dir}: {e}", file=sys.stderr)
|
|
|
+ continue
|
|
|
+
|
|
|
+ try:
|
|
|
+ documents = load_unity_yaml(file_path)
|
|
|
+ if not documents:
|
|
|
+ print(f"Skipped {file_path.name} as it's empty or could not be parsed.")
|
|
|
+ continue
|
|
|
+
|
|
|
+ print(f"Saving {len(documents)} objects from {file_path.name} to {asset_output_dir}")
|
|
|
+ for doc in documents:
|
|
|
+ if not hasattr(doc, 'anchor') or doc.anchor is None:
|
|
|
+ continue
|
|
|
+
|
|
|
+ file_id = int(doc.anchor.value)
|
|
|
+ obj_data = convert_to_plain_python_types(doc)
|
|
|
+
|
|
|
+ final_obj_data = {}
|
|
|
+ for key, value in obj_data.items():
|
|
|
+ if isinstance(value, dict):
|
|
|
+ new_value = value.copy()
|
|
|
+ new_value['fileID'] = file_id
|
|
|
+ final_obj_data[key] = new_value
|
|
|
+ else:
|
|
|
+ final_obj_data[key] = value
|
|
|
+
|
|
|
+ output_json_path = asset_output_dir / f"{file_id}.json"
|
|
|
+ write_json(final_obj_data, output_json_path, indent=args.indent)
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ print(f"Error processing asset file {file_path.name}: {e}", file=sys.stderr)
|
|
|
+
|
|
|
|
|
|
print("\nLow-level extraction complete.")
|
|
|
|