editor_scene_exporter_gltf_plugin.cpp 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*************************************************************************/
  2. /* editor_scene_exporter_gltf_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #if TOOLS_ENABLED
  31. #include "editor_scene_exporter_gltf_plugin.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/error/error_list.h"
  34. #include "core/object/object.h"
  35. #include "core/templates/vector.h"
  36. #include "editor/editor_file_system.h"
  37. #include "gltf_document.h"
  38. #include "scene/3d/mesh_instance_3d.h"
  39. #include "scene/gui/check_box.h"
  40. #include "scene/main/node.h"
  41. #include "editor/editor_node.h"
  42. String SceneExporterGLTFPlugin::get_name() const {
  43. return "ConvertGLTF2";
  44. }
  45. bool SceneExporterGLTFPlugin::has_main_screen() const {
  46. return false;
  47. }
  48. SceneExporterGLTFPlugin::SceneExporterGLTFPlugin(EditorNode *p_node) {
  49. editor = p_node;
  50. file_export_lib = memnew(EditorFileDialog);
  51. editor->get_gui_base()->add_child(file_export_lib);
  52. file_export_lib->connect("file_selected", callable_mp(this, &SceneExporterGLTFPlugin::_gltf2_dialog_action));
  53. file_export_lib->set_title(TTR("Export Library"));
  54. file_export_lib->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  55. file_export_lib->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  56. file_export_lib->clear_filters();
  57. file_export_lib->add_filter("*.glb");
  58. file_export_lib->add_filter("*.gltf");
  59. file_export_lib->set_title(TTR("Export Mesh GLTF2"));
  60. String gltf_scene_name = TTR("Export GLTF...");
  61. add_tool_menu_item(gltf_scene_name, callable_mp(this, &SceneExporterGLTFPlugin::convert_scene_to_gltf2));
  62. }
  63. void SceneExporterGLTFPlugin::_gltf2_dialog_action(String p_file) {
  64. Node *root = editor->get_tree()->get_edited_scene_root();
  65. if (!root) {
  66. editor->show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
  67. return;
  68. }
  69. List<String> deps;
  70. Ref<GLTFDocument> doc;
  71. doc.instantiate();
  72. Error err = doc->save_scene(root, p_file, p_file, 0, 30.0f, Ref<GLTFState>());
  73. if (err != OK) {
  74. ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err)));
  75. }
  76. }
  77. void SceneExporterGLTFPlugin::convert_scene_to_gltf2() {
  78. Node *root = editor->get_tree()->get_edited_scene_root();
  79. if (!root) {
  80. editor->show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
  81. return;
  82. }
  83. String filename = String(root->get_scene_file_path().get_file().get_basename());
  84. if (filename.is_empty()) {
  85. filename = root->get_name();
  86. }
  87. file_export_lib->set_current_file(filename + String(".gltf"));
  88. file_export_lib->popup_centered_ratio();
  89. }
  90. #endif // TOOLS_ENABLED