EditorExport.as 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. void ExportSceneToOBJ(String fileName)
  2. {
  3. if (fileName.empty)
  4. {
  5. MessageBox("File name for OBJ export unspecified");
  6. return;
  7. }
  8. // append obj extension if missing
  9. if (GetExtension(fileName).empty)
  10. fileName += ".obj";
  11. Octree@ octree = scene.GetComponent("Octree");
  12. if (octree is null)
  13. {
  14. MessageBox("Octree missing from scene");
  15. return;
  16. }
  17. Array<Drawable@> drawables = octree.GetAllDrawables();
  18. if (drawables.length == 0)
  19. {
  20. MessageBox("No drawables to export in the scene");
  21. return;
  22. }
  23. RemoveDebugDrawables(drawables);
  24. File@ file = File(fileName, FILE_WRITE);
  25. if (WriteDrawablesToOBJ(drawables, file))
  26. {
  27. MessageBox("OBJ file written to " + fileName, "Success");
  28. file.Close();
  29. }
  30. else
  31. {
  32. // Cleanup our file so we don't mislead anyone
  33. MessageBox("Unable to write OBJ file");
  34. file.Close();
  35. fileSystem.Delete(fileName);
  36. }
  37. }
  38. void ExportSelectedToOBJ(String fileName)
  39. {
  40. if (fileName.empty)
  41. {
  42. MessageBox("File name for OBJ export unspecified");
  43. return;
  44. }
  45. if (GetExtension(fileName).empty)
  46. fileName += ".obj";
  47. Array<Drawable@> drawables;
  48. // Add any explicitly selected drawables
  49. for (uint i = 0; i < selectedComponents.length; ++i)
  50. {
  51. Drawable@ drawable = cast<Drawable>(selectedComponents[i]);
  52. if (drawable !is null)
  53. drawables.Push(drawable);
  54. }
  55. // Add drawables of any selected nodes
  56. for (uint i = 0; i < selectedNodes.length; ++i)
  57. {
  58. Array<Component@>@ components = selectedNodes[i].GetComponents();
  59. for (uint comp = 0; comp < components.length; ++comp)
  60. {
  61. Drawable@ drawable = cast<Drawable>(components[comp]);
  62. if (drawable !is null && drawables.FindByRef(drawable) < 0)
  63. drawables.Push(drawable);
  64. }
  65. }
  66. RemoveDebugDrawables(drawables);
  67. if (drawables.length > 0)
  68. {
  69. File@ file = File(fileName, FILE_WRITE);
  70. if (WriteDrawablesToOBJ(drawables, file))
  71. {
  72. MessageBox("OBJ file written to " + fileName, "Success");
  73. file.Close();
  74. }
  75. else
  76. {
  77. MessageBox("Unable to write OBJ file");
  78. // Cleanup our file so we don't mislead anyone
  79. file.Close();
  80. fileSystem.Delete(fileName);
  81. }
  82. }
  83. else
  84. {
  85. MessageBox("No selected drawables to export to OBJ");
  86. }
  87. }
  88. void RemoveDebugDrawables(Array<Drawable@>@ drawables)
  89. {
  90. for (uint i = 0; i < drawables.length;)
  91. {
  92. if (drawables[i].node !is null && (drawables[i].node.name == "EditorGizmo" || drawables[i].node.name == "DebugIconsContainer"
  93. || drawables[i].node.name == "EditorGrid"))
  94. drawables.Erase(i);
  95. else
  96. ++i;
  97. }
  98. }