scene_import_settings.cpp 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  1. /**************************************************************************/
  2. /* scene_import_settings.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. #include "scene_import_settings.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_file_system.h"
  33. #include "editor/editor_inspector.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/gui/editor_file_dialog.h"
  38. #include "editor/plugins/skeleton_3d_editor_plugin.h"
  39. #include "editor/themes/editor_scale.h"
  40. #include "scene/3d/importer_mesh_instance_3d.h"
  41. #include "scene/animation/animation_player.h"
  42. #include "scene/gui/subviewport_container.h"
  43. #include "scene/resources/3d/importer_mesh.h"
  44. #include "scene/resources/surface_tool.h"
  45. class SceneImportSettingsData : public Object {
  46. GDCLASS(SceneImportSettingsData, Object)
  47. friend class SceneImportSettingsDialog;
  48. HashMap<StringName, Variant> *settings = nullptr;
  49. HashMap<StringName, Variant> current;
  50. HashMap<StringName, Variant> defaults;
  51. List<ResourceImporter::ImportOption> options;
  52. Vector<String> animation_list;
  53. bool hide_options = false;
  54. String path;
  55. ResourceImporterScene::InternalImportCategory category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX;
  56. bool _set(const StringName &p_name, const Variant &p_value) {
  57. if (settings) {
  58. if (defaults.has(p_name) && defaults[p_name] == p_value) {
  59. settings->erase(p_name);
  60. } else {
  61. (*settings)[p_name] = p_value;
  62. }
  63. current[p_name] = p_value;
  64. // SceneImportSettings must decide if a new collider should be generated or not.
  65. if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE) {
  66. SceneImportSettingsDialog::get_singleton()->request_generate_collider();
  67. }
  68. if (SceneImportSettingsDialog::get_singleton()->is_editing_animation()) {
  69. if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  70. if (ResourceImporterScene::get_animation_singleton()->get_option_visibility(path, p_name, current)) {
  71. SceneImportSettingsDialog::get_singleton()->update_view();
  72. }
  73. } else {
  74. if (ResourceImporterScene::get_animation_singleton()->get_internal_option_update_view_required(category, p_name, current)) {
  75. SceneImportSettingsDialog::get_singleton()->update_view();
  76. }
  77. }
  78. } else {
  79. if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  80. if (ResourceImporterScene::get_scene_singleton()->get_option_visibility(path, p_name, current)) {
  81. SceneImportSettingsDialog::get_singleton()->update_view();
  82. }
  83. } else {
  84. if (ResourceImporterScene::get_scene_singleton()->get_internal_option_update_view_required(category, p_name, current)) {
  85. SceneImportSettingsDialog::get_singleton()->update_view();
  86. }
  87. }
  88. }
  89. return true;
  90. }
  91. return false;
  92. }
  93. bool _get(const StringName &p_name, Variant &r_ret) const {
  94. if (settings) {
  95. if (settings->has(p_name)) {
  96. r_ret = (*settings)[p_name];
  97. return true;
  98. }
  99. }
  100. if (defaults.has(p_name)) {
  101. r_ret = defaults[p_name];
  102. return true;
  103. }
  104. return false;
  105. }
  106. void handle_special_properties(PropertyInfo &r_option) const {
  107. ERR_FAIL_NULL(settings);
  108. if (r_option.name == "rest_pose/load_pose") {
  109. if (!settings->has("rest_pose/load_pose") || int((*settings)["rest_pose/load_pose"]) != 2) {
  110. if (settings->has("rest_pose/external_animation_library")) {
  111. (*settings)["rest_pose/external_animation_library"] = Variant();
  112. }
  113. }
  114. }
  115. if (r_option.name == "rest_pose/selected_animation") {
  116. if (!settings->has("rest_pose/load_pose")) {
  117. return;
  118. }
  119. String hint_string;
  120. switch (int((*settings)["rest_pose/load_pose"])) {
  121. case 1: {
  122. hint_string = String(",").join(animation_list);
  123. if (animation_list.size() == 1) {
  124. (*settings)["rest_pose/selected_animation"] = animation_list[0];
  125. }
  126. } break;
  127. case 2: {
  128. Object *res = nullptr;
  129. if (settings->has("rest_pose/external_animation_library")) {
  130. res = (*settings)["rest_pose/external_animation_library"];
  131. }
  132. Ref<Animation> anim(res);
  133. Ref<AnimationLibrary> library(res);
  134. if (anim.is_valid()) {
  135. hint_string = anim->get_name();
  136. }
  137. if (library.is_valid()) {
  138. List<StringName> anim_names;
  139. library->get_animation_list(&anim_names);
  140. if (anim_names.size() == 1) {
  141. (*settings)["rest_pose/selected_animation"] = String(anim_names.front()->get());
  142. }
  143. for (StringName anim_name : anim_names) {
  144. hint_string += "," + anim_name; // Include preceding, as a catch-all.
  145. }
  146. }
  147. } break;
  148. default:
  149. break;
  150. }
  151. r_option.hint = PROPERTY_HINT_ENUM;
  152. r_option.hint_string = hint_string;
  153. }
  154. }
  155. void _get_property_list(List<PropertyInfo> *r_list) const {
  156. if (hide_options) {
  157. return;
  158. }
  159. for (const ResourceImporter::ImportOption &E : options) {
  160. PropertyInfo option = E.option;
  161. if (SceneImportSettingsDialog::get_singleton()->is_editing_animation()) {
  162. if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  163. if (ResourceImporterScene::get_animation_singleton()->get_option_visibility(path, E.option.name, current)) {
  164. handle_special_properties(option);
  165. r_list->push_back(option);
  166. }
  167. } else {
  168. if (ResourceImporterScene::get_animation_singleton()->get_internal_option_visibility(category, E.option.name, current)) {
  169. handle_special_properties(option);
  170. r_list->push_back(option);
  171. }
  172. }
  173. } else {
  174. if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  175. if (ResourceImporterScene::get_scene_singleton()->get_option_visibility(path, E.option.name, current)) {
  176. handle_special_properties(option);
  177. r_list->push_back(option);
  178. }
  179. } else {
  180. if (ResourceImporterScene::get_scene_singleton()->get_internal_option_visibility(category, E.option.name, current)) {
  181. handle_special_properties(option);
  182. r_list->push_back(option);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. };
  189. void SceneImportSettingsDialog::_fill_material(Tree *p_tree, const Ref<Material> &p_material, TreeItem *p_parent) {
  190. String import_id;
  191. bool has_import_id = false;
  192. if (p_material->has_meta("import_id")) {
  193. import_id = p_material->get_meta("import_id");
  194. has_import_id = true;
  195. } else if (!p_material->get_name().is_empty()) {
  196. import_id = p_material->get_name();
  197. has_import_id = true;
  198. } else if (unnamed_material_name_map.has(p_material)) {
  199. import_id = unnamed_material_name_map[p_material];
  200. } else {
  201. import_id = "@MATERIAL:" + itos(material_map.size());
  202. unnamed_material_name_map[p_material] = import_id;
  203. }
  204. bool created = false;
  205. if (!material_map.has(import_id)) {
  206. MaterialData md;
  207. created = true;
  208. md.has_import_id = has_import_id;
  209. md.material = p_material;
  210. _load_default_subresource_settings(md.settings, "materials", import_id, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MATERIAL);
  211. material_map[import_id] = md;
  212. }
  213. MaterialData &material_data = material_map[import_id];
  214. ERR_FAIL_COND(p_material != material_data.material);
  215. Ref<Texture2D> icon = get_editor_theme_icon(SNAME("StandardMaterial3D"));
  216. TreeItem *item = p_tree->create_item(p_parent);
  217. if (p_material->get_name().is_empty()) {
  218. item->set_text(0, TTR("<Unnamed Material>"));
  219. } else {
  220. item->set_text(0, p_material->get_name());
  221. }
  222. item->set_icon(0, icon);
  223. item->set_meta("type", "Material");
  224. item->set_meta("import_id", import_id);
  225. item->set_tooltip_text(0, vformat(TTR("Import ID: %s"), import_id));
  226. item->set_selectable(0, true);
  227. if (p_tree == scene_tree) {
  228. material_data.scene_node = item;
  229. } else if (p_tree == mesh_tree) {
  230. material_data.mesh_node = item;
  231. } else {
  232. material_data.material_node = item;
  233. }
  234. if (created) {
  235. _fill_material(material_tree, p_material, material_tree->get_root());
  236. }
  237. }
  238. void SceneImportSettingsDialog::_fill_mesh(Tree *p_tree, const Ref<Mesh> &p_mesh, TreeItem *p_parent) {
  239. String import_id;
  240. bool has_import_id = false;
  241. if (p_mesh->has_meta("import_id")) {
  242. import_id = p_mesh->get_meta("import_id");
  243. has_import_id = true;
  244. } else if (!p_mesh->get_name().is_empty()) {
  245. import_id = p_mesh->get_name();
  246. has_import_id = true;
  247. } else {
  248. import_id = "@MESH:" + itos(mesh_set.size());
  249. }
  250. if (!mesh_map.has(import_id)) {
  251. MeshData md;
  252. md.has_import_id = has_import_id;
  253. md.mesh = p_mesh;
  254. _load_default_subresource_settings(md.settings, "meshes", import_id, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH);
  255. mesh_map[import_id] = md;
  256. }
  257. MeshData &mesh_data = mesh_map[import_id];
  258. Ref<Texture2D> icon = get_editor_theme_icon(SNAME("MeshItem"));
  259. TreeItem *item = p_tree->create_item(p_parent);
  260. item->set_text(0, p_mesh->get_name());
  261. item->set_icon(0, icon);
  262. bool created = false;
  263. if (!mesh_set.has(p_mesh)) {
  264. mesh_set.insert(p_mesh);
  265. created = true;
  266. }
  267. item->set_meta("type", "Mesh");
  268. item->set_meta("import_id", import_id);
  269. item->set_tooltip_text(0, vformat(TTR("Import ID: %s"), import_id));
  270. item->set_selectable(0, true);
  271. if (p_tree == scene_tree) {
  272. mesh_data.scene_node = item;
  273. } else {
  274. mesh_data.mesh_node = item;
  275. }
  276. item->set_collapsed(true);
  277. for (int i = 0; i < p_mesh->get_surface_count(); i++) {
  278. Ref<Material> mat = p_mesh->surface_get_material(i);
  279. if (mat.is_valid()) {
  280. _fill_material(p_tree, mat, item);
  281. }
  282. }
  283. if (created) {
  284. _fill_mesh(mesh_tree, p_mesh, mesh_tree->get_root());
  285. }
  286. }
  287. void SceneImportSettingsDialog::_fill_animation(Tree *p_tree, const Ref<Animation> &p_anim, const String &p_name, TreeItem *p_parent) {
  288. if (!animation_map.has(p_name)) {
  289. AnimationData ad;
  290. ad.animation = p_anim;
  291. _load_default_subresource_settings(ad.settings, "animations", p_name, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION);
  292. animation_map[p_name] = ad;
  293. }
  294. AnimationData &animation_data = animation_map[p_name];
  295. Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Animation"));
  296. TreeItem *item = p_tree->create_item(p_parent);
  297. item->set_text(0, p_name);
  298. item->set_icon(0, icon);
  299. item->set_meta("type", "Animation");
  300. item->set_meta("import_id", p_name);
  301. item->set_selectable(0, true);
  302. animation_data.scene_node = item;
  303. }
  304. void SceneImportSettingsDialog::_fill_scene(Node *p_node, TreeItem *p_parent_item) {
  305. String import_id;
  306. if (p_node->has_meta("import_id")) {
  307. import_id = p_node->get_meta("import_id");
  308. } else {
  309. import_id = "PATH:" + String(scene->get_path_to(p_node));
  310. p_node->set_meta("import_id", import_id);
  311. }
  312. ImporterMeshInstance3D *src_mesh_node = Object::cast_to<ImporterMeshInstance3D>(p_node);
  313. if (src_mesh_node) {
  314. MeshInstance3D *mesh_node = memnew(MeshInstance3D);
  315. mesh_node->set_name(src_mesh_node->get_name());
  316. mesh_node->set_transform(src_mesh_node->get_transform());
  317. mesh_node->set_skin(src_mesh_node->get_skin());
  318. mesh_node->set_skeleton_path(src_mesh_node->get_skeleton_path());
  319. mesh_node->set_visible(src_mesh_node->is_visible());
  320. if (src_mesh_node->get_mesh().is_valid()) {
  321. Ref<ImporterMesh> editor_mesh = src_mesh_node->get_mesh();
  322. mesh_node->set_mesh(editor_mesh->get_mesh());
  323. }
  324. // Replace the original mesh node in the scene tree with the new one.
  325. if (unlikely(p_node == scene)) {
  326. scene = mesh_node;
  327. }
  328. p_node->replace_by(mesh_node);
  329. memdelete(p_node);
  330. p_node = mesh_node;
  331. }
  332. String type = p_node->get_class();
  333. if (!has_theme_icon(type, EditorStringName(EditorIcons))) {
  334. type = "Node3D";
  335. }
  336. Ref<Texture2D> icon = get_editor_theme_icon(type);
  337. TreeItem *item = scene_tree->create_item(p_parent_item);
  338. item->set_text(0, p_node->get_name());
  339. if (p_node == scene) {
  340. icon = get_editor_theme_icon(SNAME("PackedScene"));
  341. item->set_text(0, TTR("Scene"));
  342. }
  343. item->set_icon(0, icon);
  344. item->set_meta("type", "Node");
  345. item->set_meta("class", type);
  346. item->set_meta("import_id", import_id);
  347. item->set_tooltip_text(0, vformat(TTR("Type: %s\nImport ID: %s"), type, import_id));
  348. item->set_selectable(0, true);
  349. if (!node_map.has(import_id)) {
  350. NodeData nd;
  351. if (p_node != scene) {
  352. ResourceImporterScene::InternalImportCategory category;
  353. if (src_mesh_node) {
  354. category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE;
  355. } else if (Object::cast_to<AnimationPlayer>(p_node)) {
  356. category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE;
  357. animation_player = Object::cast_to<AnimationPlayer>(p_node);
  358. animation_player->connect(SceneStringName(animation_finished), callable_mp(this, &SceneImportSettingsDialog::_animation_finished));
  359. } else if (Object::cast_to<Skeleton3D>(p_node)) {
  360. category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE;
  361. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_node);
  362. skeleton->connect(SceneStringName(tree_entered), callable_mp(this, &SceneImportSettingsDialog::_skeleton_tree_entered).bind(skeleton));
  363. skeletons.push_back(skeleton);
  364. } else {
  365. category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_NODE;
  366. }
  367. _load_default_subresource_settings(nd.settings, "nodes", import_id, category);
  368. }
  369. node_map[import_id] = nd;
  370. }
  371. NodeData &node_data = node_map[import_id];
  372. node_data.node = p_node;
  373. node_data.scene_node = item;
  374. AnimationPlayer *anim_node = Object::cast_to<AnimationPlayer>(p_node);
  375. if (anim_node) {
  376. Vector<String> animation_list;
  377. List<StringName> animations;
  378. anim_node->get_animation_list(&animations);
  379. for (const StringName &E : animations) {
  380. _fill_animation(scene_tree, anim_node->get_animation(E), E, item);
  381. animation_list.append(E);
  382. }
  383. if (scene_import_settings_data != nullptr) {
  384. scene_import_settings_data->animation_list = animation_list;
  385. }
  386. }
  387. for (int i = 0; i < p_node->get_child_count(); i++) {
  388. _fill_scene(p_node->get_child(i), item);
  389. }
  390. MeshInstance3D *mesh_node = Object::cast_to<MeshInstance3D>(p_node);
  391. if (mesh_node && mesh_node->get_mesh().is_valid()) {
  392. if (!editing_animation) {
  393. _fill_mesh(scene_tree, mesh_node->get_mesh(), item);
  394. }
  395. // Add the collider view.
  396. MeshInstance3D *collider_view = memnew(MeshInstance3D);
  397. collider_view->set_name("collider_view");
  398. collider_view->set_visible(false);
  399. mesh_node->add_child(collider_view, true);
  400. collider_view->set_owner(mesh_node);
  401. Transform3D accum_xform;
  402. Node3D *base = mesh_node;
  403. while (base) {
  404. accum_xform = base->get_transform() * accum_xform;
  405. base = Object::cast_to<Node3D>(base->get_parent());
  406. }
  407. AABB aabb = accum_xform.xform(mesh_node->get_mesh()->get_aabb());
  408. if (first_aabb) {
  409. contents_aabb = aabb;
  410. first_aabb = false;
  411. } else {
  412. contents_aabb.merge_with(aabb);
  413. }
  414. }
  415. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(p_node);
  416. if (skeleton) {
  417. Ref<ArrayMesh> bones_mesh = Skeleton3DGizmoPlugin::get_bones_mesh(skeleton, -1, true);
  418. bones_mesh_preview->set_mesh(bones_mesh);
  419. Transform3D accum_xform;
  420. Node3D *base = skeleton;
  421. while (base) {
  422. accum_xform = base->get_transform() * accum_xform;
  423. base = Object::cast_to<Node3D>(base->get_parent());
  424. }
  425. bones_mesh_preview->set_transform(accum_xform * skeleton->get_transform());
  426. AABB aabb = accum_xform.xform(bones_mesh->get_aabb());
  427. if (first_aabb) {
  428. contents_aabb = aabb;
  429. first_aabb = false;
  430. } else {
  431. contents_aabb.merge_with(aabb);
  432. }
  433. }
  434. }
  435. void SceneImportSettingsDialog::_update_scene() {
  436. scene_tree->clear();
  437. material_tree->clear();
  438. mesh_tree->clear();
  439. // Hidden roots.
  440. material_tree->create_item();
  441. mesh_tree->create_item();
  442. _fill_scene(scene, nullptr);
  443. }
  444. void SceneImportSettingsDialog::_update_view_gizmos() {
  445. if (!is_visible()) {
  446. return;
  447. }
  448. const HashMap<StringName, Variant> &main_settings = scene_import_settings_data->current;
  449. bool reshow_settings = false;
  450. if (main_settings.has("nodes/import_as_skeleton_bones")) {
  451. bool new_import_as_skeleton = main_settings["nodes/import_as_skeleton_bones"];
  452. reshow_settings = reshow_settings || (new_import_as_skeleton != previous_import_as_skeleton);
  453. previous_import_as_skeleton = new_import_as_skeleton;
  454. }
  455. if (main_settings.has("animation/import_rest_as_RESET")) {
  456. bool new_rest_as_reset = main_settings["animation/import_rest_as_RESET"];
  457. reshow_settings = reshow_settings || (new_rest_as_reset != previous_rest_as_reset);
  458. previous_rest_as_reset = new_rest_as_reset;
  459. }
  460. if (reshow_settings) {
  461. _re_import();
  462. open_settings(base_path);
  463. return;
  464. }
  465. for (const KeyValue<String, NodeData> &e : node_map) {
  466. // Skip import nodes that aren't MeshInstance3D.
  467. const MeshInstance3D *mesh_node = Object::cast_to<MeshInstance3D>(e.value.node);
  468. if (mesh_node == nullptr || mesh_node->get_mesh().is_null()) {
  469. continue;
  470. }
  471. // Determine if the mesh collider should be visible.
  472. bool show_collider_view = false;
  473. if (e.value.settings.has(SNAME("generate/physics"))) {
  474. show_collider_view = e.value.settings[SNAME("generate/physics")];
  475. }
  476. // Get the collider_view MeshInstance3D.
  477. TypedArray<Node> descendants = mesh_node->find_children("collider_view", "MeshInstance3D");
  478. CRASH_COND_MSG(descendants.is_empty(), "This is unreachable, since the collider view is always created even when the collision is not used! If this is triggered there is a bug on the function `_fill_scene`.");
  479. MeshInstance3D *collider_view = Object::cast_to<MeshInstance3D>(descendants[0].operator Object *());
  480. // Regenerate the physics collider for this MeshInstance3D if either:
  481. // - A regeneration is requested for the selected import node.
  482. // - The collider is being made visible.
  483. if ((generate_collider && e.key == selected_id) || (show_collider_view && !collider_view->is_visible())) {
  484. // This collider_view doesn't have a mesh so we need to generate a new one.
  485. Ref<ImporterMesh> mesh;
  486. mesh.instantiate();
  487. // ResourceImporterScene::get_collision_shapes() expects ImporterMesh, not Mesh.
  488. // TODO: Duplicate code with EditorSceneFormatImporterESCN::import_scene()
  489. // Consider making a utility function to convert from Mesh to ImporterMesh.
  490. Ref<Mesh> mesh_3d_mesh = mesh_node->get_mesh();
  491. Ref<ArrayMesh> array_mesh_3d_mesh = mesh_3d_mesh;
  492. if (array_mesh_3d_mesh.is_valid()) {
  493. // For the MeshInstance3D nodes, we need to convert the ArrayMesh to an ImporterMesh specially.
  494. mesh->set_name(array_mesh_3d_mesh->get_name());
  495. for (int32_t blend_i = 0; blend_i < array_mesh_3d_mesh->get_blend_shape_count(); blend_i++) {
  496. mesh->add_blend_shape(array_mesh_3d_mesh->get_blend_shape_name(blend_i));
  497. }
  498. for (int32_t surface_i = 0; surface_i < array_mesh_3d_mesh->get_surface_count(); surface_i++) {
  499. mesh->add_surface(array_mesh_3d_mesh->surface_get_primitive_type(surface_i),
  500. array_mesh_3d_mesh->surface_get_arrays(surface_i),
  501. array_mesh_3d_mesh->surface_get_blend_shape_arrays(surface_i),
  502. array_mesh_3d_mesh->surface_get_lods(surface_i),
  503. array_mesh_3d_mesh->surface_get_material(surface_i),
  504. array_mesh_3d_mesh->surface_get_name(surface_i),
  505. array_mesh_3d_mesh->surface_get_format(surface_i));
  506. }
  507. mesh->set_blend_shape_mode(array_mesh_3d_mesh->get_blend_shape_mode());
  508. } else if (mesh_3d_mesh.is_valid()) {
  509. // For the MeshInstance3D nodes, we need to convert the Mesh to an ImporterMesh specially.
  510. mesh->set_name(mesh_3d_mesh->get_name());
  511. for (int32_t surface_i = 0; surface_i < mesh_3d_mesh->get_surface_count(); surface_i++) {
  512. mesh->add_surface(mesh_3d_mesh->surface_get_primitive_type(surface_i),
  513. mesh_3d_mesh->surface_get_arrays(surface_i),
  514. Array(),
  515. mesh_3d_mesh->surface_get_lods(surface_i),
  516. mesh_3d_mesh->surface_get_material(surface_i),
  517. mesh_3d_mesh->surface_get_material(surface_i).is_valid() ? mesh_3d_mesh->surface_get_material(surface_i)->get_name() : String(),
  518. mesh_3d_mesh->surface_get_format(surface_i));
  519. }
  520. }
  521. // Generate the mesh collider.
  522. Vector<Ref<Shape3D>> shapes = ResourceImporterScene::get_collision_shapes(mesh, e.value.settings, 1.0);
  523. const Transform3D transform = ResourceImporterScene::get_collision_shapes_transform(e.value.settings);
  524. Ref<ArrayMesh> collider_view_mesh;
  525. collider_view_mesh.instantiate();
  526. for (Ref<Shape3D> shape : shapes) {
  527. Ref<ArrayMesh> debug_shape_mesh;
  528. if (shape.is_valid()) {
  529. debug_shape_mesh = shape->get_debug_mesh();
  530. }
  531. if (debug_shape_mesh.is_valid()) {
  532. collider_view_mesh->add_surface_from_arrays(
  533. debug_shape_mesh->surface_get_primitive_type(0),
  534. debug_shape_mesh->surface_get_arrays(0));
  535. collider_view_mesh->surface_set_material(
  536. collider_view_mesh->get_surface_count() - 1,
  537. collider_mat);
  538. }
  539. }
  540. collider_view->set_mesh(collider_view_mesh);
  541. collider_view->set_transform(transform);
  542. }
  543. // Set the collider visibility.
  544. collider_view->set_visible(show_collider_view);
  545. }
  546. generate_collider = false;
  547. }
  548. void SceneImportSettingsDialog::_update_camera() {
  549. AABB camera_aabb;
  550. float rot_x = cam_rot_x;
  551. float rot_y = cam_rot_y;
  552. float zoom = cam_zoom;
  553. if (selected_type == "Node" || selected_type == "Animation" || selected_type.is_empty()) {
  554. camera_aabb = contents_aabb;
  555. } else {
  556. if (mesh_preview->get_mesh().is_valid()) {
  557. camera_aabb = mesh_preview->get_transform().xform(mesh_preview->get_mesh()->get_aabb());
  558. } else {
  559. camera_aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  560. }
  561. if (selected_type == "Mesh" && mesh_map.has(selected_id)) {
  562. const MeshData &md = mesh_map[selected_id];
  563. rot_x = md.cam_rot_x;
  564. rot_y = md.cam_rot_y;
  565. zoom = md.cam_zoom;
  566. } else if (selected_type == "Material" && material_map.has(selected_id)) {
  567. const MaterialData &md = material_map[selected_id];
  568. rot_x = md.cam_rot_x;
  569. rot_y = md.cam_rot_y;
  570. zoom = md.cam_zoom;
  571. }
  572. }
  573. Vector3 center = camera_aabb.get_center();
  574. float camera_size = camera_aabb.get_longest_axis_size();
  575. camera->set_orthogonal(camera_size * zoom, 0.0001, camera_size * 2);
  576. Transform3D xf;
  577. xf.basis = Basis(Vector3(0, 1, 0), rot_y) * Basis(Vector3(1, 0, 0), rot_x);
  578. xf.origin = center;
  579. xf.translate_local(0, 0, camera_size);
  580. camera->set_transform(xf);
  581. }
  582. void SceneImportSettingsDialog::_load_default_subresource_settings(HashMap<StringName, Variant> &settings, const String &p_type, const String &p_import_id, ResourceImporterScene::InternalImportCategory p_category) {
  583. if (base_subresource_settings.has(p_type)) {
  584. Dictionary d = base_subresource_settings[p_type];
  585. if (d.has(p_import_id)) {
  586. d = d[p_import_id];
  587. List<ResourceImporterScene::ImportOption> options;
  588. if (editing_animation) {
  589. ResourceImporterScene::get_animation_singleton()->get_internal_import_options(p_category, &options);
  590. } else {
  591. ResourceImporterScene::get_scene_singleton()->get_internal_import_options(p_category, &options);
  592. }
  593. for (const ResourceImporterScene::ImportOption &E : options) {
  594. String key = E.option.name;
  595. if (d.has(key)) {
  596. settings[key] = d[key];
  597. }
  598. }
  599. }
  600. }
  601. }
  602. void SceneImportSettingsDialog::request_generate_collider() {
  603. generate_collider = true;
  604. }
  605. void SceneImportSettingsDialog::update_view() {
  606. update_view_timer->start();
  607. }
  608. void SceneImportSettingsDialog::open_settings(const String &p_path, const String &p_scene_import_type) {
  609. if (scene) {
  610. _cleanup();
  611. memdelete(scene);
  612. scene = nullptr;
  613. }
  614. editing_animation = p_scene_import_type == "AnimationLibrary";
  615. scene_import_settings_data->settings = nullptr;
  616. scene_import_settings_data->path = p_path;
  617. // Visibility.
  618. data_mode->set_tab_hidden(1, editing_animation);
  619. data_mode->set_tab_hidden(2, editing_animation);
  620. if (editing_animation) {
  621. data_mode->set_current_tab(0);
  622. }
  623. action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_EXTRACT_MATERIALS), editing_animation);
  624. action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_CHOOSE_MESH_SAVE_PATHS), editing_animation);
  625. base_path = p_path;
  626. mesh_set.clear();
  627. animation_map.clear();
  628. material_map.clear();
  629. unnamed_material_name_map.clear();
  630. mesh_map.clear();
  631. node_map.clear();
  632. defaults.clear();
  633. mesh_preview->hide();
  634. selected_id = "";
  635. selected_type = "";
  636. cam_rot_x = -Math::PI / 4;
  637. cam_rot_y = -Math::PI / 4;
  638. cam_zoom = 1;
  639. {
  640. base_subresource_settings.clear();
  641. Ref<ConfigFile> config;
  642. config.instantiate();
  643. Error err = config->load(p_path + ".import");
  644. if (err == OK) {
  645. List<String> keys;
  646. config->get_section_keys("params", &keys);
  647. for (const String &E : keys) {
  648. Variant value = config->get_value("params", E);
  649. if (E == "_subresources") {
  650. base_subresource_settings = value;
  651. } else {
  652. defaults[E] = value;
  653. }
  654. }
  655. }
  656. }
  657. scene = ResourceImporterScene::get_scene_singleton()->pre_import(p_path, defaults); // Use the scene singleton here because we want to see the full thing.
  658. if (scene == nullptr) {
  659. EditorNode::get_singleton()->show_warning(TTR("Error opening scene"));
  660. return;
  661. }
  662. first_aabb = true;
  663. _update_scene();
  664. base_viewport->add_child(scene);
  665. inspector->edit(nullptr);
  666. if (first_aabb) {
  667. contents_aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  668. first_aabb = false;
  669. }
  670. const HashMap<StringName, Variant> &main_settings = scene_import_settings_data->current;
  671. if (main_settings.has("nodes/import_as_skeleton_bones")) {
  672. previous_import_as_skeleton = main_settings["nodes/import_as_skeleton_bones"];
  673. }
  674. if (main_settings.has("animation/import_rest_as_RESET")) {
  675. previous_rest_as_reset = main_settings["animation/import_rest_as_RESET"];
  676. }
  677. popup_centered_ratio();
  678. _update_view_gizmos();
  679. _update_camera();
  680. // Start with the root item (Scene) selected.
  681. scene_tree->get_root()->select(0);
  682. if (editing_animation) {
  683. set_title(vformat(TTR("Advanced Import Settings for AnimationLibrary '%s'"), base_path.get_file()));
  684. } else {
  685. set_title(vformat(TTR("Advanced Import Settings for Scene '%s'"), base_path.get_file()));
  686. }
  687. }
  688. SceneImportSettingsDialog *SceneImportSettingsDialog::singleton = nullptr;
  689. SceneImportSettingsDialog *SceneImportSettingsDialog::get_singleton() {
  690. return singleton;
  691. }
  692. Node *SceneImportSettingsDialog::get_selected_node() {
  693. if (selected_id == "") {
  694. return nullptr;
  695. }
  696. return node_map[selected_id].node;
  697. }
  698. void SceneImportSettingsDialog::_select(Tree *p_from, const String &p_type, const String &p_id) {
  699. selecting = true;
  700. scene_import_settings_data->hide_options = false;
  701. bones_mesh_preview->hide();
  702. if (p_type == "Node") {
  703. node_selected->hide(); // Always hide just in case.
  704. mesh_preview->hide();
  705. _reset_animation();
  706. if (Object::cast_to<Node3D>(scene)) {
  707. Object::cast_to<Node3D>(scene)->show();
  708. }
  709. material_tree->deselect_all();
  710. mesh_tree->deselect_all();
  711. NodeData &nd = node_map[p_id];
  712. MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(nd.node);
  713. if (mi) {
  714. Ref<Mesh> base_mesh = mi->get_mesh();
  715. if (base_mesh.is_valid()) {
  716. AABB aabb = base_mesh->get_aabb();
  717. Transform3D aabb_xf;
  718. aabb_xf.basis.scale(aabb.size);
  719. aabb_xf.origin = aabb.position;
  720. aabb_xf = mi->get_global_transform() * aabb_xf;
  721. node_selected->set_transform(aabb_xf);
  722. node_selected->show();
  723. }
  724. }
  725. if (nd.node == scene) {
  726. scene_import_settings_data->settings = &defaults;
  727. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX;
  728. } else {
  729. scene_import_settings_data->settings = &nd.settings;
  730. if (mi) {
  731. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE;
  732. scene_import_settings_data->hide_options = editing_animation;
  733. } else if (Object::cast_to<AnimationPlayer>(nd.node)) {
  734. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE;
  735. } else if (Object::cast_to<Skeleton3D>(nd.node)) {
  736. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE;
  737. bones_mesh_preview->show();
  738. } else {
  739. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_NODE;
  740. scene_import_settings_data->hide_options = editing_animation;
  741. }
  742. }
  743. } else if (p_type == "Animation") {
  744. node_selected->hide(); // Always hide just in case.
  745. mesh_preview->hide();
  746. _reset_animation(p_id);
  747. if (Object::cast_to<Node3D>(scene)) {
  748. Object::cast_to<Node3D>(scene)->show();
  749. }
  750. material_tree->deselect_all();
  751. mesh_tree->deselect_all();
  752. AnimationData &ad = animation_map[p_id];
  753. scene_import_settings_data->settings = &ad.settings;
  754. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION;
  755. _animation_update_skeleton_visibility();
  756. } else if (p_type == "Mesh") {
  757. node_selected->hide();
  758. if (Object::cast_to<Node3D>(scene)) {
  759. Object::cast_to<Node3D>(scene)->hide();
  760. }
  761. MeshData &md = mesh_map[p_id];
  762. if (md.mesh_node != nullptr) {
  763. if (p_from != mesh_tree) {
  764. md.mesh_node->uncollapse_tree();
  765. md.mesh_node->select(0);
  766. mesh_tree->ensure_cursor_is_visible();
  767. }
  768. if (p_from != scene_tree) {
  769. md.scene_node->uncollapse_tree();
  770. md.scene_node->select(0);
  771. scene_tree->ensure_cursor_is_visible();
  772. }
  773. }
  774. mesh_preview->set_mesh(md.mesh);
  775. mesh_preview->show();
  776. _reset_animation();
  777. material_tree->deselect_all();
  778. scene_import_settings_data->settings = &md.settings;
  779. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH;
  780. } else if (p_type == "Material") {
  781. node_selected->hide();
  782. if (Object::cast_to<Node3D>(scene)) {
  783. Object::cast_to<Node3D>(scene)->hide();
  784. }
  785. mesh_preview->show();
  786. _reset_animation();
  787. MaterialData &md = material_map[p_id];
  788. material_preview->set_material(md.material);
  789. mesh_preview->set_mesh(material_preview);
  790. if (p_from != mesh_tree) {
  791. md.mesh_node->uncollapse_tree();
  792. md.mesh_node->select(0);
  793. mesh_tree->ensure_cursor_is_visible();
  794. }
  795. if (p_from != scene_tree) {
  796. md.scene_node->uncollapse_tree();
  797. md.scene_node->select(0);
  798. scene_tree->ensure_cursor_is_visible();
  799. }
  800. if (p_from != material_tree) {
  801. md.material_node->uncollapse_tree();
  802. md.material_node->select(0);
  803. material_tree->ensure_cursor_is_visible();
  804. }
  805. scene_import_settings_data->settings = &md.settings;
  806. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MATERIAL;
  807. }
  808. selected_type = p_type;
  809. selected_id = p_id;
  810. selecting = false;
  811. _update_camera();
  812. List<ResourceImporter::ImportOption> options;
  813. if (editing_animation) {
  814. if (scene_import_settings_data->category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  815. ResourceImporterScene::get_animation_singleton()->get_import_options(base_path, &options);
  816. } else {
  817. ResourceImporterScene::get_animation_singleton()->get_internal_import_options(scene_import_settings_data->category, &options);
  818. }
  819. } else {
  820. if (scene_import_settings_data->category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  821. ResourceImporterScene::get_scene_singleton()->get_import_options(base_path, &options);
  822. } else {
  823. ResourceImporterScene::get_scene_singleton()->get_internal_import_options(scene_import_settings_data->category, &options);
  824. }
  825. }
  826. scene_import_settings_data->defaults.clear();
  827. scene_import_settings_data->current.clear();
  828. if (scene_import_settings_data->settings) {
  829. for (const ResourceImporter::ImportOption &E : options) {
  830. scene_import_settings_data->defaults[E.option.name] = E.default_value;
  831. // Needed for visibility toggling (fails if something is missing).
  832. if (scene_import_settings_data->settings->has(E.option.name)) {
  833. scene_import_settings_data->current[E.option.name] = (*scene_import_settings_data->settings)[E.option.name];
  834. } else {
  835. scene_import_settings_data->current[E.option.name] = E.default_value;
  836. }
  837. }
  838. }
  839. scene_import_settings_data->options = options;
  840. inspector->edit(scene_import_settings_data);
  841. scene_import_settings_data->notify_property_list_changed();
  842. }
  843. void SceneImportSettingsDialog::_inspector_property_edited(const String &p_name) {
  844. if (p_name == "settings/loop_mode") {
  845. if (!animation_map.has(selected_id)) {
  846. return;
  847. }
  848. HashMap<StringName, Variant> settings = animation_map[selected_id].settings;
  849. if (settings.has(p_name)) {
  850. animation_loop_mode = static_cast<Animation::LoopMode>((int)settings[p_name]);
  851. } else {
  852. animation_loop_mode = Animation::LoopMode::LOOP_NONE;
  853. }
  854. }
  855. }
  856. void SceneImportSettingsDialog::_reset_bone_transforms() {
  857. for (Skeleton3D *skeleton : skeletons) {
  858. skeleton->reset_bone_poses();
  859. }
  860. }
  861. void SceneImportSettingsDialog::_play_animation() {
  862. if (animation_player == nullptr) {
  863. return;
  864. }
  865. StringName id = StringName(selected_id);
  866. if (animation_player->has_animation(id)) {
  867. if (animation_player->is_playing()) {
  868. animation_player->pause();
  869. animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  870. set_process(false);
  871. } else {
  872. animation_player->play(id);
  873. animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("Pause")));
  874. set_process(true);
  875. }
  876. }
  877. }
  878. void SceneImportSettingsDialog::_stop_current_animation() {
  879. animation_pingpong = false;
  880. animation_player->stop();
  881. animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  882. animation_slider->set_value_no_signal(0.0);
  883. set_process(false);
  884. }
  885. void SceneImportSettingsDialog::_reset_animation(const String &p_animation_name) {
  886. if (p_animation_name.is_empty()) {
  887. animation_preview->hide();
  888. if (animation_player != nullptr && animation_player->is_playing()) {
  889. animation_player->stop();
  890. }
  891. animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  892. _reset_bone_transforms();
  893. set_process(false);
  894. } else {
  895. _reset_bone_transforms();
  896. animation_preview->show();
  897. animation_loop_mode = Animation::LoopMode::LOOP_NONE;
  898. animation_pingpong = false;
  899. if (animation_map.has(p_animation_name)) {
  900. HashMap<StringName, Variant> settings = animation_map[p_animation_name].settings;
  901. if (settings.has("settings/loop_mode")) {
  902. animation_loop_mode = static_cast<Animation::LoopMode>((int)settings["settings/loop_mode"]);
  903. }
  904. }
  905. if (animation_player->is_playing() && animation_loop_mode != Animation::LoopMode::LOOP_NONE) {
  906. animation_player->play(p_animation_name);
  907. } else {
  908. animation_player->stop(true);
  909. animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  910. animation_player->set_assigned_animation(p_animation_name);
  911. animation_player->seek(0.0, true);
  912. animation_slider->set_value_no_signal(0.0);
  913. set_process(false);
  914. }
  915. }
  916. }
  917. void SceneImportSettingsDialog::_animation_slider_value_changed(double p_value) {
  918. if (animation_player == nullptr || !animation_map.has(selected_id) || animation_map[selected_id].animation.is_null()) {
  919. return;
  920. }
  921. if (animation_player->is_playing()) {
  922. animation_player->stop();
  923. animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  924. set_process(false);
  925. }
  926. animation_player->seek(p_value * animation_map[selected_id].animation->get_length(), true);
  927. }
  928. void SceneImportSettingsDialog::_skeleton_tree_entered(Skeleton3D *p_skeleton) {
  929. bones_mesh_preview->set_skeleton_path(p_skeleton->get_path());
  930. bones_mesh_preview->set_skin(p_skeleton->register_skin(p_skeleton->create_skin_from_rest_transforms()));
  931. }
  932. void SceneImportSettingsDialog::_animation_finished(const StringName &p_name) {
  933. Animation::LoopMode loop_mode = animation_loop_mode;
  934. switch (loop_mode) {
  935. case Animation::LOOP_NONE: {
  936. animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  937. animation_slider->set_value_no_signal(1.0);
  938. set_process(false);
  939. } break;
  940. case Animation::LOOP_LINEAR: {
  941. animation_player->play(p_name);
  942. } break;
  943. case Animation::LOOP_PINGPONG: {
  944. if (animation_pingpong) {
  945. animation_player->play(p_name);
  946. } else {
  947. animation_player->play_backwards(p_name);
  948. }
  949. animation_pingpong = !animation_pingpong;
  950. } break;
  951. default: {
  952. } break;
  953. }
  954. }
  955. void SceneImportSettingsDialog::_animation_update_skeleton_visibility() {
  956. if (animation_toggle_skeleton_visibility->is_pressed()) {
  957. bones_mesh_preview->show();
  958. } else {
  959. bones_mesh_preview->hide();
  960. }
  961. }
  962. void SceneImportSettingsDialog::_material_tree_selected() {
  963. if (selecting) {
  964. return;
  965. }
  966. TreeItem *item = material_tree->get_selected();
  967. String type = item->get_meta("type");
  968. String import_id = item->get_meta("import_id");
  969. _select(material_tree, type, import_id);
  970. }
  971. void SceneImportSettingsDialog::_mesh_tree_selected() {
  972. if (selecting) {
  973. return;
  974. }
  975. TreeItem *item = mesh_tree->get_selected();
  976. String type = item->get_meta("type");
  977. String import_id = item->get_meta("import_id");
  978. _select(mesh_tree, type, import_id);
  979. }
  980. void SceneImportSettingsDialog::_scene_tree_selected() {
  981. if (selecting) {
  982. return;
  983. }
  984. TreeItem *item = scene_tree->get_selected();
  985. String type = item->get_meta("type");
  986. String import_id = item->get_meta("import_id");
  987. _select(scene_tree, type, import_id);
  988. }
  989. void SceneImportSettingsDialog::_cleanup() {
  990. skeletons.clear();
  991. if (animation_player != nullptr) {
  992. animation_player->disconnect(SceneStringName(animation_finished), callable_mp(this, &SceneImportSettingsDialog::_animation_finished));
  993. animation_player = nullptr;
  994. }
  995. set_process(false);
  996. }
  997. void SceneImportSettingsDialog::_on_light_1_switch_pressed() {
  998. light1->set_visible(light_1_switch->is_pressed());
  999. }
  1000. void SceneImportSettingsDialog::_on_light_2_switch_pressed() {
  1001. light2->set_visible(light_2_switch->is_pressed());
  1002. }
  1003. void SceneImportSettingsDialog::_on_light_rotate_switch_pressed() {
  1004. bool light_top_level = !light_rotate_switch->is_pressed();
  1005. light1->set_as_top_level_keep_local(light_top_level);
  1006. light2->set_as_top_level_keep_local(light_top_level);
  1007. }
  1008. void SceneImportSettingsDialog::_viewport_input(const Ref<InputEvent> &p_input) {
  1009. float *rot_x = &cam_rot_x;
  1010. float *rot_y = &cam_rot_y;
  1011. float *zoom = &cam_zoom;
  1012. if (selected_type == "Mesh" && mesh_map.has(selected_id)) {
  1013. MeshData &md = mesh_map[selected_id];
  1014. rot_x = &md.cam_rot_x;
  1015. rot_y = &md.cam_rot_y;
  1016. zoom = &md.cam_zoom;
  1017. } else if (selected_type == "Material" && material_map.has(selected_id)) {
  1018. MaterialData &md = material_map[selected_id];
  1019. rot_x = &md.cam_rot_x;
  1020. rot_y = &md.cam_rot_y;
  1021. zoom = &md.cam_zoom;
  1022. }
  1023. Ref<InputEventMouseMotion> mm = p_input;
  1024. if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  1025. (*rot_x) -= mm->get_relative().y * 0.01 * EDSCALE;
  1026. (*rot_y) -= mm->get_relative().x * 0.01 * EDSCALE;
  1027. (*rot_x) = CLAMP((*rot_x), -Math::PI / 2, Math::PI / 2);
  1028. _update_camera();
  1029. }
  1030. if (mm.is_valid() && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CURSOR_SHAPE)) {
  1031. DisplayServer::get_singleton()->cursor_set_shape(DisplayServer::CursorShape::CURSOR_ARROW);
  1032. }
  1033. Ref<InputEventMouseButton> mb = p_input;
  1034. if (mb.is_valid() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  1035. (*zoom) *= 1.1;
  1036. if ((*zoom) > 10.0) {
  1037. (*zoom) = 10.0;
  1038. }
  1039. _update_camera();
  1040. }
  1041. if (mb.is_valid() && mb->get_button_index() == MouseButton::WHEEL_UP) {
  1042. (*zoom) /= 1.1;
  1043. if ((*zoom) < 0.1) {
  1044. (*zoom) = 0.1;
  1045. }
  1046. _update_camera();
  1047. }
  1048. Ref<InputEventMagnifyGesture> mg = p_input;
  1049. if (mg.is_valid()) {
  1050. real_t mg_factor = mg->get_factor();
  1051. if (mg_factor == 0.0) {
  1052. mg_factor = 1.0;
  1053. }
  1054. (*zoom) /= mg_factor;
  1055. if ((*zoom) < 0.1) {
  1056. (*zoom) = 0.1;
  1057. } else if ((*zoom) > 10.0) {
  1058. (*zoom) = 10.0;
  1059. }
  1060. _update_camera();
  1061. }
  1062. }
  1063. void SceneImportSettingsDialog::_re_import() {
  1064. HashMap<StringName, Variant> main_settings;
  1065. main_settings = scene_import_settings_data->current;
  1066. main_settings.erase("_subresources");
  1067. Dictionary nodes;
  1068. Dictionary materials;
  1069. Dictionary meshes;
  1070. Dictionary animations;
  1071. Dictionary subresources;
  1072. for (KeyValue<String, NodeData> &E : node_map) {
  1073. if (E.value.settings.size()) {
  1074. Dictionary d;
  1075. for (const KeyValue<StringName, Variant> &F : E.value.settings) {
  1076. d[String(F.key)] = F.value;
  1077. }
  1078. nodes[E.key] = d;
  1079. }
  1080. }
  1081. if (nodes.size()) {
  1082. subresources["nodes"] = nodes;
  1083. }
  1084. for (KeyValue<String, MaterialData> &E : material_map) {
  1085. if (E.value.settings.size()) {
  1086. Dictionary d;
  1087. for (const KeyValue<StringName, Variant> &F : E.value.settings) {
  1088. d[String(F.key)] = F.value;
  1089. }
  1090. materials[E.key] = d;
  1091. }
  1092. }
  1093. if (materials.size()) {
  1094. subresources["materials"] = materials;
  1095. }
  1096. for (KeyValue<String, MeshData> &E : mesh_map) {
  1097. if (E.value.settings.size()) {
  1098. Dictionary d;
  1099. for (const KeyValue<StringName, Variant> &F : E.value.settings) {
  1100. d[String(F.key)] = F.value;
  1101. }
  1102. meshes[E.key] = d;
  1103. }
  1104. }
  1105. if (meshes.size()) {
  1106. subresources["meshes"] = meshes;
  1107. }
  1108. for (KeyValue<String, AnimationData> &E : animation_map) {
  1109. if (E.value.settings.size()) {
  1110. Dictionary d;
  1111. for (const KeyValue<StringName, Variant> &F : E.value.settings) {
  1112. d[String(F.key)] = F.value;
  1113. }
  1114. animations[E.key] = d;
  1115. }
  1116. }
  1117. if (animations.size()) {
  1118. subresources["animations"] = animations;
  1119. }
  1120. if (subresources.size()) {
  1121. main_settings["_subresources"] = subresources;
  1122. }
  1123. _cleanup(); // Prevent skeletons and other pointers from pointing to dangling references.
  1124. EditorFileSystem::get_singleton()->reimport_file_with_custom_parameters(base_path, editing_animation ? "animation_library" : "scene", main_settings);
  1125. }
  1126. void SceneImportSettingsDialog::_update_theme_item_cache() {
  1127. ConfirmationDialog::_update_theme_item_cache();
  1128. theme_cache.light_1_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight1"));
  1129. theme_cache.light_2_icon = get_editor_theme_icon(SNAME("MaterialPreviewLight2"));
  1130. theme_cache.rotate_icon = get_editor_theme_icon(SNAME("PreviewRotate"));
  1131. }
  1132. void SceneImportSettingsDialog::_notification(int p_what) {
  1133. switch (p_what) {
  1134. case NOTIFICATION_READY: {
  1135. connect(SceneStringName(confirmed), callable_mp(this, &SceneImportSettingsDialog::_re_import));
  1136. } break;
  1137. case NOTIFICATION_THEME_CHANGED: {
  1138. action_menu->begin_bulk_theme_override();
  1139. action_menu->add_theme_style_override(CoreStringName(normal), get_theme_stylebox(CoreStringName(normal), "Button"));
  1140. action_menu->add_theme_style_override(SceneStringName(hover), get_theme_stylebox(SceneStringName(hover), "Button"));
  1141. action_menu->add_theme_style_override(SceneStringName(pressed), get_theme_stylebox(SceneStringName(pressed), "Button"));
  1142. action_menu->end_bulk_theme_override();
  1143. if (animation_player != nullptr && animation_player->is_playing()) {
  1144. animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("Pause")));
  1145. } else {
  1146. animation_play_button->set_button_icon(get_editor_theme_icon(SNAME("MainPlay")));
  1147. }
  1148. animation_stop_button->set_button_icon(get_editor_theme_icon(SNAME("Stop")));
  1149. light_1_switch->set_button_icon(theme_cache.light_1_icon);
  1150. light_2_switch->set_button_icon(theme_cache.light_2_icon);
  1151. light_rotate_switch->set_button_icon(theme_cache.rotate_icon);
  1152. animation_toggle_skeleton_visibility->set_button_icon(get_editor_theme_icon(SNAME("SkeletonPreview")));
  1153. } break;
  1154. case NOTIFICATION_PROCESS: {
  1155. if (animation_player != nullptr) {
  1156. animation_slider->set_value_no_signal(animation_player->get_current_animation_position() / animation_player->get_current_animation_length());
  1157. }
  1158. } break;
  1159. case NOTIFICATION_VISIBILITY_CHANGED: {
  1160. if (!is_visible()) {
  1161. _cleanup();
  1162. }
  1163. } break;
  1164. }
  1165. }
  1166. void SceneImportSettingsDialog::_menu_callback(int p_id) {
  1167. switch (p_id) {
  1168. case ACTION_EXTRACT_MATERIALS: {
  1169. save_path->set_title(TTR("Select folder to extract material resources"));
  1170. external_extension_type->select(0);
  1171. } break;
  1172. case ACTION_CHOOSE_MESH_SAVE_PATHS: {
  1173. save_path->set_title(TTR("Select folder where mesh resources will save on import"));
  1174. external_extension_type->select(1);
  1175. } break;
  1176. case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
  1177. save_path->set_title(TTR("Select folder where animations will save on import"));
  1178. external_extension_type->select(1);
  1179. } break;
  1180. }
  1181. save_path->set_current_dir(base_path.get_base_dir());
  1182. current_action = p_id;
  1183. save_path->popup_centered_ratio();
  1184. }
  1185. void SceneImportSettingsDialog::_save_path_changed(const String &p_path) {
  1186. save_path_item->set_text(1, p_path);
  1187. if (FileAccess::exists(p_path)) {
  1188. save_path_item->set_text(2, TTR("Warning: File exists"));
  1189. save_path_item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced."));
  1190. save_path_item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
  1191. } else {
  1192. save_path_item->set_text(2, TTR("Will create new file"));
  1193. save_path_item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
  1194. }
  1195. }
  1196. void SceneImportSettingsDialog::_browse_save_callback(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  1197. if (p_button != MouseButton::LEFT) {
  1198. return;
  1199. }
  1200. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  1201. String path = item->get_text(1);
  1202. item_save_path->set_current_file(path);
  1203. save_path_item = item;
  1204. item_save_path->popup_centered_ratio();
  1205. }
  1206. void SceneImportSettingsDialog::_save_dir_callback(const String &p_path) {
  1207. external_path_tree->clear();
  1208. TreeItem *root = external_path_tree->create_item();
  1209. save_path_items.clear();
  1210. switch (current_action) {
  1211. case ACTION_EXTRACT_MATERIALS: {
  1212. for (const KeyValue<String, MaterialData> &E : material_map) {
  1213. MaterialData &md = material_map[E.key];
  1214. TreeItem *item = external_path_tree->create_item(root);
  1215. String name = md.material_node->get_text(0);
  1216. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  1217. item->set_icon(0, get_editor_theme_icon(SNAME("StandardMaterial3D")));
  1218. item->set_text(0, name);
  1219. if (md.has_import_id) {
  1220. if (md.settings.has("use_external/enabled") && bool(md.settings["use_external/enabled"])) {
  1221. item->set_text(2, TTR("Already External"));
  1222. item->set_tooltip_text(2, TTR("This material already references an external file, no action will be taken.\nDisable the external property for it to be extracted again."));
  1223. } else {
  1224. item->set_metadata(0, E.key);
  1225. item->set_editable(0, true);
  1226. item->set_checked(0, true);
  1227. name = name.validate_filename();
  1228. String path = p_path.path_join(name);
  1229. if (external_extension_type->get_selected() == 0) {
  1230. path += ".tres";
  1231. } else {
  1232. path += ".res";
  1233. }
  1234. item->set_text(1, path);
  1235. if (FileAccess::exists(path)) {
  1236. item->set_text(2, TTR("Warning: File exists"));
  1237. item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced."));
  1238. item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
  1239. } else {
  1240. item->set_text(2, TTR("Will create new file"));
  1241. item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
  1242. }
  1243. item->add_button(1, get_editor_theme_icon(SNAME("Folder")));
  1244. }
  1245. } else {
  1246. item->set_text(2, TTR("No import ID"));
  1247. item->set_tooltip_text(2, TTR("Material has no name nor any other way to identify on re-import.\nPlease name it or ensure it is exported with an unique ID."));
  1248. item->set_icon(2, get_editor_theme_icon(SNAME("StatusError")));
  1249. }
  1250. save_path_items.push_back(item);
  1251. }
  1252. external_paths->set_title(TTR("Extract Materials to Resource Files"));
  1253. external_paths->set_ok_button_text(TTR("Extract"));
  1254. } break;
  1255. case ACTION_CHOOSE_MESH_SAVE_PATHS: {
  1256. for (const KeyValue<String, MeshData> &E : mesh_map) {
  1257. MeshData &md = mesh_map[E.key];
  1258. TreeItem *item = external_path_tree->create_item(root);
  1259. String name = md.mesh_node->get_text(0);
  1260. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  1261. item->set_icon(0, get_editor_theme_icon(SNAME("MeshItem")));
  1262. item->set_text(0, name);
  1263. if (md.has_import_id) {
  1264. if (md.settings.has("save_to_file/enabled") && bool(md.settings["save_to_file/enabled"])) {
  1265. item->set_text(2, TTR("Already Saving"));
  1266. item->set_tooltip_text(2, TTR("This mesh already saves to an external resource, no action will be taken."));
  1267. } else {
  1268. item->set_metadata(0, E.key);
  1269. item->set_editable(0, true);
  1270. item->set_checked(0, true);
  1271. name = name.validate_filename();
  1272. String path = p_path.path_join(name);
  1273. if (external_extension_type->get_selected() == 0) {
  1274. path += ".tres";
  1275. } else {
  1276. path += ".res";
  1277. }
  1278. item->set_text(1, path);
  1279. if (FileAccess::exists(path)) {
  1280. item->set_text(2, TTR("Warning: File exists"));
  1281. item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced on import."));
  1282. item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
  1283. } else {
  1284. item->set_text(2, TTR("Will save to new file"));
  1285. item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
  1286. }
  1287. item->add_button(1, get_editor_theme_icon(SNAME("Folder")));
  1288. }
  1289. } else {
  1290. item->set_text(2, TTR("No import ID"));
  1291. item->set_tooltip_text(2, TTR("Mesh has no name nor any other way to identify on re-import.\nPlease name it or ensure it is exported with an unique ID."));
  1292. item->set_icon(2, get_editor_theme_icon(SNAME("StatusError")));
  1293. }
  1294. save_path_items.push_back(item);
  1295. }
  1296. external_paths->set_title(TTR("Set paths to save meshes as resource files on Reimport"));
  1297. external_paths->set_ok_button_text(TTR("Set Paths"));
  1298. } break;
  1299. case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
  1300. for (const KeyValue<String, AnimationData> &E : animation_map) {
  1301. AnimationData &ad = animation_map[E.key];
  1302. TreeItem *item = external_path_tree->create_item(root);
  1303. String name = ad.scene_node->get_text(0);
  1304. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  1305. item->set_icon(0, get_editor_theme_icon(SNAME("Animation")));
  1306. item->set_text(0, name);
  1307. if (ad.settings.has("save_to_file/enabled") && bool(ad.settings["save_to_file/enabled"])) {
  1308. item->set_text(2, TTR("Already Saving"));
  1309. item->set_tooltip_text(2, TTR("This animation already saves to an external resource, no action will be taken."));
  1310. } else {
  1311. item->set_metadata(0, E.key);
  1312. item->set_editable(0, true);
  1313. item->set_checked(0, true);
  1314. name = name.validate_filename();
  1315. String path = p_path.path_join(name);
  1316. if (external_extension_type->get_selected() == 0) {
  1317. path += ".tres";
  1318. } else {
  1319. path += ".res";
  1320. }
  1321. item->set_text(1, path);
  1322. if (FileAccess::exists(path)) {
  1323. item->set_text(2, TTR("Warning: File exists"));
  1324. item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced on import."));
  1325. item->set_icon(2, get_editor_theme_icon(SNAME("StatusWarning")));
  1326. } else {
  1327. item->set_text(2, TTR("Will save to new file"));
  1328. item->set_icon(2, get_editor_theme_icon(SNAME("StatusSuccess")));
  1329. }
  1330. item->add_button(1, get_editor_theme_icon(SNAME("Folder")));
  1331. }
  1332. save_path_items.push_back(item);
  1333. }
  1334. external_paths->set_title(TTR("Set paths to save animations as resource files on Reimport"));
  1335. external_paths->set_ok_button_text(TTR("Set Paths"));
  1336. } break;
  1337. }
  1338. external_paths->popup_centered_ratio();
  1339. }
  1340. void SceneImportSettingsDialog::_save_dir_confirm() {
  1341. for (int i = 0; i < save_path_items.size(); i++) {
  1342. TreeItem *item = save_path_items[i];
  1343. if (!item->is_checked(0)) {
  1344. continue; //ignore
  1345. }
  1346. String path = item->get_text(1);
  1347. String uid_path = path;
  1348. if (path.begins_with("uid://")) {
  1349. path = ResourceUID::uid_to_path(uid_path);
  1350. }
  1351. if (!path.is_resource_file()) {
  1352. continue;
  1353. }
  1354. String id = item->get_metadata(0);
  1355. switch (current_action) {
  1356. case ACTION_EXTRACT_MATERIALS: {
  1357. ERR_CONTINUE(!material_map.has(id));
  1358. MaterialData &md = material_map[id];
  1359. Error err = ResourceSaver::save(md.material, path);
  1360. if (err != OK) {
  1361. EditorNode::get_singleton()->add_io_error(TTR("Can't make material external to file, write error:") + "\n\t" + path);
  1362. continue;
  1363. }
  1364. uid_path = ResourceUID::path_to_uid(path);
  1365. md.settings["use_external/enabled"] = true;
  1366. md.settings["use_external/path"] = uid_path;
  1367. md.settings["use_external/fallback_path"] = path;
  1368. } break;
  1369. case ACTION_CHOOSE_MESH_SAVE_PATHS: {
  1370. ERR_CONTINUE(!mesh_map.has(id));
  1371. MeshData &md = mesh_map[id];
  1372. md.settings["save_to_file/enabled"] = true;
  1373. md.settings["save_to_file/path"] = uid_path;
  1374. md.settings["save_to_file/fallback_path"] = path;
  1375. } break;
  1376. case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
  1377. ERR_CONTINUE(!animation_map.has(id));
  1378. AnimationData &ad = animation_map[id];
  1379. ad.settings["save_to_file/enabled"] = true;
  1380. ad.settings["save_to_file/path"] = uid_path;
  1381. ad.settings["save_to_file/fallback_path"] = path;
  1382. } break;
  1383. }
  1384. }
  1385. if (current_action == ACTION_EXTRACT_MATERIALS) {
  1386. //as this happens right now, the scene needs to be saved and reimported.
  1387. _re_import();
  1388. open_settings(base_path);
  1389. } else {
  1390. scene_import_settings_data->notify_property_list_changed();
  1391. }
  1392. }
  1393. SceneImportSettingsDialog::SceneImportSettingsDialog() {
  1394. singleton = this;
  1395. VBoxContainer *main_vb = memnew(VBoxContainer);
  1396. add_child(main_vb);
  1397. HBoxContainer *menu_hb = memnew(HBoxContainer);
  1398. main_vb->add_child(menu_hb);
  1399. action_menu = memnew(MenuButton);
  1400. action_menu->set_text(TTR("Actions..."));
  1401. // Style the MenuButton like a regular Button to make it more noticeable.
  1402. action_menu->set_flat(false);
  1403. action_menu->set_focus_mode(Control::FOCUS_ALL);
  1404. menu_hb->add_child(action_menu);
  1405. action_menu->get_popup()->add_item(TTR("Extract Materials"), ACTION_EXTRACT_MATERIALS);
  1406. action_menu->get_popup()->add_separator();
  1407. action_menu->get_popup()->add_item(TTR("Set Animation Save Paths"), ACTION_CHOOSE_ANIMATION_SAVE_PATHS);
  1408. action_menu->get_popup()->add_item(TTR("Set Mesh Save Paths"), ACTION_CHOOSE_MESH_SAVE_PATHS);
  1409. action_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &SceneImportSettingsDialog::_menu_callback));
  1410. tree_split = memnew(HSplitContainer);
  1411. main_vb->add_child(tree_split);
  1412. tree_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1413. data_mode = memnew(TabContainer);
  1414. tree_split->add_child(data_mode);
  1415. data_mode->set_custom_minimum_size(Size2(300 * EDSCALE, 0));
  1416. data_mode->set_theme_type_variation("TabContainerOdd");
  1417. property_split = memnew(HSplitContainer);
  1418. tree_split->add_child(property_split);
  1419. property_split->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1420. scene_tree = memnew(Tree);
  1421. scene_tree->set_name(TTR("Scene"));
  1422. scene_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1423. data_mode->add_child(scene_tree);
  1424. scene_tree->connect("cell_selected", callable_mp(this, &SceneImportSettingsDialog::_scene_tree_selected));
  1425. mesh_tree = memnew(Tree);
  1426. mesh_tree->set_name(TTR("Meshes"));
  1427. mesh_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1428. data_mode->add_child(mesh_tree);
  1429. mesh_tree->set_hide_root(true);
  1430. mesh_tree->connect("cell_selected", callable_mp(this, &SceneImportSettingsDialog::_mesh_tree_selected));
  1431. material_tree = memnew(Tree);
  1432. material_tree->set_name(TTR("Materials"));
  1433. material_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1434. data_mode->add_child(material_tree);
  1435. material_tree->connect("cell_selected", callable_mp(this, &SceneImportSettingsDialog::_material_tree_selected));
  1436. material_tree->set_hide_root(true);
  1437. VBoxContainer *vp_vb = memnew(VBoxContainer);
  1438. vp_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1439. vp_vb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1440. vp_vb->set_anchors_and_offsets_preset(Control::LayoutPreset::PRESET_FULL_RECT);
  1441. property_split->add_child(vp_vb);
  1442. SubViewportContainer *vp_container = memnew(SubViewportContainer);
  1443. vp_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1444. vp_container->set_custom_minimum_size(Size2(10, 10));
  1445. vp_container->set_stretch(true);
  1446. vp_container->connect(SceneStringName(gui_input), callable_mp(this, &SceneImportSettingsDialog::_viewport_input));
  1447. vp_vb->add_child(vp_container);
  1448. base_viewport = memnew(SubViewport);
  1449. vp_container->add_child(base_viewport);
  1450. animation_preview = memnew(PanelContainer);
  1451. animation_preview->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1452. vp_vb->add_child(animation_preview);
  1453. animation_preview->hide();
  1454. HBoxContainer *animation_hbox = memnew(HBoxContainer);
  1455. animation_preview->add_child(animation_hbox);
  1456. animation_play_button = memnew(Button);
  1457. animation_hbox->add_child(animation_play_button);
  1458. animation_play_button->set_flat(true);
  1459. animation_play_button->set_accessibility_name(TTRC("Play"));
  1460. animation_play_button->set_focus_mode(Control::FOCUS_NONE);
  1461. animation_play_button->set_shortcut(ED_SHORTCUT("scene_import_settings/play_selected_animation", TTRC("Selected Animation Play/Pause"), Key::SPACE));
  1462. animation_play_button->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_play_animation));
  1463. animation_stop_button = memnew(Button);
  1464. animation_hbox->add_child(animation_stop_button);
  1465. animation_stop_button->set_flat(true);
  1466. animation_stop_button->set_accessibility_name(TTRC("Stop"));
  1467. animation_stop_button->set_focus_mode(Control::FOCUS_NONE);
  1468. animation_stop_button->set_tooltip_text(TTR("Selected Animation Stop"));
  1469. animation_stop_button->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_stop_current_animation));
  1470. animation_slider = memnew(HSlider);
  1471. animation_hbox->add_child(animation_slider);
  1472. animation_slider->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1473. animation_slider->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1474. animation_slider->set_max(1.0);
  1475. animation_slider->set_step(1.0 / 100.0);
  1476. animation_slider->set_value_no_signal(0.0);
  1477. animation_slider->set_focus_mode(Control::FOCUS_NONE);
  1478. animation_slider->set_accessibility_name(TTRC("Animation"));
  1479. animation_slider->connect(SceneStringName(value_changed), callable_mp(this, &SceneImportSettingsDialog::_animation_slider_value_changed));
  1480. animation_toggle_skeleton_visibility = memnew(Button);
  1481. animation_hbox->add_child(animation_toggle_skeleton_visibility);
  1482. animation_toggle_skeleton_visibility->set_toggle_mode(true);
  1483. animation_toggle_skeleton_visibility->set_theme_type_variation("FlatButton");
  1484. animation_toggle_skeleton_visibility->set_focus_mode(Control::FOCUS_NONE);
  1485. animation_toggle_skeleton_visibility->set_tooltip_text(TTR("Toggle Animation Skeleton Visibility"));
  1486. animation_toggle_skeleton_visibility->set_accessibility_name(TTRC("Skeleton Visibility"));
  1487. animation_toggle_skeleton_visibility->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_animation_update_skeleton_visibility));
  1488. base_viewport->set_use_own_world_3d(true);
  1489. HBoxContainer *viewport_hbox = memnew(HBoxContainer);
  1490. vp_container->add_child(viewport_hbox);
  1491. viewport_hbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, 2);
  1492. viewport_hbox->add_spacer();
  1493. VBoxContainer *vb_light = memnew(VBoxContainer);
  1494. vb_light->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1495. viewport_hbox->add_child(vb_light);
  1496. light_rotate_switch = memnew(Button);
  1497. light_rotate_switch->set_theme_type_variation("PreviewLightButton");
  1498. light_rotate_switch->set_toggle_mode(true);
  1499. light_rotate_switch->set_pressed(true);
  1500. light_rotate_switch->set_tooltip_text(TTR("Rotate Lights With Model"));
  1501. light_rotate_switch->set_accessibility_name(TTRC("Rotate Lights With Model"));
  1502. light_rotate_switch->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_on_light_rotate_switch_pressed));
  1503. vb_light->add_child(light_rotate_switch);
  1504. light_1_switch = memnew(Button);
  1505. light_1_switch->set_theme_type_variation("PreviewLightButton");
  1506. light_1_switch->set_toggle_mode(true);
  1507. light_1_switch->set_pressed(true);
  1508. light_1_switch->set_tooltip_text(TTR("Primary Light"));
  1509. light_1_switch->set_accessibility_name(TTRC("Primary Light"));
  1510. light_1_switch->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_on_light_1_switch_pressed));
  1511. vb_light->add_child(light_1_switch);
  1512. light_2_switch = memnew(Button);
  1513. light_2_switch->set_theme_type_variation("PreviewLightButton");
  1514. light_2_switch->set_toggle_mode(true);
  1515. light_2_switch->set_pressed(true);
  1516. light_2_switch->set_tooltip_text(TTR("Secondary Light"));
  1517. light_2_switch->set_accessibility_name(TTRC("Secondary Light"));
  1518. light_2_switch->connect(SceneStringName(pressed), callable_mp(this, &SceneImportSettingsDialog::_on_light_2_switch_pressed));
  1519. vb_light->add_child(light_2_switch);
  1520. camera = memnew(Camera3D);
  1521. base_viewport->add_child(camera);
  1522. camera->make_current();
  1523. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  1524. camera_attributes.instantiate();
  1525. camera->set_attributes(camera_attributes);
  1526. }
  1527. // Use a grayscale gradient sky to avoid skewing the preview towards a specific color,
  1528. // but still allow shaded areas to be easily distinguished (using the ambient and reflected light).
  1529. // This also helps the user orient themselves in the preview, since the bottom of the sky is black
  1530. // and the top of the sky is white.
  1531. procedural_sky_material.instantiate();
  1532. procedural_sky_material->set_sky_top_color(Color(1, 1, 1));
  1533. procedural_sky_material->set_sky_horizon_color(Color(0.5, 0.5, 0.5));
  1534. procedural_sky_material->set_ground_horizon_color(Color(0.5, 0.5, 0.5));
  1535. procedural_sky_material->set_ground_bottom_color(Color(0, 0, 0));
  1536. procedural_sky_material->set_sky_curve(2.0);
  1537. procedural_sky_material->set_ground_curve(0.5);
  1538. // Hide the sun from the sky.
  1539. procedural_sky_material->set_sun_angle_max(0.0);
  1540. sky.instantiate();
  1541. sky->set_material(procedural_sky_material);
  1542. environment.instantiate();
  1543. environment->set_background(Environment::BG_SKY);
  1544. environment->set_sky(sky);
  1545. // A custom FOV must be specified, as an orthogonal camera is used for the preview.
  1546. environment->set_sky_custom_fov(50.0);
  1547. camera->set_environment(environment);
  1548. light1 = memnew(DirectionalLight3D);
  1549. light1->set_transform(Transform3D(Basis::looking_at(Vector3(-1, -1, -1))));
  1550. light1->set_shadow(true);
  1551. camera->add_child(light1);
  1552. light2 = memnew(DirectionalLight3D);
  1553. light2->set_transform(Transform3D(Basis::looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1))));
  1554. light2->set_color(Color(0.5f, 0.5f, 0.5f));
  1555. camera->add_child(light2);
  1556. {
  1557. Ref<StandardMaterial3D> selection_mat;
  1558. selection_mat.instantiate();
  1559. selection_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1560. selection_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1561. selection_mat->set_albedo(Color(1, 0.8, 1.0));
  1562. Ref<SurfaceTool> st;
  1563. st.instantiate();
  1564. st->begin(Mesh::PRIMITIVE_LINES);
  1565. AABB base_aabb;
  1566. base_aabb.size = Vector3(1, 1, 1);
  1567. for (int i = 0; i < 12; i++) {
  1568. Vector3 a, b;
  1569. base_aabb.get_edge(i, a, b);
  1570. st->add_vertex(a);
  1571. st->add_vertex(a.lerp(b, 0.2));
  1572. st->add_vertex(b);
  1573. st->add_vertex(b.lerp(a, 0.2));
  1574. }
  1575. selection_mesh.instantiate();
  1576. st->commit(selection_mesh);
  1577. selection_mesh->surface_set_material(0, selection_mat);
  1578. node_selected = memnew(MeshInstance3D);
  1579. node_selected->set_mesh(selection_mesh);
  1580. node_selected->set_cast_shadows_setting(GeometryInstance3D::SHADOW_CASTING_SETTING_OFF);
  1581. base_viewport->add_child(node_selected);
  1582. node_selected->hide();
  1583. }
  1584. {
  1585. mesh_preview = memnew(MeshInstance3D);
  1586. base_viewport->add_child(mesh_preview);
  1587. mesh_preview->hide();
  1588. material_preview.instantiate();
  1589. }
  1590. {
  1591. collider_mat.instantiate();
  1592. collider_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1593. collider_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1594. collider_mat->set_albedo(Color(0.5, 0.5, 1.0));
  1595. }
  1596. {
  1597. bones_mesh_preview = memnew(MeshInstance3D);
  1598. bones_mesh_preview->set_cast_shadows_setting(GeometryInstance3D::SHADOW_CASTING_SETTING_OFF);
  1599. bones_mesh_preview->set_skeleton_path(NodePath());
  1600. base_viewport->add_child(bones_mesh_preview);
  1601. }
  1602. inspector = memnew(EditorInspector);
  1603. inspector->set_custom_minimum_size(Size2(300 * EDSCALE, 0));
  1604. inspector->connect(SNAME("property_edited"), callable_mp(this, &SceneImportSettingsDialog::_inspector_property_edited));
  1605. // Display the same tooltips as in the Import dock.
  1606. inspector->set_object_class(ResourceImporterScene::get_class_static());
  1607. inspector->set_use_doc_hints(true);
  1608. property_split->add_child(inspector);
  1609. scene_import_settings_data = memnew(SceneImportSettingsData);
  1610. set_ok_button_text(TTR("Reimport"));
  1611. set_cancel_button_text(TTR("Close"));
  1612. external_paths = memnew(ConfirmationDialog);
  1613. add_child(external_paths);
  1614. external_path_tree = memnew(Tree);
  1615. external_paths->add_child(external_path_tree);
  1616. external_path_tree->connect("button_clicked", callable_mp(this, &SceneImportSettingsDialog::_browse_save_callback));
  1617. external_paths->connect(SceneStringName(confirmed), callable_mp(this, &SceneImportSettingsDialog::_save_dir_confirm));
  1618. external_path_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1619. external_path_tree->set_columns(3);
  1620. external_path_tree->set_column_titles_visible(true);
  1621. external_path_tree->set_column_expand(0, true);
  1622. external_path_tree->set_column_custom_minimum_width(0, 100 * EDSCALE);
  1623. external_path_tree->set_column_title(0, TTR("Resource"));
  1624. external_path_tree->set_column_expand(1, true);
  1625. external_path_tree->set_column_custom_minimum_width(1, 100 * EDSCALE);
  1626. external_path_tree->set_column_title(1, TTR("Path"));
  1627. external_path_tree->set_column_expand(2, false);
  1628. external_path_tree->set_column_custom_minimum_width(2, 200 * EDSCALE);
  1629. external_path_tree->set_column_title(2, TTR("Status"));
  1630. save_path = memnew(EditorFileDialog);
  1631. save_path->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
  1632. HBoxContainer *extension_hb = memnew(HBoxContainer);
  1633. save_path->get_vbox()->add_child(extension_hb);
  1634. extension_hb->add_spacer();
  1635. extension_hb->add_child(memnew(Label(TTR("Save Extension:"))));
  1636. external_extension_type = memnew(OptionButton);
  1637. extension_hb->add_child(external_extension_type);
  1638. external_extension_type->add_item(TTR("Text: *.tres"));
  1639. external_extension_type->add_item(TTR("Binary: *.res"));
  1640. external_path_tree->set_hide_root(true);
  1641. add_child(save_path);
  1642. item_save_path = memnew(EditorFileDialog);
  1643. item_save_path->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  1644. item_save_path->add_filter("*.tres", TTR("Text Resource"));
  1645. item_save_path->add_filter("*.res", TTR("Binary Resource"));
  1646. add_child(item_save_path);
  1647. item_save_path->connect("file_selected", callable_mp(this, &SceneImportSettingsDialog::_save_path_changed));
  1648. save_path->connect("dir_selected", callable_mp(this, &SceneImportSettingsDialog::_save_dir_callback));
  1649. update_view_timer = memnew(Timer);
  1650. update_view_timer->set_wait_time(0.2);
  1651. update_view_timer->connect("timeout", callable_mp(this, &SceneImportSettingsDialog::_update_view_gizmos));
  1652. add_child(update_view_timer);
  1653. }
  1654. SceneImportSettingsDialog::~SceneImportSettingsDialog() {
  1655. memdelete(scene_import_settings_data);
  1656. }