extract_mid_level.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import argparse
  2. import sys
  3. import json
  4. import shutil
  5. from pathlib import Path
  6. # Add the utils directory to the Python path
  7. utils_path = Path(__file__).parent / 'utils'
  8. sys.path.append(str(utils_path))
  9. from file_utils import replicate_directory_structure, find_files_by_extension
  10. from virtual_tree_builder import build_virtual_tree
  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 Task 3: 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. # Ensure the destination directory exists
  23. destination_path.parent.mkdir(parents=True, exist_ok=True)
  24. try:
  25. shutil.copy(script_path, destination_path)
  26. except IOError as e:
  27. print(f"Error copying {script_path} to {destination_path}: {e}", file=sys.stderr)
  28. print("Script copying complete.")
  29. def main():
  30. """
  31. Main function to run the mid-level data extraction process.
  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. # --- Task 1: Create output folder and replicate structure ---
  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 assets_dir.is_dir():
  65. print(f"\n--- Replicating 'Assets' directory structure ---")
  66. replicate_directory_structure(str(assets_dir), str(output_assets_dir))
  67. print("Directory structure replication complete.")
  68. else:
  69. print(f"Warning: 'Assets' directory not found in '{input_dir}'. Skipping structure replication.", file=sys.stderr)
  70. return # Exit if there's no Assets directory
  71. # --- Task 3: Script Handling ---
  72. copy_scripts(assets_dir, output_assets_dir)
  73. # --- Task 4: Orchestration ---
  74. print("\n--- Starting Task 4: Orchestration ---")
  75. # Find all scene and prefab files
  76. scene_files = find_files_by_extension(str(assets_dir), '.unity')
  77. prefab_files = find_files_by_extension(str(assets_dir), '.prefab')
  78. files_to_process = scene_files + prefab_files
  79. print(f"Found {len(files_to_process)} scene/prefab files to process.")
  80. for file_path_str in files_to_process:
  81. file_path = Path(file_path_str)
  82. print(f"\nProcessing: {file_path.name}")
  83. # Generate the virtual tree
  84. virtual_tree = build_virtual_tree(str(file_path))
  85. if virtual_tree:
  86. # Construct the output path
  87. relative_path = file_path.relative_to(assets_dir)
  88. output_path = output_assets_dir / relative_path
  89. output_path = output_path.with_suffix('.json')
  90. # Create parent directories if they don't exist
  91. output_path.parent.mkdir(parents=True, exist_ok=True)
  92. # Save the JSON output
  93. try:
  94. with open(output_path, 'w', encoding='utf-8') as f:
  95. json.dump(virtual_tree, f, indent=4)
  96. print(f"Successfully created: {output_path}")
  97. except IOError as e:
  98. print(f"Error writing to {output_path}: {e}", file=sys.stderr)
  99. else:
  100. print(f"Skipped {file_path.name} as virtual tree generation failed.")
  101. print("\nMid-level extraction complete.")
  102. if __name__ == "__main__":
  103. main()