editor_scene_exporter_gltf_plugin.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "editor_scene_exporter_gltf_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/object/object.h"
  33. #include "core/templates/vector.h"
  34. #include "editor/editor_file_system.h"
  35. #include "scene/3d/mesh_instance_3d.h"
  36. #include "scene/gui/check_box.h"
  37. #include "scene/main/node.h"
  38. #include "editor/editor_node.h"
  39. String SceneExporterGLTFPlugin::get_name() const {
  40. return "ConvertGLTF2";
  41. }
  42. bool SceneExporterGLTFPlugin::has_main_screen() const {
  43. return false;
  44. }
  45. SceneExporterGLTFPlugin::SceneExporterGLTFPlugin(EditorNode *p_node) {
  46. editor = p_node;
  47. convert_gltf2.instance();
  48. file_export_lib = memnew(EditorFileDialog);
  49. editor->get_gui_base()->add_child(file_export_lib);
  50. file_export_lib->connect("file_selected", callable_mp(this, &SceneExporterGLTFPlugin::_gltf2_dialog_action));
  51. file_export_lib->set_title(TTR("Export Library"));
  52. file_export_lib->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  53. file_export_lib->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  54. file_export_lib->clear_filters();
  55. file_export_lib->add_filter("*.glb");
  56. file_export_lib->add_filter("*.gltf");
  57. file_export_lib->set_title(TTR("Export Mesh GLTF2"));
  58. String gltf_scene_name = TTR("Export GLTF...");
  59. add_tool_menu_item(gltf_scene_name, callable_mp(this, &SceneExporterGLTFPlugin::convert_scene_to_gltf2));
  60. }
  61. void SceneExporterGLTFPlugin::_gltf2_dialog_action(String p_file) {
  62. Node *root = editor->get_tree()->get_edited_scene_root();
  63. if (!root) {
  64. editor->show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
  65. return;
  66. }
  67. List<String> deps;
  68. convert_gltf2->save_scene(root, p_file, p_file, 0, 1000.0f, &deps);
  69. EditorFileSystem::get_singleton()->scan_changes();
  70. }
  71. void SceneExporterGLTFPlugin::convert_scene_to_gltf2() {
  72. Node *root = editor->get_tree()->get_edited_scene_root();
  73. if (!root) {
  74. editor->show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
  75. return;
  76. }
  77. String filename = String(root->get_filename().get_file().get_basename());
  78. if (filename.is_empty()) {
  79. filename = root->get_name();
  80. }
  81. file_export_lib->set_current_file(filename + String(".gltf"));
  82. file_export_lib->popup_centered_ratio();
  83. }