extract_mid_level.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import argparse
  2. import sys
  3. import json
  4. import shutil
  5. import subprocess
  6. from pathlib import Path
  7. # Add the utils directory to the Python path
  8. utils_path = Path(__file__).parent / 'utils'
  9. sys.path.append(str(utils_path))
  10. from file_utils import replicate_directory_structure, find_files_by_extension, create_guid_to_path_map
  11. def copy_scripts(assets_dir, output_assets_dir):
  12. """
  13. Copies all C# scripts (.cs) to the target directory.
  14. """
  15. print("\n--- Starting Script Handling ---")
  16. cs_files = find_files_by_extension(str(assets_dir), '.cs')
  17. print(f"Found {len(cs_files)} C# script files to copy.")
  18. for script_path_str in cs_files:
  19. script_path = Path(script_path_str)
  20. relative_path = script_path.relative_to(assets_dir)
  21. destination_path = output_assets_dir / relative_path
  22. destination_path.parent.mkdir(parents=True, exist_ok=True)
  23. try:
  24. shutil.copy(script_path, destination_path)
  25. except IOError as e:
  26. print(f"Error copying {script_path} to {destination_path}: {e}", file=sys.stderr)
  27. print("Script copying complete.")
  28. def main():
  29. """
  30. Main function to run the mid-level data extraction process.
  31. This script orchestrates the parsing of scene and prefab files.
  32. """
  33. parser = argparse.ArgumentParser(
  34. description="Generates a virtual representation of the project's structure."
  35. )
  36. parser.add_argument(
  37. "--input",
  38. type=str,
  39. required=True,
  40. help="The root directory of the target Unity project."
  41. )
  42. parser.add_argument(
  43. "--output",
  44. type=str,
  45. required=True,
  46. help="The directory where the generated output folder will be saved."
  47. )
  48. args = parser.parse_args()
  49. input_dir = Path(args.input).resolve()
  50. output_dir = Path(args.output).resolve()
  51. if not input_dir.is_dir():
  52. print(f"Error: Input path '{input_dir}' is not a valid directory.", file=sys.stderr)
  53. sys.exit(1)
  54. # --- Setup Output Directories ---
  55. mid_level_output_dir = output_dir / "MidLevel"
  56. output_assets_dir = mid_level_output_dir / "Assets"
  57. try:
  58. output_assets_dir.mkdir(parents=True, exist_ok=True)
  59. print(f"Output will be saved to: {mid_level_output_dir}")
  60. except OSError as e:
  61. print(f"Error: Could not create output directory '{mid_level_output_dir}'. {e}", file=sys.stderr)
  62. sys.exit(1)
  63. assets_dir = input_dir / "Assets"
  64. if not assets_dir.is_dir():
  65. print(f"Warning: 'Assets' directory not found in '{input_dir}'. Skipping all processing.", file=sys.stderr)
  66. return
  67. # --- Task 1: Replicate 'Assets' directory structure ---
  68. print(f"\n--- Replicating 'Assets' directory structure ---")
  69. replicate_directory_structure(str(assets_dir), str(output_assets_dir))
  70. print("Directory structure replication complete.")
  71. # --- Task 2: Copy C# Scripts ---
  72. copy_scripts(assets_dir, output_assets_dir)
  73. # --- Task 3: Generate GUID Map ---
  74. print("\n--- Generating GUID Map ---")
  75. guid_map = create_guid_to_path_map(str(input_dir))
  76. guid_map_path = mid_level_output_dir / "guid_map.json"
  77. try:
  78. with open(guid_map_path, 'w', encoding='utf-8') as f:
  79. json.dump(guid_map, f)
  80. print(f"Successfully created GUID map: {guid_map_path}")
  81. except IOError as e:
  82. print(f"Error writing GUID map: {e}", file=sys.stderr)
  83. sys.exit(1)
  84. # --- Task 4: Orchestrate Scene and Prefab Parsing ---
  85. print("\n--- Starting Scene/Prefab Parsing Orchestration ---")
  86. scene_files = find_files_by_extension(str(assets_dir), '.unity')
  87. prefab_files = find_files_by_extension(str(assets_dir), '.prefab')
  88. files_to_process = scene_files + prefab_files
  89. print(f"Found {len(files_to_process)} scene/prefab files to process.")
  90. script_path = Path(__file__).parent / "scene_processor.py"
  91. for file_path_str in files_to_process:
  92. file_path = Path(file_path_str)
  93. relative_path = file_path.relative_to(assets_dir)
  94. output_path = output_assets_dir / relative_path
  95. output_path = output_path.with_suffix('.json')
  96. command = [
  97. sys.executable,
  98. str(script_path),
  99. "--input",
  100. str(file_path),
  101. "--guid-map",
  102. str(guid_map_path),
  103. "--output",
  104. str(output_path)
  105. ]
  106. try:
  107. print(f"\n--- Calling processor for: {file_path.name} ---")
  108. subprocess.run(command, check=True, text=True)
  109. except subprocess.CalledProcessError as e:
  110. print(f"Error processing {file_path.name}. Subprocess failed with exit code {e.returncode}", file=sys.stderr)
  111. print(f"Stderr: {e.stderr}", file=sys.stderr)
  112. print(f"Stdout: {e.stdout}", file=sys.stderr)
  113. except FileNotFoundError:
  114. print(f"Error: Could not find the 'scene_processor.py' script at '{script_path}'", file=sys.stderr)
  115. sys.exit(1)
  116. print("\nMid-level extraction complete.")
  117. if __name__ == "__main__":
  118. main()