parse_generic_assets.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import argparse
  2. import sys
  3. from pathlib import Path
  4. # Add the utils directory to the Python path
  5. utils_path = Path(__file__).parent.parent / 'utils'
  6. sys.path.append(str(utils_path))
  7. from file_utils import find_files_by_extension
  8. from json_utils import write_json
  9. from yaml_utils import load_unity_yaml, convert_to_plain_python_types
  10. def main():
  11. parser = argparse.ArgumentParser(description="Parses generic YAML-based assets (.mat, .controller, .anim) into JSON.")
  12. parser.add_argument("--input", type=str, required=True, help="The root directory of the target Unity project.")
  13. parser.add_argument("--output", type=str, required=True, help="The directory where the output will be saved.")
  14. parser.add_argument("--indent", type=int, default=None)
  15. args = parser.parse_args()
  16. input_dir = Path(args.input).resolve()
  17. output_dir = Path(args.output).resolve()
  18. assets_dir = input_dir / "Assets"
  19. output_assets_dir = output_dir / "Assets"
  20. if not assets_dir.is_dir():
  21. return
  22. extensions = ['.mat', '.controller', '.anim']
  23. files_to_process = []
  24. for ext in extensions:
  25. files_to_process.extend(find_files_by_extension(str(assets_dir), ext))
  26. if not files_to_process:
  27. print("No generic asset files (.mat, .controller, .anim) found.")
  28. return
  29. print(f"\n--- Starting Generic Asset Parsing ---")
  30. print(f"Found {len(files_to_process)} files to process.")
  31. for file_path_str in files_to_process:
  32. file_path = Path(file_path_str)
  33. relative_path = file_path.relative_to(assets_dir)
  34. output_path = (output_assets_dir / relative_path).with_suffix('.json')
  35. output_path.parent.mkdir(parents=True, exist_ok=True)
  36. try:
  37. documents = load_unity_yaml(file_path)
  38. if documents:
  39. # Most asset files have one document
  40. data = convert_to_plain_python_types(documents[0])
  41. write_json(data, output_path, indent=args.indent)
  42. except Exception as e:
  43. print(f"Error processing {file_path.name}: {e}", file=sys.stderr)
  44. print("Generic asset parsing complete.")
  45. if __name__ == "__main__":
  46. main()