| 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 | |
| 31 | #include "editor_scene_exporter_gltf_plugin.h" |
| 32 | |
| 33 | #include "editor_scene_exporter_gltf_settings.h" |
| 34 | |
| 35 | #include "editor/editor_node.h" |
| 36 | #include "editor/file_system/editor_file_system.h" |
| 37 | #include "editor/gui/editor_file_dialog.h" |
| 38 | #include "editor/import/3d/scene_import_settings.h" |
| 39 | #include "editor/inspector/editor_inspector.h" |
| 40 | #include "editor/themes/editor_scale.h" |
| 41 | #include "scene/gui/dialogs.h" |
| 42 | |
| 43 | String SceneExporterGLTFPlugin::get_plugin_name() const { |
| 44 | return "ConvertGLTF2"; |
| 45 | } |
| 46 | |
| 47 | bool SceneExporterGLTFPlugin::has_main_screen() const { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | SceneExporterGLTFPlugin::SceneExporterGLTFPlugin() { |
| 52 | _gltf_document.instantiate(); |
| 53 | // Set up the file dialog. |
| 54 | _file_dialog = memnew(EditorFileDialog); |
| 55 | _file_dialog->connect("file_selected", callable_mp(this, &SceneExporterGLTFPlugin::_popup_gltf_settings_dialog)); |
| 56 | _file_dialog->set_title(TTR("Export Library")); |
| 57 | _file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE); |
| 58 | _file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM); |
| 59 | _file_dialog->clear_filters(); |
| 60 | _file_dialog->add_filter("*.glb"); |
| 61 | _file_dialog->add_filter("*.gltf"); |
| 62 | _file_dialog->set_title(TTR("Export Scene to glTF 2.0 File")); |
| 63 | EditorNode::get_singleton()->get_gui_base()->add_child(_file_dialog); |
| 64 | // Set up the export settings menu. |
| 65 | _config_dialog = memnew(ConfirmationDialog); |
| 66 | _config_dialog->set_title(TTRC("Export Settings")); |
| 67 | EditorNode::get_singleton()->get_gui_base()->add_child(_config_dialog); |
| 68 | _config_dialog->connect(SceneStringName(confirmed), callable_mp(this, &SceneExporterGLTFPlugin::_export_scene_as_gltf)); |
| 69 | |
| 70 | _export_settings.instantiate(); |
| 71 | _export_settings->generate_property_list(_gltf_document); |
| 72 | _settings_inspector = memnew(EditorInspector); |
| 73 | _settings_inspector->set_custom_minimum_size(Size2(350, 300) * EDSCALE); |
| 74 | _config_dialog->add_child(_settings_inspector); |
| 75 | // Add a button to the Scene -> Export menu to pop up the settings dialog. |
| 76 | PopupMenu *menu = get_export_as_menu(); |
| 77 | int idx = menu->get_item_count(); |
| 78 | menu->add_item(TTRC("glTF 2.0 Scene...")); |
| 79 | menu->set_item_metadata(idx, callable_mp(this, &SceneExporterGLTFPlugin::_popup_gltf_export_dialog)); |
| 80 | } |
| 81 | |
| 82 | void SceneExporterGLTFPlugin::_popup_gltf_settings_dialog(const String &p_selected_path) { |
| 83 | export_path = p_selected_path; |
| 84 | |
| 85 | Node *root = EditorNode::get_singleton()->get_tree()->get_edited_scene_root(); |
| 86 | ERR_FAIL_NULL(root); |
| 87 | // Generate and refresh the export settings. |
| 88 | _export_settings->generate_property_list(_gltf_document, root); |
| 89 | _settings_inspector->edit(nullptr); |
| 90 | _settings_inspector->edit(_export_settings.ptr()); |
| 91 | // Show the config dialog. |
| 92 | _config_dialog->popup_centered(); |
| 93 | } |
| 94 | |
| 95 | void SceneExporterGLTFPlugin::_popup_gltf_export_dialog() { |
| 96 | Node *root = EditorNode::get_singleton()->get_tree()->get_edited_scene_root(); |
| 97 | if (!root) { |
| 98 | EditorNode::get_singleton()->show_warning(TTR("This operation can't be done without a scene.")); |
| 99 | return; |
| 100 | } |
| 101 | // Set the file dialog's file name to the scene name. |
| 102 | String filename = String(root->get_scene_file_path().get_file().get_basename()); |
| 103 | if (filename.is_empty()) { |
| 104 | filename = root->get_name(); |
| 105 | } |
| 106 | _file_dialog->set_current_file(filename + String(".gltf")); |
| 107 | // Show the file dialog. |
| 108 | _file_dialog->popup_file_dialog(); |
| 109 | } |
| 110 | |
| 111 | void SceneExporterGLTFPlugin::_export_scene_as_gltf() { |
| 112 | Node *root = EditorNode::get_singleton()->get_tree()->get_edited_scene_root(); |
| 113 | ERR_FAIL_NULL(root); |
| 114 | List<String> deps; |
| 115 | Ref<GLTFState> state; |
| 116 | state.instantiate(); |
| 117 | state->set_copyright(_export_settings->get_copyright()); |
| 118 | int32_t flags = 0; |
| 119 | flags |= EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS; |
| 120 | state->set_bake_fps(_export_settings->get_bake_fps()); |
| 121 | Error err = _gltf_document->append_from_scene(root, state, flags); |
| 122 | if (err != OK) { |
| 123 | ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err))); |
| 124 | } |
| 125 | err = _gltf_document->write_to_filesystem(state, export_path); |
| 126 | if (err != OK) { |
| 127 | ERR_PRINT(vformat("glTF2 save scene error %s.", itos(err))); |
| 128 | } |
| 129 | EditorFileSystem::get_singleton()->scan_changes(); |
| 130 | } |