Path: blob/master/editor/import/fbx_importer_manager.cpp
9902 views
/**************************************************************************/1/* fbx_importer_manager.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "fbx_importer_manager.h"3132#include "core/config/project_settings.h"33#include "editor/editor_node.h"34#include "editor/editor_string_names.h"35#include "editor/settings/editor_settings.h"36#include "editor/themes/editor_scale.h"37#include "scene/gui/link_button.h"3839void FBXImporterManager::_notification(int p_what) {40switch (p_what) {41case NOTIFICATION_THEME_CHANGED: {42fbx_path_browse->set_button_icon(get_editor_theme_icon(SNAME("FileBrowse")));43} break;4445case NOTIFICATION_READY: {46connect(SceneStringName(confirmed), callable_mp(this, &FBXImporterManager::_path_confirmed));47} break;48}49}5051void FBXImporterManager::show_dialog(bool p_exclusive) {52String fbx2gltf_path = EDITOR_GET("filesystem/import/fbx/fbx2gltf_path");53fbx_path->set_text(fbx2gltf_path);54_validate_path(fbx2gltf_path);5556// If exclusive, we're importing a FBX file, there's no exit.57is_importing = p_exclusive;58set_flag(Window::FLAG_BORDERLESS, p_exclusive); // Avoid closing accidentally.59set_close_on_escape(!p_exclusive);6061if (is_importing) {62get_cancel_button()->set_text(TTR("Disable FBX2glTF & Restart"));63get_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."));64} else {65get_cancel_button()->set_text(TTR("Cancel"));66get_cancel_button()->set_tooltip_text("");67}6869popup_centered();70}7172void FBXImporterManager::_validate_path(const String &p_path) {73String error;74bool success = false;7576if (p_path == "") {77error = TTR("Path to FBX2glTF executable is empty.");78} else if (!FileAccess::exists(p_path)) {79error = TTR("Path to FBX2glTF executable is invalid.");80} else {81List<String> args;82args.push_back("--version");83int exitcode;84Error err = OS::get_singleton()->execute(p_path, args, nullptr, &exitcode);8586if (err == OK && exitcode == 0) {87success = true;88} else {89error = TTR("Error executing this file (wrong version or architecture).");90}91}9293if (success) {94path_status->set_text(TTR("FBX2glTF executable is valid."));95path_status->add_theme_color_override(SceneStringName(font_color), path_status->get_theme_color(SNAME("success_color"), EditorStringName(Editor)));96get_ok_button()->set_disabled(false);97} else {98path_status->set_text(error);99path_status->add_theme_color_override(SceneStringName(font_color), path_status->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));100get_ok_button()->set_disabled(true);101}102}103104void FBXImporterManager::_select_file(const String &p_path) {105fbx_path->set_text(p_path);106_validate_path(p_path);107}108109void FBXImporterManager::_path_confirmed() {110String path = fbx_path->get_text();111EditorSettings::get_singleton()->set("filesystem/import/fbx/fbx2gltf_path", path);112EditorSettings::get_singleton()->save();113}114115void FBXImporterManager::_cancel_setup() {116if (!is_importing) {117return; // No worry.118}119// No escape.120ProjectSettings::get_singleton()->set("filesystem/import/fbx2gltf/enabled", false);121ProjectSettings::get_singleton()->save();122EditorNode::get_singleton()->save_all_scenes();123EditorNode::get_singleton()->restart_editor();124}125126void FBXImporterManager::_browse_install() {127if (fbx_path->get_text() != String()) {128browse_dialog->set_current_file(fbx_path->get_text());129}130131browse_dialog->popup_centered_ratio();132}133134FBXImporterManager *FBXImporterManager::singleton = nullptr;135136FBXImporterManager::FBXImporterManager() {137singleton = this;138139set_title(TTR("Configure FBX Importer"));140141VBoxContainer *vb = memnew(VBoxContainer);142vb->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:"))));143LinkButton *lb = memnew(LinkButton);144lb->set_text(TTR("Click this link to download FBX2glTF"));145lb->set_uri("https://godotengine.org/fbx-import");146vb->add_child(lb);147148HBoxContainer *hb = memnew(HBoxContainer);149150fbx_path = memnew(LineEdit);151fbx_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);152fbx_path->set_accessibility_name(TTRC("Path"));153hb->add_child(fbx_path);154fbx_path_browse = memnew(Button);155fbx_path_browse->set_text(TTR("Browse"));156fbx_path_browse->connect(SceneStringName(pressed), callable_mp(this, &FBXImporterManager::_browse_install));157hb->add_child(fbx_path_browse);158hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);159hb->set_custom_minimum_size(Size2(400 * EDSCALE, 0));160161vb->add_child(hb);162163path_status = memnew(Label);164path_status->set_focus_mode(Control::FOCUS_ACCESSIBILITY);165vb->add_child(path_status);166167add_child(vb);168169fbx_path->connect(SceneStringName(text_changed), callable_mp(this, &FBXImporterManager::_validate_path));170171get_ok_button()->set_text(TTR("Confirm Path"));172get_cancel_button()->connect(SceneStringName(pressed), callable_mp(this, &FBXImporterManager::_cancel_setup));173174browse_dialog = memnew(EditorFileDialog);175browse_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);176browse_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);177#ifdef WINDOWS_ENABLED178browse_dialog->add_filter("*.exe");179#endif180181browse_dialog->connect("file_selected", callable_mp(this, &FBXImporterManager::_select_file));182183add_child(browse_dialog);184}185186187