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