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 | #ifdef TOOLS_ENABLED |
34 | |
35 | #include "../gltf_document.h" |
36 | |
37 | #include "editor/editor_file_system.h" |
38 | #include "editor/editor_node.h" |
39 | #include "editor/gui/editor_file_dialog.h" |
40 | #include "scene/gui/popup_menu.h" |
41 | |
42 | String SceneExporterGLTFPlugin::get_name() const { |
43 | return "ConvertGLTF2" ; |
44 | } |
45 | |
46 | bool SceneExporterGLTFPlugin::has_main_screen() const { |
47 | return false; |
48 | } |
49 | |
50 | SceneExporterGLTFPlugin::SceneExporterGLTFPlugin() { |
51 | file_export_lib = memnew(EditorFileDialog); |
52 | EditorNode::get_singleton()->get_gui_base()->add_child(file_export_lib); |
53 | file_export_lib->connect("file_selected" , callable_mp(this, &SceneExporterGLTFPlugin::_gltf2_dialog_action)); |
54 | file_export_lib->set_title(TTR("Export Library" )); |
55 | file_export_lib->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE); |
56 | file_export_lib->set_access(EditorFileDialog::ACCESS_FILESYSTEM); |
57 | file_export_lib->clear_filters(); |
58 | file_export_lib->add_filter("*.glb" ); |
59 | file_export_lib->add_filter("*.gltf" ); |
60 | file_export_lib->set_title(TTR("Export Scene to glTF 2.0 File" )); |
61 | |
62 | PopupMenu * = get_export_as_menu(); |
63 | int idx = menu->get_item_count(); |
64 | menu->add_item(TTR("glTF 2.0 Scene..." )); |
65 | menu->set_item_metadata(idx, callable_mp(this, &SceneExporterGLTFPlugin::convert_scene_to_gltf2)); |
66 | } |
67 | |
68 | void SceneExporterGLTFPlugin::_gltf2_dialog_action(String p_file) { |
69 | Node *root = EditorNode::get_singleton()->get_tree()->get_edited_scene_root(); |
70 | if (!root) { |
71 | EditorNode::get_singleton()->show_accept(TTR("This operation can't be done without a scene." ), TTR("OK" )); |
72 | return; |
73 | } |
74 | List<String> deps; |
75 | Ref<GLTFDocument> doc; |
76 | doc.instantiate(); |
77 | Ref<GLTFState> state; |
78 | state.instantiate(); |
79 | int32_t flags = 0; |
80 | flags |= EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS; |
81 | Error err = doc->append_from_scene(root, state, flags); |
82 | if (err != OK) { |
83 | ERR_PRINT(vformat("glTF2 save scene error %s." , itos(err))); |
84 | } |
85 | err = doc->write_to_filesystem(state, p_file); |
86 | if (err != OK) { |
87 | ERR_PRINT(vformat("glTF2 save scene error %s." , itos(err))); |
88 | } |
89 | EditorFileSystem::get_singleton()->scan_changes(); |
90 | } |
91 | |
92 | void SceneExporterGLTFPlugin::convert_scene_to_gltf2() { |
93 | Node *root = EditorNode::get_singleton()->get_tree()->get_edited_scene_root(); |
94 | if (!root) { |
95 | EditorNode::get_singleton()->show_accept(TTR("This operation can't be done without a scene." ), TTR("OK" )); |
96 | return; |
97 | } |
98 | String filename = String(root->get_scene_file_path().get_file().get_basename()); |
99 | if (filename.is_empty()) { |
100 | filename = root->get_name(); |
101 | } |
102 | file_export_lib->set_current_file(filename + String(".gltf" )); |
103 | file_export_lib->popup_centered_ratio(); |
104 | } |
105 | |
106 | #endif // TOOLS_ENABLED |
107 | |