editor_scene_exporter_gltf_plugin.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifdef TOOLS_ENABLED
  31. #include "editor_scene_exporter_gltf_plugin.h"
  32. #include "editor/editor_file_dialog.h"
  33. #include "editor/editor_file_system.h"
  34. #include "editor/editor_node.h"
  35. #include "scene/main/node.h"
  36. String SceneExporterGLTFPlugin::get_name() const {
  37. return "ConvertGLTF2";
  38. }
  39. bool SceneExporterGLTFPlugin::has_main_screen() const {
  40. return false;
  41. }
  42. SceneExporterGLTFPlugin::SceneExporterGLTFPlugin(EditorNode *p_node) {
  43. editor = p_node;
  44. convert_gltf2.instance();
  45. file_export_lib = memnew(EditorFileDialog);
  46. editor->get_gui_base()->add_child(file_export_lib);
  47. file_export_lib->connect("file_selected", this, "_gltf2_dialog_action");
  48. file_export_lib->set_title(TTR("Export Library"));
  49. file_export_lib->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  50. file_export_lib->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  51. file_export_lib->clear_filters();
  52. file_export_lib->add_filter("*.glb");
  53. file_export_lib->add_filter("*.gltf");
  54. file_export_lib->set_title(TTR("Export Mesh GLTF2"));
  55. String gltf_scene_name = TTR("Export GLTF...");
  56. add_tool_menu_item(gltf_scene_name, this, "convert_scene_to_gltf2", DEFVAL(Variant()));
  57. }
  58. void SceneExporterGLTFPlugin::_gltf2_dialog_action(String p_file) {
  59. Node *root = editor->get_tree()->get_edited_scene_root();
  60. if (!root) {
  61. editor->show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
  62. return;
  63. }
  64. List<String> deps;
  65. convert_gltf2->save_scene(root, p_file, p_file, 0, 1000.0f, &deps);
  66. EditorFileSystem::get_singleton()->scan_changes();
  67. }
  68. void SceneExporterGLTFPlugin::_bind_methods() {
  69. ClassDB::bind_method(D_METHOD("convert_scene_to_gltf2"), &SceneExporterGLTFPlugin::convert_scene_to_gltf2);
  70. ClassDB::bind_method(D_METHOD("_gltf2_dialog_action", "file"), &SceneExporterGLTFPlugin::_gltf2_dialog_action);
  71. }
  72. void SceneExporterGLTFPlugin::convert_scene_to_gltf2(Variant p_null) {
  73. Node *root = editor->get_tree()->get_edited_scene_root();
  74. if (!root) {
  75. editor->show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
  76. return;
  77. }
  78. String filename = String(root->get_filename().get_file().get_basename());
  79. if (filename.empty()) {
  80. filename = root->get_name();
  81. }
  82. file_export_lib->set_current_file(filename + ".gltf");
  83. file_export_lib->popup_centered_ratio();
  84. }
  85. #endif // TOOLS_ENABLED