EditorExport.as 3.4 KB

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