ProjectExporterController.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine;
  7. namespace ProjectExporter
  8. {
  9. public class ProjectExporterController
  10. {
  11. private const string PythonExecutable = "/Users/sujithpudussery/Documents/UnityProjects/terra-llm/venv/bin/python3";
  12. private const string ConversionScript = "/Users/sujithpudussery/Documents/UnityProjects/terra-llm/Assets/ProjectExporter/source/convert_scene.py";
  13. private static readonly string ProjectRoot = Path.GetDirectoryName(Application.dataPath);
  14. private static readonly string OutputDirectory = Path.Combine(ProjectRoot, "Library", "ExportedJSON");
  15. // Finds assets based on a search filter (e.g., "t:Scene") and builds a hierarchical model.
  16. public AssetModel GetAssets(string filter)
  17. {
  18. var root = new AssetModel("Assets", "Assets");
  19. var assetGuids = AssetDatabase.FindAssets(filter, new[] { "Assets" });
  20. foreach (var guid in assetGuids)
  21. {
  22. var path = AssetDatabase.GUIDToAssetPath(guid);
  23. if (filter.Contains("t:ScriptableObject") && !path.EndsWith(".asset"))
  24. {
  25. continue;
  26. }
  27. var parts = path.Split('/');
  28. var currentNode = root;
  29. for (int i = 1; i < parts.Length; i++) // Start at 1 to skip "Assets"
  30. {
  31. var part = parts[i];
  32. var child = currentNode.Children.FirstOrDefault(c => c.Name == part);
  33. if (child == null)
  34. {
  35. var currentPath = string.Join("/", parts.Take(i + 1));
  36. child = new AssetModel(currentPath, part);
  37. currentNode.Children.Add(child);
  38. }
  39. currentNode = child;
  40. }
  41. }
  42. return root;
  43. }
  44. // Lists all files in the ProjectSettings directory.
  45. public AssetModel GetProjectSettingsFiles()
  46. {
  47. var settingsRootPath = Path.Combine(ProjectRoot, "ProjectSettings");
  48. var root = new AssetModel(settingsRootPath, "ProjectSettings");
  49. if (Directory.Exists(settingsRootPath))
  50. {
  51. // Filter to only include *.asset files.
  52. var files = Directory.GetFiles(settingsRootPath, "*.asset", SearchOption.AllDirectories);
  53. foreach (var file in files)
  54. {
  55. var relativePath = Path.GetRelativePath(ProjectRoot, file);
  56. root.Children.Add(new AssetModel(relativePath, Path.GetFileName(file)));
  57. }
  58. }
  59. return root;
  60. }
  61. // Scans the entire Assets folder for .meta files and builds a hierarchical model.
  62. public AssetModel GetAllMetaFiles()
  63. {
  64. var assetsRootPath = Application.dataPath; // This is the "Assets" folder
  65. var root = new AssetModel("Assets", "Assets");
  66. var files = Directory.GetFiles(assetsRootPath, "*.meta", SearchOption.AllDirectories);
  67. foreach (var file in files)
  68. {
  69. var relativePath = "Assets/" + Path.GetRelativePath(assetsRootPath, file).Replace('\\', '/');
  70. var parts = relativePath.Split('/');
  71. var currentNode = root;
  72. for (int i = 1; i < parts.Length; i++) // Start at 1 to skip "Assets"
  73. {
  74. var part = parts[i];
  75. var child = currentNode.Children.FirstOrDefault(c => c.Name == part);
  76. if (child == null)
  77. {
  78. var currentPath = string.Join("/", parts.Take(i + 1));
  79. child = new AssetModel(currentPath, part);
  80. currentNode.Children.Add(child);
  81. }
  82. currentNode = child;
  83. }
  84. }
  85. return root;
  86. }
  87. // Executes the Python conversion script for the selected assets.
  88. public void ExportAssets(AssetModel rootNode)
  89. {
  90. var assetsToExport = new List<string>();
  91. CollectSelectedAssets(rootNode, assetsToExport);
  92. EditorUtility.DisplayProgressBar("Exporting...", "Starting export process...", 0f);
  93. try
  94. {
  95. for (int i = 0; i < assetsToExport.Count; i++)
  96. {
  97. var relativeAssetPath = assetsToExport[i];
  98. var progress = (float)i / assetsToExport.Count;
  99. EditorUtility.DisplayProgressBar("Exporting...", $"Processing {Path.GetFileName(relativeAssetPath)}", progress);
  100. var absoluteAssetPath = Path.Combine(ProjectRoot, relativeAssetPath);
  101. var outputJsonPath = Path.Combine(OutputDirectory, relativeAssetPath + ".json");
  102. var process = new Process
  103. {
  104. StartInfo = new ProcessStartInfo
  105. {
  106. FileName = PythonExecutable,
  107. Arguments = $"\"{ConversionScript}\" \"{absoluteAssetPath}\" \"{outputJsonPath}\"",
  108. RedirectStandardOutput = true,
  109. RedirectStandardError = true,
  110. UseShellExecute = false,
  111. CreateNoWindow = true
  112. }
  113. };
  114. process.Start();
  115. string output = process.StandardOutput.ReadToEnd();
  116. string error = process.StandardError.ReadToEnd();
  117. process.WaitForExit();
  118. if (process.ExitCode != 0)
  119. {
  120. UnityEngine.Debug.LogError($"Error exporting {relativeAssetPath}: {error}");
  121. }
  122. else
  123. {
  124. UnityEngine.Debug.Log($"Successfully exported {relativeAssetPath}: {output}");
  125. }
  126. }
  127. }
  128. finally
  129. {
  130. EditorUtility.ClearProgressBar();
  131. }
  132. }
  133. // Recursively collects the paths of all selected assets.
  134. private void CollectSelectedAssets(AssetModel node, List<string> selectedPaths)
  135. {
  136. // If the node itself is a file and is selected, add it.
  137. if (node.Children.Count == 0 && node.IsSelected)
  138. {
  139. selectedPaths.Add(node.Path);
  140. }
  141. // Recurse into children
  142. foreach (var child in node.Children)
  143. {
  144. CollectSelectedAssets(child, selectedPaths);
  145. }
  146. }
  147. }
  148. }