DataExtractorMenu.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Diagnostics;
  5. namespace LLM.Editor
  6. {
  7. public static class DataExtractorMenu
  8. {
  9. private const string HighLevelScript = "Assets/LLM/source/orchestrators/extract_high_level.py";
  10. private const string MidLevelScript = "Assets/LLM/source/orchestrators/extract_mid_level.py";
  11. private const string LowLevelScript = "Assets/LLM/source/orchestrators/extract_low_level.py";
  12. private const string CreateConfigScript = "Assets/LLM/source/orchestrators/create_config.py";
  13. private const string TRSExtractorScript = "Assets/LLM/source/orchestrators/extract_scene_trs.py";
  14. [MenuItem("Tools/DataExtractor/High Level Export")]
  15. private static void ExportHighLevel()
  16. {
  17. RunExtractor("High Level", HighLevelScript, true);
  18. }
  19. [MenuItem("Tools/DataExtractor/Mid Level Export")]
  20. private static void ExportMidLevel()
  21. {
  22. RunExtractor("Mid Level", MidLevelScript, true);
  23. }
  24. [MenuItem("Tools/DataExtractor/Low Level Export")]
  25. private static void ExportLowLevel()
  26. {
  27. RunExtractor("Low Level", LowLevelScript, true);
  28. }
  29. [MenuItem("Tools/DataExtractor/TRS Extractor")]
  30. private static void ExportTRS()
  31. {
  32. RunExtractor("TRS", TRSExtractorScript, true);
  33. }
  34. [MenuItem("Tools/DataExtractor/Create or Update Config File")]
  35. private static void CreateOrUpdateConfigFile()
  36. {
  37. RunExtractor("Create Config", CreateConfigScript, false);
  38. }
  39. private static void RunExtractor(string level, string scriptPath, bool requireOutputPath)
  40. {
  41. string outputPath = "";
  42. if (requireOutputPath)
  43. {
  44. outputPath = EditorUtility.OpenFolderPanel($"Select Output Folder for {level} Export", "", "");
  45. if (string.IsNullOrEmpty(outputPath))
  46. {
  47. UnityEngine.Debug.Log($"{level} export cancelled by user.");
  48. return;
  49. }
  50. }
  51. var projectRoot = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
  52. var pythonExecutable = Application.platform == RuntimePlatform.WindowsEditor ?
  53. Path.Combine(projectRoot, "venv", "Scripts", "python.exe") :
  54. Path.Combine(projectRoot, "venv", "bin", "python");
  55. var fullScriptPath = Path.Combine(projectRoot, scriptPath);
  56. if (!File.Exists(pythonExecutable))
  57. {
  58. UnityEngine.Debug.LogError($"Python executable not found at: {pythonExecutable}");
  59. EditorUtility.DisplayDialog("Export Error", "The Python virtual environment executable was not found. Please ensure the 'venv' directory is set up correctly.", "OK");
  60. return;
  61. }
  62. if (!File.Exists(fullScriptPath))
  63. {
  64. UnityEngine.Debug.LogError($"Extractor script not found at: {fullScriptPath}");
  65. EditorUtility.DisplayDialog("Export Error", $"The Python script for the {level} task was not found.", "OK");
  66. return;
  67. }
  68. var arguments = requireOutputPath ?
  69. $"\"{fullScriptPath}\" --input \"{projectRoot}\" --output \"{outputPath}\"" :
  70. $"\"{fullScriptPath}\" ";
  71. UnityEngine.Debug.Log($"Running command: \"{pythonExecutable}\" {arguments}");
  72. var process = new Process
  73. {
  74. StartInfo = new ProcessStartInfo
  75. {
  76. FileName = pythonExecutable,
  77. Arguments = arguments,
  78. RedirectStandardOutput = true,
  79. RedirectStandardError = true,
  80. UseShellExecute = false,
  81. CreateNoWindow = true,
  82. WorkingDirectory = projectRoot
  83. }
  84. };
  85. EditorUtility.DisplayProgressBar("Data Extractor", $"Running {level} task...", 0.5f);
  86. process.OutputDataReceived += (_, e) => { if (e.Data != null) UnityEngine.Debug.Log($"[Extractor] {e.Data}"); };
  87. process.ErrorDataReceived += (_, e) => { if (e.Data != null) UnityEngine.Debug.LogError($"[Extractor ERROR] {e.Data}"); };
  88. process.Start();
  89. process.BeginOutputReadLine();
  90. process.BeginErrorReadLine();
  91. process.WaitForExit();
  92. EditorUtility.ClearProgressBar();
  93. if (process.ExitCode == 0)
  94. {
  95. EditorUtility.DisplayDialog("Task Complete", $"{level} task finished successfully.", "OK");
  96. }
  97. else
  98. {
  99. EditorUtility.DisplayDialog("Task Failed", $"The {level} task failed. Check the Unity Console for error messages.", "OK");
  100. }
  101. }
  102. }
  103. }