Skip to content
0.2.0-dev Download ZIP
187 lines ·7.6 KB Raw
1/**************************************************************************/
2/* fbx_importer_manager.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 "fbx_importer_manager.h"
32
33#include "core/config/project_settings.h"
34#include "core/object/callable_mp.h"
35#include "core/os/os.h"
36#include "editor/editor_node.h"
37#include "editor/editor_string_names.h"
38#include "editor/settings/editor_settings.h"
39#include "editor/themes/editor_scale.h"
40#include "scene/gui/link_button.h"
41
42void FBXImporterManager::_notification(int p_what) {
43 switch (p_what) {
44 case NOTIFICATION_THEME_CHANGED: {
45 fbx_path_browse->set_button_icon(get_editor_theme_icon(SNAME("FileBrowse")));
46 } break;
47
48 case NOTIFICATION_READY: {
49 connect(SceneStringName(confirmed), callable_mp(this, &FBXImporterManager::_path_confirmed));
50 } break;
51 }
52}
53
54void FBXImporterManager::show_dialog(bool p_exclusive) {
55 String fbx2gltf_path = EDITOR_GET("filesystem/import/fbx/fbx2gltf_path");
56 fbx_path->set_text(fbx2gltf_path);
57 _validate_path(fbx2gltf_path);
58
59 // If exclusive, we're importing a FBX file, there's no exit.
60 is_importing = p_exclusive;
61 set_flag(Window::FLAG_BORDERLESS, p_exclusive); // Avoid closing accidentally.
62 set_close_on_escape(!p_exclusive);
63
64 if (is_importing) {
65 get_cancel_button()->set_text(TTR("Disable FBX2glTF & Restart"));
66 get_cancel_button()->set_tooltip_text(TTR("Canceling this dialog will disable the FBX2glTF importer and use the ufbx importer.\nYou can re-enable FBX2glTF in the Project Settings under Filesystem > Import > FBX > Enabled.\n\nThe editor will restart as importers are registered when the editor starts."));
67 } else {
68 get_cancel_button()->set_text(TTR("Cancel"));
69 get_cancel_button()->set_tooltip_text("");
70 }
71
72 popup_centered();
73}
74
75void FBXImporterManager::_validate_path(const String &p_path) {
76 String error;
77 bool success = false;
78
79 if (p_path == "") {
80 error = TTR("Path to FBX2glTF executable is empty.");
81 } else if (!FileAccess::exists(p_path)) {
82 error = TTR("Path to FBX2glTF executable is invalid.");
83 } else {
84 List<String> args;
85 args.push_back("--version");
86 int exitcode;
87 Error err = OS::get_singleton()->execute(p_path, args, nullptr, &exitcode);
88
89 if (err == OK && exitcode == 0) {
90 success = true;
91 } else {
92 error = TTR("Error executing this file (wrong version or architecture).");
93 }
94 }
95
96 if (success) {
97 path_status->set_text(TTR("FBX2glTF executable is valid."));
98 path_status->add_theme_color_override(SceneStringName(font_color), path_status->get_theme_color(SNAME("success_color"), EditorStringName(Editor)));
99 get_ok_button()->set_disabled(false);
100 } else {
101 path_status->set_text(error);
102 path_status->add_theme_color_override(SceneStringName(font_color), path_status->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
103 get_ok_button()->set_disabled(true);
104 }
105}
106
107void FBXImporterManager::_select_file(const String &p_path) {
108 fbx_path->set_text(p_path);
109 _validate_path(p_path);
110}
111
112void FBXImporterManager::_path_confirmed() {
113 String path = fbx_path->get_text();
114 EditorSettings::get_singleton()->set("filesystem/import/fbx/fbx2gltf_path", path);
115 EditorSettings::get_singleton()->save();
116}
117
118void FBXImporterManager::_cancel_setup() {
119 if (!is_importing) {
120 return; // No worry.
121 }
122 // No escape.
123 ProjectSettings::get_singleton()->set("filesystem/import/fbx2gltf/enabled", false);
124 ProjectSettings::get_singleton()->save();
125 EditorNode::get_singleton()->save_all_scenes();
126 EditorNode::get_singleton()->restart_editor();
127}
128
129void FBXImporterManager::_browse_install() {
130 if (fbx_path->get_text() != String()) {
131 browse_dialog->set_current_file(fbx_path->get_text());
132 }
133
134 browse_dialog->popup_centered_ratio();
135}
136
137FBXImporterManager *FBXImporterManager::singleton = nullptr;
138
139FBXImporterManager::FBXImporterManager() {
140 singleton = this;
141
142 set_title(TTR("Configure FBX Importer"));
143
144 VBoxContainer *vb = memnew(VBoxContainer);
145 vb->add_child(memnew(Label(TTR("FBX2glTF is required for importing FBX files if using FBX2glTF.\nAlternatively, you can use ufbx by disabling FBX2glTF.\nPlease download the necessary tool and provide a valid path to the binary:"))));
146 LinkButton *lb = memnew(LinkButton);
147 lb->set_text(TTR("Click this link to download FBX2glTF"));
148 lb->set_uri("https://godotengine.org/fbx-import");
149 vb->add_child(lb);
150
151 HBoxContainer *hb = memnew(HBoxContainer);
152
153 fbx_path = memnew(LineEdit);
154 fbx_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
155 fbx_path->set_accessibility_name(TTRC("Path"));
156 hb->add_child(fbx_path);
157 fbx_path_browse = memnew(Button);
158 fbx_path_browse->set_text(TTR("Browse"));
159 fbx_path_browse->connect(SceneStringName(pressed), callable_mp(this, &FBXImporterManager::_browse_install));
160 hb->add_child(fbx_path_browse);
161 hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
162 hb->set_custom_minimum_size(Size2(400 * EDSCALE, 0));
163
164 vb->add_child(hb);
165
166 path_status = memnew(Label);
167 path_status->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
168 vb->add_child(path_status);
169
170 add_child(vb);
171
172 fbx_path->connect(SceneStringName(text_changed), callable_mp(this, &FBXImporterManager::_validate_path));
173
174 get_ok_button()->set_text(TTR("Confirm Path"));
175 get_cancel_button()->connect(SceneStringName(pressed), callable_mp(this, &FBXImporterManager::_cancel_setup));
176
177 browse_dialog = memnew(EditorFileDialog);
178 browse_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
179 browse_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
180#ifdef WINDOWS_ENABLED
181 browse_dialog->add_filter("*.exe");
182#endif
183
184 browse_dialog->connect("file_selected", callable_mp(this, &FBXImporterManager::_select_file));
185
186 add_child(browse_dialog);
187}