resource_importer_scene.cpp 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701
  1. /**************************************************************************/
  2. /* resource_importer_scene.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 "resource_importer_scene.h"
  31. #include "core/error/error_macros.h"
  32. #include "core/io/resource_saver.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/import/scene_import_settings.h"
  36. #include "scene/3d/area_3d.h"
  37. #include "scene/3d/collision_shape_3d.h"
  38. #include "scene/3d/importer_mesh_instance_3d.h"
  39. #include "scene/3d/mesh_instance_3d.h"
  40. #include "scene/3d/navigation_region_3d.h"
  41. #include "scene/3d/occluder_instance_3d.h"
  42. #include "scene/3d/physics_body_3d.h"
  43. #include "scene/3d/vehicle_body_3d.h"
  44. #include "scene/animation/animation_player.h"
  45. #include "scene/resources/animation.h"
  46. #include "scene/resources/box_shape_3d.h"
  47. #include "scene/resources/importer_mesh.h"
  48. #include "scene/resources/packed_scene.h"
  49. #include "scene/resources/resource_format_text.h"
  50. #include "scene/resources/separation_ray_shape_3d.h"
  51. #include "scene/resources/sphere_shape_3d.h"
  52. #include "scene/resources/surface_tool.h"
  53. #include "scene/resources/world_boundary_shape_3d.h"
  54. uint32_t EditorSceneFormatImporter::get_import_flags() const {
  55. uint32_t ret;
  56. if (GDVIRTUAL_CALL(_get_import_flags, ret)) {
  57. return ret;
  58. }
  59. ERR_FAIL_V(0);
  60. }
  61. void EditorSceneFormatImporter::get_extensions(List<String> *r_extensions) const {
  62. Vector<String> arr;
  63. if (GDVIRTUAL_CALL(_get_extensions, arr)) {
  64. for (int i = 0; i < arr.size(); i++) {
  65. r_extensions->push_back(arr[i]);
  66. }
  67. return;
  68. }
  69. ERR_FAIL();
  70. }
  71. Node *EditorSceneFormatImporter::import_scene(const String &p_path, uint32_t p_flags, const HashMap<StringName, Variant> &p_options, List<String> *r_missing_deps, Error *r_err) {
  72. Dictionary options_dict;
  73. for (const KeyValue<StringName, Variant> &elem : p_options) {
  74. options_dict[elem.key] = elem.value;
  75. }
  76. Object *ret = nullptr;
  77. if (GDVIRTUAL_CALL(_import_scene, p_path, p_flags, options_dict, ret)) {
  78. return Object::cast_to<Node>(ret);
  79. }
  80. ERR_FAIL_V(nullptr);
  81. }
  82. void EditorSceneFormatImporter::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options) {
  83. GDVIRTUAL_CALL(_get_import_options, p_path);
  84. }
  85. Variant EditorSceneFormatImporter::get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options) {
  86. Variant ret;
  87. GDVIRTUAL_CALL(_get_option_visibility, p_path, p_for_animation, p_option, ret);
  88. return ret;
  89. }
  90. void EditorSceneFormatImporter::_bind_methods() {
  91. GDVIRTUAL_BIND(_get_import_flags);
  92. GDVIRTUAL_BIND(_get_extensions);
  93. GDVIRTUAL_BIND(_import_scene, "path", "flags", "options");
  94. GDVIRTUAL_BIND(_get_import_options, "path");
  95. GDVIRTUAL_BIND(_get_option_visibility, "path", "for_animation", "option");
  96. BIND_CONSTANT(IMPORT_SCENE);
  97. BIND_CONSTANT(IMPORT_ANIMATION);
  98. BIND_CONSTANT(IMPORT_FAIL_ON_MISSING_DEPENDENCIES);
  99. BIND_CONSTANT(IMPORT_GENERATE_TANGENT_ARRAYS);
  100. BIND_CONSTANT(IMPORT_USE_NAMED_SKIN_BINDS);
  101. BIND_CONSTANT(IMPORT_DISCARD_MESHES_AND_MATERIALS);
  102. }
  103. /////////////////////////////////
  104. void EditorScenePostImport::_bind_methods() {
  105. GDVIRTUAL_BIND(_post_import, "scene")
  106. ClassDB::bind_method(D_METHOD("get_source_file"), &EditorScenePostImport::get_source_file);
  107. }
  108. Node *EditorScenePostImport::post_import(Node *p_scene) {
  109. Object *ret;
  110. if (GDVIRTUAL_CALL(_post_import, p_scene, ret)) {
  111. return Object::cast_to<Node>(ret);
  112. }
  113. return p_scene;
  114. }
  115. String EditorScenePostImport::get_source_file() const {
  116. return source_file;
  117. }
  118. void EditorScenePostImport::init(const String &p_source_file) {
  119. source_file = p_source_file;
  120. }
  121. EditorScenePostImport::EditorScenePostImport() {
  122. }
  123. ///////////////////////////////////////////////////////
  124. Variant EditorScenePostImportPlugin::get_option_value(const StringName &p_name) const {
  125. ERR_FAIL_COND_V_MSG(current_options == nullptr && current_options_dict == nullptr, Variant(), "get_option_value called from a function where option values are not available.");
  126. ERR_FAIL_COND_V_MSG(current_options && !current_options->has(p_name), Variant(), "get_option_value called with unexisting option argument: " + String(p_name));
  127. ERR_FAIL_COND_V_MSG(current_options_dict && !current_options_dict->has(p_name), Variant(), "get_option_value called with unexisting option argument: " + String(p_name));
  128. if (current_options && current_options->has(p_name)) {
  129. return (*current_options)[p_name];
  130. }
  131. if (current_options_dict && current_options_dict->has(p_name)) {
  132. return (*current_options_dict)[p_name];
  133. }
  134. return Variant();
  135. }
  136. void EditorScenePostImportPlugin::add_import_option(const String &p_name, Variant p_default_value) {
  137. ERR_FAIL_COND_MSG(current_option_list == nullptr, "add_import_option() can only be called from get_import_options()");
  138. add_import_option_advanced(p_default_value.get_type(), p_name, p_default_value);
  139. }
  140. void EditorScenePostImportPlugin::add_import_option_advanced(Variant::Type p_type, const String &p_name, Variant p_default_value, PropertyHint p_hint, const String &p_hint_string, int p_usage_flags) {
  141. ERR_FAIL_COND_MSG(current_option_list == nullptr, "add_import_option_advanced() can only be called from get_import_options()");
  142. current_option_list->push_back(ResourceImporter::ImportOption(PropertyInfo(p_type, p_name, p_hint, p_hint_string, p_usage_flags), p_default_value));
  143. }
  144. void EditorScenePostImportPlugin::get_internal_import_options(InternalImportCategory p_category, List<ResourceImporter::ImportOption> *r_options) {
  145. current_option_list = r_options;
  146. GDVIRTUAL_CALL(_get_internal_import_options, p_category);
  147. current_option_list = nullptr;
  148. }
  149. Variant EditorScenePostImportPlugin::get_internal_option_visibility(InternalImportCategory p_category, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  150. current_options = &p_options;
  151. Variant ret;
  152. GDVIRTUAL_CALL(_get_internal_option_visibility, p_category, p_for_animation, p_option, ret);
  153. current_options = nullptr;
  154. return ret;
  155. }
  156. Variant EditorScenePostImportPlugin::get_internal_option_update_view_required(InternalImportCategory p_category, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  157. current_options = &p_options;
  158. Variant ret;
  159. GDVIRTUAL_CALL(_get_internal_option_update_view_required, p_category, p_option, ret);
  160. current_options = nullptr;
  161. return ret;
  162. }
  163. void EditorScenePostImportPlugin::internal_process(InternalImportCategory p_category, Node *p_base_scene, Node *p_node, Ref<Resource> p_resource, const Dictionary &p_options) {
  164. current_options_dict = &p_options;
  165. GDVIRTUAL_CALL(_internal_process, p_category, p_base_scene, p_node, p_resource);
  166. current_options_dict = nullptr;
  167. }
  168. void EditorScenePostImportPlugin::get_import_options(const String &p_path, List<ResourceImporter::ImportOption> *r_options) {
  169. current_option_list = r_options;
  170. GDVIRTUAL_CALL(_get_import_options, p_path);
  171. current_option_list = nullptr;
  172. }
  173. Variant EditorScenePostImportPlugin::get_option_visibility(const String &p_path, bool p_for_animation, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  174. current_options = &p_options;
  175. Variant ret;
  176. GDVIRTUAL_CALL(_get_option_visibility, p_path, p_for_animation, p_option, ret);
  177. current_options = nullptr;
  178. return ret;
  179. }
  180. void EditorScenePostImportPlugin::pre_process(Node *p_scene, const HashMap<StringName, Variant> &p_options) {
  181. current_options = &p_options;
  182. GDVIRTUAL_CALL(_pre_process, p_scene);
  183. current_options = nullptr;
  184. }
  185. void EditorScenePostImportPlugin::post_process(Node *p_scene, const HashMap<StringName, Variant> &p_options) {
  186. current_options = &p_options;
  187. GDVIRTUAL_CALL(_post_process, p_scene);
  188. current_options = nullptr;
  189. }
  190. void EditorScenePostImportPlugin::_bind_methods() {
  191. ClassDB::bind_method(D_METHOD("get_option_value", "name"), &EditorScenePostImportPlugin::get_option_value);
  192. ClassDB::bind_method(D_METHOD("add_import_option", "name", "value"), &EditorScenePostImportPlugin::add_import_option);
  193. ClassDB::bind_method(D_METHOD("add_import_option_advanced", "type", "name", "default_value", "hint", "hint_string", "usage_flags"), &EditorScenePostImportPlugin::add_import_option_advanced, DEFVAL(PROPERTY_HINT_NONE), DEFVAL(""), DEFVAL(PROPERTY_USAGE_DEFAULT));
  194. GDVIRTUAL_BIND(_get_internal_import_options, "category");
  195. GDVIRTUAL_BIND(_get_internal_option_visibility, "category", "for_animation", "option");
  196. GDVIRTUAL_BIND(_get_internal_option_update_view_required, "category", "option");
  197. GDVIRTUAL_BIND(_internal_process, "category", "base_node", "node", "resource");
  198. GDVIRTUAL_BIND(_get_import_options, "path");
  199. GDVIRTUAL_BIND(_get_option_visibility, "path", "for_animation", "option");
  200. GDVIRTUAL_BIND(_pre_process, "scene");
  201. GDVIRTUAL_BIND(_post_process, "scene");
  202. BIND_ENUM_CONSTANT(INTERNAL_IMPORT_CATEGORY_NODE);
  203. BIND_ENUM_CONSTANT(INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE);
  204. BIND_ENUM_CONSTANT(INTERNAL_IMPORT_CATEGORY_MESH);
  205. BIND_ENUM_CONSTANT(INTERNAL_IMPORT_CATEGORY_MATERIAL);
  206. BIND_ENUM_CONSTANT(INTERNAL_IMPORT_CATEGORY_ANIMATION);
  207. BIND_ENUM_CONSTANT(INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE);
  208. BIND_ENUM_CONSTANT(INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE);
  209. BIND_ENUM_CONSTANT(INTERNAL_IMPORT_CATEGORY_MAX);
  210. }
  211. /////////////////////////////////////////////////////////
  212. String ResourceImporterScene::get_importer_name() const {
  213. return animation_importer ? "animation_library" : "scene";
  214. }
  215. String ResourceImporterScene::get_visible_name() const {
  216. return animation_importer ? "Animation Library" : "Scene";
  217. }
  218. void ResourceImporterScene::get_recognized_extensions(List<String> *p_extensions) const {
  219. for (Ref<EditorSceneFormatImporter> importer_elem : importers) {
  220. importer_elem->get_extensions(p_extensions);
  221. }
  222. }
  223. String ResourceImporterScene::get_save_extension() const {
  224. return animation_importer ? "res" : "scn";
  225. }
  226. String ResourceImporterScene::get_resource_type() const {
  227. return animation_importer ? "AnimationLibrary" : "PackedScene";
  228. }
  229. int ResourceImporterScene::get_format_version() const {
  230. return 1;
  231. }
  232. bool ResourceImporterScene::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  233. if (animation_importer) {
  234. if (p_option == "animation/import") { // Option ignored, animation always imported.
  235. return false;
  236. }
  237. } else if (p_option.begins_with("animation/")) {
  238. if (p_option != "animation/import" && !bool(p_options["animation/import"])) {
  239. return false;
  240. }
  241. }
  242. if (animation_importer && (p_option.begins_with("nodes/") || p_option.begins_with("meshes/") || p_option.begins_with("skins/"))) {
  243. return false; // Nothing to do here for animations.
  244. }
  245. if (p_option == "meshes/lightmap_texel_size" && int(p_options["meshes/light_baking"]) != 2) {
  246. // Only display the lightmap texel size import option when using the Static Lightmaps light baking mode.
  247. return false;
  248. }
  249. for (int i = 0; i < post_importer_plugins.size(); i++) {
  250. Variant ret = post_importer_plugins.write[i]->get_option_visibility(p_path, animation_importer, p_option, p_options);
  251. if (ret.get_type() == Variant::BOOL) {
  252. return ret;
  253. }
  254. }
  255. for (Ref<EditorSceneFormatImporter> importer : importers) {
  256. Variant ret = importer->get_option_visibility(p_path, animation_importer, p_option, p_options);
  257. if (ret.get_type() == Variant::BOOL) {
  258. return ret;
  259. }
  260. }
  261. return true;
  262. }
  263. int ResourceImporterScene::get_preset_count() const {
  264. return 0;
  265. }
  266. String ResourceImporterScene::get_preset_name(int p_idx) const {
  267. return String();
  268. }
  269. static bool _teststr(const String &p_what, const String &p_str) {
  270. String what = p_what;
  271. // Remove trailing spaces and numbers, some apps like blender add ".number" to duplicates
  272. // (dot is replaced with _ as invalid character) so also compensate for this.
  273. while (what.length() && (is_digit(what[what.length() - 1]) || what[what.length() - 1] <= 32 || what[what.length() - 1] == '_')) {
  274. what = what.substr(0, what.length() - 1);
  275. }
  276. if (what.findn("$" + p_str) != -1) { //blender and other stuff
  277. return true;
  278. }
  279. if (what.to_lower().ends_with("-" + p_str)) { //collada only supports "_" and "-" besides letters
  280. return true;
  281. }
  282. if (what.to_lower().ends_with("_" + p_str)) { //collada only supports "_" and "-" besides letters
  283. return true;
  284. }
  285. return false;
  286. }
  287. static String _fixstr(const String &p_what, const String &p_str) {
  288. String what = p_what;
  289. // Remove trailing spaces and numbers, some apps like blender add ".number" to duplicates
  290. // (dot is replaced with _ as invalid character) so also compensate for this.
  291. while (what.length() && (is_digit(what[what.length() - 1]) || what[what.length() - 1] <= 32 || what[what.length() - 1] == '_')) {
  292. what = what.substr(0, what.length() - 1);
  293. }
  294. String end = p_what.substr(what.length(), p_what.length() - what.length());
  295. if (what.findn("$" + p_str) != -1) { //blender and other stuff
  296. return what.replace("$" + p_str, "") + end;
  297. }
  298. if (what.to_lower().ends_with("-" + p_str)) { //collada only supports "_" and "-" besides letters
  299. return what.substr(0, what.length() - (p_str.length() + 1)) + end;
  300. }
  301. if (what.to_lower().ends_with("_" + p_str)) { //collada only supports "_" and "-" besides letters
  302. return what.substr(0, what.length() - (p_str.length() + 1)) + end;
  303. }
  304. return what;
  305. }
  306. static void _pre_gen_shape_list(Ref<ImporterMesh> &mesh, Vector<Ref<Shape3D>> &r_shape_list, bool p_convex) {
  307. ERR_FAIL_NULL_MSG(mesh, "Cannot generate shape list with null mesh value");
  308. if (!p_convex) {
  309. Ref<ConcavePolygonShape3D> shape = mesh->create_trimesh_shape();
  310. r_shape_list.push_back(shape);
  311. } else {
  312. Vector<Ref<Shape3D>> cd;
  313. cd.push_back(mesh->create_convex_shape(true, /*Passing false, otherwise VHACD will be used to simplify (Decompose) the Mesh.*/ false));
  314. if (cd.size()) {
  315. for (int i = 0; i < cd.size(); i++) {
  316. r_shape_list.push_back(cd[i]);
  317. }
  318. }
  319. }
  320. }
  321. struct ScalableNodeCollection {
  322. HashSet<Node3D *> node_3ds;
  323. HashSet<Ref<ImporterMesh>> importer_meshes;
  324. HashSet<Ref<Skin>> skins;
  325. HashSet<Ref<Animation>> animations;
  326. };
  327. void _rescale_importer_mesh(Vector3 p_scale, Ref<ImporterMesh> p_mesh, bool is_shadow = false) {
  328. // MESH and SKIN data divide, to compensate for object position multiplying.
  329. const int surf_count = p_mesh->get_surface_count();
  330. const int blendshape_count = p_mesh->get_blend_shape_count();
  331. struct LocalSurfData {
  332. Mesh::PrimitiveType prim = {};
  333. Array arr;
  334. Array bsarr;
  335. Dictionary lods;
  336. String name;
  337. Ref<Material> mat;
  338. int fmt_compress_flags = 0;
  339. };
  340. Vector<LocalSurfData> surf_data_by_mesh;
  341. Vector<String> blendshape_names;
  342. for (int bsidx = 0; bsidx < blendshape_count; bsidx++) {
  343. blendshape_names.append(p_mesh->get_blend_shape_name(bsidx));
  344. }
  345. for (int surf_idx = 0; surf_idx < surf_count; surf_idx++) {
  346. Mesh::PrimitiveType prim = p_mesh->get_surface_primitive_type(surf_idx);
  347. const int fmt_compress_flags = p_mesh->get_surface_format(surf_idx);
  348. Array arr = p_mesh->get_surface_arrays(surf_idx);
  349. String name = p_mesh->get_surface_name(surf_idx);
  350. Dictionary lods;
  351. Ref<Material> mat = p_mesh->get_surface_material(surf_idx);
  352. {
  353. Vector<Vector3> vertex_array = arr[ArrayMesh::ARRAY_VERTEX];
  354. for (int vert_arr_i = 0; vert_arr_i < vertex_array.size(); vert_arr_i++) {
  355. vertex_array.write[vert_arr_i] = vertex_array[vert_arr_i] * p_scale;
  356. }
  357. arr[ArrayMesh::ARRAY_VERTEX] = vertex_array;
  358. }
  359. Array blendshapes;
  360. for (int bsidx = 0; bsidx < blendshape_count; bsidx++) {
  361. Array current_bsarr = p_mesh->get_surface_blend_shape_arrays(surf_idx, bsidx);
  362. Vector<Vector3> current_bs_vertex_array = current_bsarr[ArrayMesh::ARRAY_VERTEX];
  363. int current_bs_vert_arr_len = current_bs_vertex_array.size();
  364. for (int32_t bs_vert_arr_i = 0; bs_vert_arr_i < current_bs_vert_arr_len; bs_vert_arr_i++) {
  365. current_bs_vertex_array.write[bs_vert_arr_i] = current_bs_vertex_array[bs_vert_arr_i] * p_scale;
  366. }
  367. current_bsarr[ArrayMesh::ARRAY_VERTEX] = current_bs_vertex_array;
  368. blendshapes.push_back(current_bsarr);
  369. }
  370. LocalSurfData surf_data_dictionary = LocalSurfData();
  371. surf_data_dictionary.prim = prim;
  372. surf_data_dictionary.arr = arr;
  373. surf_data_dictionary.bsarr = blendshapes;
  374. surf_data_dictionary.lods = lods;
  375. surf_data_dictionary.fmt_compress_flags = fmt_compress_flags;
  376. surf_data_dictionary.name = name;
  377. surf_data_dictionary.mat = mat;
  378. surf_data_by_mesh.push_back(surf_data_dictionary);
  379. }
  380. p_mesh->clear();
  381. for (int bsidx = 0; bsidx < blendshape_count; bsidx++) {
  382. p_mesh->add_blend_shape(blendshape_names[bsidx]);
  383. }
  384. for (int surf_idx = 0; surf_idx < surf_count; surf_idx++) {
  385. const Mesh::PrimitiveType prim = surf_data_by_mesh[surf_idx].prim;
  386. const Array arr = surf_data_by_mesh[surf_idx].arr;
  387. const Array bsarr = surf_data_by_mesh[surf_idx].bsarr;
  388. const Dictionary lods = surf_data_by_mesh[surf_idx].lods;
  389. const int fmt_compress_flags = surf_data_by_mesh[surf_idx].fmt_compress_flags;
  390. const String name = surf_data_by_mesh[surf_idx].name;
  391. const Ref<Material> mat = surf_data_by_mesh[surf_idx].mat;
  392. p_mesh->add_surface(prim, arr, bsarr, lods, mat, name, fmt_compress_flags);
  393. }
  394. if (!is_shadow && p_mesh->get_shadow_mesh() != p_mesh && p_mesh->get_shadow_mesh().is_valid()) {
  395. _rescale_importer_mesh(p_scale, p_mesh->get_shadow_mesh(), true);
  396. }
  397. }
  398. void _rescale_skin(Vector3 p_scale, Ref<Skin> p_skin) {
  399. // MESH and SKIN data divide, to compensate for object position multiplying.
  400. for (int i = 0; i < p_skin->get_bind_count(); i++) {
  401. Transform3D transform = p_skin->get_bind_pose(i);
  402. p_skin->set_bind_pose(i, Transform3D(transform.basis, p_scale * transform.origin));
  403. }
  404. }
  405. void _rescale_animation(Vector3 p_scale, Ref<Animation> p_animation) {
  406. for (int track_idx = 0; track_idx < p_animation->get_track_count(); track_idx++) {
  407. if (p_animation->track_get_type(track_idx) == Animation::TYPE_POSITION_3D) {
  408. for (int key_idx = 0; key_idx < p_animation->track_get_key_count(track_idx); key_idx++) {
  409. Vector3 value = p_animation->track_get_key_value(track_idx, key_idx);
  410. value = p_scale * value;
  411. p_animation->track_set_key_value(track_idx, key_idx, value);
  412. }
  413. }
  414. }
  415. }
  416. void _apply_scale_to_scalable_node_collection(ScalableNodeCollection &p_collection, Vector3 p_scale) {
  417. for (Node3D *node_3d : p_collection.node_3ds) {
  418. node_3d->set_position(p_scale * node_3d->get_position());
  419. Skeleton3D *skeleton_3d = Object::cast_to<Skeleton3D>(node_3d);
  420. if (skeleton_3d) {
  421. for (int i = 0; i < skeleton_3d->get_bone_count(); i++) {
  422. Transform3D rest = skeleton_3d->get_bone_rest(i);
  423. skeleton_3d->set_bone_rest(i, Transform3D(rest.basis, p_scale * rest.origin));
  424. skeleton_3d->set_bone_pose_position(i, p_scale * rest.origin);
  425. }
  426. }
  427. }
  428. for (Ref<ImporterMesh> mesh : p_collection.importer_meshes) {
  429. _rescale_importer_mesh(p_scale, mesh, false);
  430. }
  431. for (Ref<Skin> skin : p_collection.skins) {
  432. _rescale_skin(p_scale, skin);
  433. }
  434. for (Ref<Animation> animation : p_collection.animations) {
  435. _rescale_animation(p_scale, animation);
  436. }
  437. }
  438. void _populate_scalable_nodes_collection(Node *p_node, ScalableNodeCollection &p_collection) {
  439. if (!p_node) {
  440. return;
  441. }
  442. Node3D *node_3d = Object::cast_to<Node3D>(p_node);
  443. if (node_3d) {
  444. p_collection.node_3ds.insert(node_3d);
  445. ImporterMeshInstance3D *mesh_instance_3d = Object::cast_to<ImporterMeshInstance3D>(p_node);
  446. if (mesh_instance_3d) {
  447. Ref<ImporterMesh> mesh = mesh_instance_3d->get_mesh();
  448. if (mesh.is_valid()) {
  449. p_collection.importer_meshes.insert(mesh);
  450. }
  451. Ref<Skin> skin = mesh_instance_3d->get_skin();
  452. if (skin.is_valid()) {
  453. p_collection.skins.insert(skin);
  454. }
  455. }
  456. }
  457. AnimationPlayer *animation_player = Object::cast_to<AnimationPlayer>(p_node);
  458. if (animation_player) {
  459. List<StringName> animation_list;
  460. animation_player->get_animation_list(&animation_list);
  461. for (const StringName &E : animation_list) {
  462. Ref<Animation> animation = animation_player->get_animation(E);
  463. p_collection.animations.insert(animation);
  464. }
  465. }
  466. for (int i = 0; i < p_node->get_child_count(); i++) {
  467. Node *child = p_node->get_child(i);
  468. _populate_scalable_nodes_collection(child, p_collection);
  469. }
  470. }
  471. void _apply_permanent_scale_to_descendants(Node *p_root_node, Vector3 p_scale) {
  472. ScalableNodeCollection scalable_node_collection;
  473. _populate_scalable_nodes_collection(p_root_node, scalable_node_collection);
  474. _apply_scale_to_scalable_node_collection(scalable_node_collection, p_scale);
  475. }
  476. Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &r_collision_map, Pair<PackedVector3Array, PackedInt32Array> *r_occluder_arrays, List<Pair<NodePath, Node *>> &r_node_renames) {
  477. // Children first.
  478. for (int i = 0; i < p_node->get_child_count(); i++) {
  479. Node *r = _pre_fix_node(p_node->get_child(i), p_root, r_collision_map, r_occluder_arrays, r_node_renames);
  480. if (!r) {
  481. i--; // Was erased.
  482. }
  483. }
  484. String name = p_node->get_name();
  485. NodePath original_path = p_root->get_path_to(p_node); // Used to detect renames due to import hints.
  486. bool isroot = p_node == p_root;
  487. if (!isroot && _teststr(name, "noimp")) {
  488. p_node->set_owner(nullptr);
  489. memdelete(p_node);
  490. return nullptr;
  491. }
  492. if (Object::cast_to<ImporterMeshInstance3D>(p_node)) {
  493. ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node);
  494. Ref<ImporterMesh> m = mi->get_mesh();
  495. if (m.is_valid()) {
  496. for (int i = 0; i < m->get_surface_count(); i++) {
  497. Ref<BaseMaterial3D> mat = m->get_surface_material(i);
  498. if (!mat.is_valid()) {
  499. continue;
  500. }
  501. if (_teststr(mat->get_name(), "alpha")) {
  502. mat->set_transparency(BaseMaterial3D::TRANSPARENCY_ALPHA);
  503. mat->set_name(_fixstr(mat->get_name(), "alpha"));
  504. }
  505. if (_teststr(mat->get_name(), "vcol")) {
  506. mat->set_flag(BaseMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  507. mat->set_flag(BaseMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  508. mat->set_name(_fixstr(mat->get_name(), "vcol"));
  509. }
  510. }
  511. }
  512. }
  513. if (Object::cast_to<AnimationPlayer>(p_node)) {
  514. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
  515. // Node paths in animation tracks are relative to the following path (this is used to fix node paths below).
  516. Node *ap_root = ap->get_node(ap->get_root());
  517. NodePath path_prefix = p_root->get_path_to(ap_root);
  518. bool nodes_were_renamed = r_node_renames.size() != 0;
  519. List<StringName> anims;
  520. ap->get_animation_list(&anims);
  521. for (const StringName &E : anims) {
  522. Ref<Animation> anim = ap->get_animation(E);
  523. ERR_CONTINUE(anim.is_null());
  524. // Remove animation tracks referencing non-importable nodes.
  525. for (int i = 0; i < anim->get_track_count(); i++) {
  526. NodePath path = anim->track_get_path(i);
  527. for (int j = 0; j < path.get_name_count(); j++) {
  528. String node = path.get_name(j);
  529. if (_teststr(node, "noimp")) {
  530. anim->remove_track(i);
  531. i--;
  532. break;
  533. }
  534. }
  535. }
  536. // Fix node paths in animations, in case nodes were renamed earlier due to import hints.
  537. if (nodes_were_renamed) {
  538. for (int i = 0; i < anim->get_track_count(); i++) {
  539. NodePath path = anim->track_get_path(i);
  540. // Convert track path to absolute node path without subnames (some manual work because we are not in the scene tree).
  541. Vector<StringName> absolute_path_names = path_prefix.get_names();
  542. absolute_path_names.append_array(path.get_names());
  543. NodePath absolute_path(absolute_path_names, false);
  544. absolute_path.simplify();
  545. // Fix paths to renamed nodes.
  546. for (const Pair<NodePath, Node *> &F : r_node_renames) {
  547. if (F.first == absolute_path) {
  548. NodePath new_path(ap_root->get_path_to(F.second).get_names(), path.get_subnames(), false);
  549. print_verbose(vformat("Fix: Correcting node path in animation track: %s should be %s", path, new_path));
  550. anim->track_set_path(i, new_path);
  551. break; // Only one match is possible.
  552. }
  553. }
  554. }
  555. }
  556. String animname = E;
  557. const int loop_string_count = 3;
  558. static const char *loop_strings[loop_string_count] = { "loop_mode", "loop", "cycle" };
  559. for (int i = 0; i < loop_string_count; i++) {
  560. if (_teststr(animname, loop_strings[i])) {
  561. anim->set_loop_mode(Animation::LOOP_LINEAR);
  562. animname = _fixstr(animname, loop_strings[i]);
  563. Ref<AnimationLibrary> library = ap->get_animation_library(ap->find_animation_library(anim));
  564. library->rename_animation(E, animname);
  565. }
  566. }
  567. }
  568. }
  569. if (_teststr(name, "colonly") || _teststr(name, "convcolonly")) {
  570. if (isroot) {
  571. return p_node;
  572. }
  573. String fixed_name;
  574. if (_teststr(name, "colonly")) {
  575. fixed_name = _fixstr(name, "colonly");
  576. } else if (_teststr(name, "convcolonly")) {
  577. fixed_name = _fixstr(name, "convcolonly");
  578. }
  579. ERR_FAIL_COND_V(fixed_name.is_empty(), nullptr);
  580. ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node);
  581. if (mi) {
  582. Ref<ImporterMesh> mesh = mi->get_mesh();
  583. if (mesh.is_valid()) {
  584. Vector<Ref<Shape3D>> shapes;
  585. if (r_collision_map.has(mesh)) {
  586. shapes = r_collision_map[mesh];
  587. } else if (_teststr(name, "colonly")) {
  588. _pre_gen_shape_list(mesh, shapes, false);
  589. r_collision_map[mesh] = shapes;
  590. } else if (_teststr(name, "convcolonly")) {
  591. _pre_gen_shape_list(mesh, shapes, true);
  592. r_collision_map[mesh] = shapes;
  593. }
  594. if (shapes.size()) {
  595. StaticBody3D *col = memnew(StaticBody3D);
  596. col->set_transform(mi->get_transform());
  597. col->set_name(fixed_name);
  598. p_node->replace_by(col);
  599. p_node->set_owner(nullptr);
  600. memdelete(p_node);
  601. p_node = col;
  602. _add_shapes(col, shapes);
  603. }
  604. }
  605. } else if (p_node->has_meta("empty_draw_type")) {
  606. String empty_draw_type = String(p_node->get_meta("empty_draw_type"));
  607. StaticBody3D *sb = memnew(StaticBody3D);
  608. sb->set_name(fixed_name);
  609. Object::cast_to<Node3D>(sb)->set_transform(Object::cast_to<Node3D>(p_node)->get_transform());
  610. p_node->replace_by(sb);
  611. p_node->set_owner(nullptr);
  612. memdelete(p_node);
  613. p_node = sb;
  614. CollisionShape3D *colshape = memnew(CollisionShape3D);
  615. if (empty_draw_type == "CUBE") {
  616. BoxShape3D *boxShape = memnew(BoxShape3D);
  617. boxShape->set_size(Vector3(2, 2, 2));
  618. colshape->set_shape(boxShape);
  619. } else if (empty_draw_type == "SINGLE_ARROW") {
  620. SeparationRayShape3D *rayShape = memnew(SeparationRayShape3D);
  621. rayShape->set_length(1);
  622. colshape->set_shape(rayShape);
  623. Object::cast_to<Node3D>(sb)->rotate_x(Math_PI / 2);
  624. } else if (empty_draw_type == "IMAGE") {
  625. WorldBoundaryShape3D *world_boundary_shape = memnew(WorldBoundaryShape3D);
  626. colshape->set_shape(world_boundary_shape);
  627. } else {
  628. SphereShape3D *sphereShape = memnew(SphereShape3D);
  629. sphereShape->set_radius(1);
  630. colshape->set_shape(sphereShape);
  631. }
  632. sb->add_child(colshape, true);
  633. colshape->set_owner(sb->get_owner());
  634. }
  635. } else if (_teststr(name, "rigid") && Object::cast_to<ImporterMeshInstance3D>(p_node)) {
  636. if (isroot) {
  637. return p_node;
  638. }
  639. ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node);
  640. Ref<ImporterMesh> mesh = mi->get_mesh();
  641. if (mesh.is_valid()) {
  642. Vector<Ref<Shape3D>> shapes;
  643. if (r_collision_map.has(mesh)) {
  644. shapes = r_collision_map[mesh];
  645. } else {
  646. _pre_gen_shape_list(mesh, shapes, true);
  647. }
  648. RigidBody3D *rigid_body = memnew(RigidBody3D);
  649. rigid_body->set_name(_fixstr(name, "rigid_body"));
  650. p_node->replace_by(rigid_body);
  651. rigid_body->set_transform(mi->get_transform());
  652. p_node = rigid_body;
  653. mi->set_transform(Transform3D());
  654. rigid_body->add_child(mi, true);
  655. mi->set_owner(rigid_body->get_owner());
  656. _add_shapes(rigid_body, shapes);
  657. }
  658. } else if ((_teststr(name, "col") || (_teststr(name, "convcol"))) && Object::cast_to<ImporterMeshInstance3D>(p_node)) {
  659. ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node);
  660. Ref<ImporterMesh> mesh = mi->get_mesh();
  661. if (mesh.is_valid()) {
  662. Vector<Ref<Shape3D>> shapes;
  663. String fixed_name;
  664. if (r_collision_map.has(mesh)) {
  665. shapes = r_collision_map[mesh];
  666. } else if (_teststr(name, "col")) {
  667. _pre_gen_shape_list(mesh, shapes, false);
  668. r_collision_map[mesh] = shapes;
  669. } else if (_teststr(name, "convcol")) {
  670. _pre_gen_shape_list(mesh, shapes, true);
  671. r_collision_map[mesh] = shapes;
  672. }
  673. if (_teststr(name, "col")) {
  674. fixed_name = _fixstr(name, "col");
  675. } else if (_teststr(name, "convcol")) {
  676. fixed_name = _fixstr(name, "convcol");
  677. }
  678. if (!fixed_name.is_empty()) {
  679. if (mi->get_parent() && !mi->get_parent()->has_node(fixed_name)) {
  680. mi->set_name(fixed_name);
  681. }
  682. }
  683. if (shapes.size()) {
  684. StaticBody3D *col = memnew(StaticBody3D);
  685. mi->add_child(col, true);
  686. col->set_owner(mi->get_owner());
  687. _add_shapes(col, shapes);
  688. }
  689. }
  690. } else if (_teststr(name, "navmesh") && Object::cast_to<ImporterMeshInstance3D>(p_node)) {
  691. if (isroot) {
  692. return p_node;
  693. }
  694. ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node);
  695. Ref<ImporterMesh> mesh = mi->get_mesh();
  696. ERR_FAIL_COND_V(mesh.is_null(), nullptr);
  697. NavigationRegion3D *nmi = memnew(NavigationRegion3D);
  698. nmi->set_name(_fixstr(name, "navmesh"));
  699. Ref<NavigationMesh> nmesh = mesh->create_navigation_mesh();
  700. nmi->set_navigation_mesh(nmesh);
  701. Object::cast_to<Node3D>(nmi)->set_transform(mi->get_transform());
  702. p_node->replace_by(nmi);
  703. p_node->set_owner(nullptr);
  704. memdelete(p_node);
  705. p_node = nmi;
  706. } else if (_teststr(name, "occ") || _teststr(name, "occonly")) {
  707. if (isroot) {
  708. return p_node;
  709. }
  710. ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node);
  711. if (mi) {
  712. Ref<ImporterMesh> mesh = mi->get_mesh();
  713. if (mesh.is_valid()) {
  714. if (r_occluder_arrays) {
  715. OccluderInstance3D::bake_single_node(mi, 0.0f, r_occluder_arrays->first, r_occluder_arrays->second);
  716. }
  717. if (_teststr(name, "occ")) {
  718. String fixed_name = _fixstr(name, "occ");
  719. if (!fixed_name.is_empty()) {
  720. if (mi->get_parent() && !mi->get_parent()->has_node(fixed_name)) {
  721. mi->set_name(fixed_name);
  722. }
  723. }
  724. } else {
  725. p_node->set_owner(nullptr);
  726. memdelete(p_node);
  727. p_node = nullptr;
  728. }
  729. }
  730. }
  731. } else if (_teststr(name, "vehicle")) {
  732. if (isroot) {
  733. return p_node;
  734. }
  735. Node *owner = p_node->get_owner();
  736. Node3D *s = Object::cast_to<Node3D>(p_node);
  737. VehicleBody3D *bv = memnew(VehicleBody3D);
  738. String n = _fixstr(p_node->get_name(), "vehicle");
  739. bv->set_name(n);
  740. p_node->replace_by(bv);
  741. p_node->set_name(n);
  742. bv->add_child(p_node);
  743. bv->set_owner(owner);
  744. p_node->set_owner(owner);
  745. bv->set_transform(s->get_transform());
  746. s->set_transform(Transform3D());
  747. p_node = bv;
  748. } else if (_teststr(name, "wheel")) {
  749. if (isroot) {
  750. return p_node;
  751. }
  752. Node *owner = p_node->get_owner();
  753. Node3D *s = Object::cast_to<Node3D>(p_node);
  754. VehicleWheel3D *bv = memnew(VehicleWheel3D);
  755. String n = _fixstr(p_node->get_name(), "wheel");
  756. bv->set_name(n);
  757. p_node->replace_by(bv);
  758. p_node->set_name(n);
  759. bv->add_child(p_node);
  760. bv->set_owner(owner);
  761. p_node->set_owner(owner);
  762. bv->set_transform(s->get_transform());
  763. s->set_transform(Transform3D());
  764. p_node = bv;
  765. } else if (Object::cast_to<ImporterMeshInstance3D>(p_node)) {
  766. //last attempt, maybe collision inside the mesh data
  767. ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node);
  768. Ref<ImporterMesh> mesh = mi->get_mesh();
  769. if (!mesh.is_null()) {
  770. Vector<Ref<Shape3D>> shapes;
  771. if (r_collision_map.has(mesh)) {
  772. shapes = r_collision_map[mesh];
  773. } else if (_teststr(mesh->get_name(), "col")) {
  774. _pre_gen_shape_list(mesh, shapes, false);
  775. r_collision_map[mesh] = shapes;
  776. mesh->set_name(_fixstr(mesh->get_name(), "col"));
  777. } else if (_teststr(mesh->get_name(), "convcol")) {
  778. _pre_gen_shape_list(mesh, shapes, true);
  779. r_collision_map[mesh] = shapes;
  780. mesh->set_name(_fixstr(mesh->get_name(), "convcol"));
  781. } else if (_teststr(mesh->get_name(), "occ")) {
  782. if (r_occluder_arrays) {
  783. OccluderInstance3D::bake_single_node(mi, 0.0f, r_occluder_arrays->first, r_occluder_arrays->second);
  784. }
  785. mesh->set_name(_fixstr(mesh->get_name(), "occ"));
  786. }
  787. if (shapes.size()) {
  788. StaticBody3D *col = memnew(StaticBody3D);
  789. p_node->add_child(col, true);
  790. col->set_owner(p_node->get_owner());
  791. _add_shapes(col, shapes);
  792. }
  793. }
  794. }
  795. if (p_node) {
  796. NodePath new_path = p_root->get_path_to(p_node);
  797. if (new_path != original_path) {
  798. print_verbose(vformat("Fix: Renamed %s to %s", original_path, new_path));
  799. r_node_renames.push_back({ original_path, p_node });
  800. }
  801. }
  802. return p_node;
  803. }
  804. Node *ResourceImporterScene::_pre_fix_animations(Node *p_node, Node *p_root, const Dictionary &p_node_data, const Dictionary &p_animation_data, float p_animation_fps) {
  805. // children first
  806. for (int i = 0; i < p_node->get_child_count(); i++) {
  807. Node *r = _pre_fix_animations(p_node->get_child(i), p_root, p_node_data, p_animation_data, p_animation_fps);
  808. if (!r) {
  809. i--; //was erased
  810. }
  811. }
  812. String import_id = p_node->get_meta("import_id", "PATH:" + p_root->get_path_to(p_node));
  813. Dictionary node_settings;
  814. if (p_node_data.has(import_id)) {
  815. node_settings = p_node_data[import_id];
  816. }
  817. {
  818. //make sure this is unique
  819. node_settings = node_settings.duplicate(true);
  820. //fill node settings for this node with default values
  821. List<ImportOption> iopts;
  822. get_internal_import_options(INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE, &iopts);
  823. for (const ImportOption &E : iopts) {
  824. if (!node_settings.has(E.option.name)) {
  825. node_settings[E.option.name] = E.default_value;
  826. }
  827. }
  828. }
  829. if (Object::cast_to<AnimationPlayer>(p_node)) {
  830. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
  831. List<StringName> anims;
  832. ap->get_animation_list(&anims);
  833. for (const StringName &name : anims) {
  834. Ref<Animation> anim = ap->get_animation(name);
  835. Array animation_slices;
  836. if (p_animation_data.has(name)) {
  837. Dictionary anim_settings = p_animation_data[name];
  838. int slices_count = anim_settings["slices/amount"];
  839. for (int i = 0; i < slices_count; i++) {
  840. String slice_name = anim_settings["slice_" + itos(i + 1) + "/name"];
  841. int from_frame = anim_settings["slice_" + itos(i + 1) + "/start_frame"];
  842. int end_frame = anim_settings["slice_" + itos(i + 1) + "/end_frame"];
  843. Animation::LoopMode loop_mode = static_cast<Animation::LoopMode>((int)anim_settings["slice_" + itos(i + 1) + "/loop_mode"]);
  844. bool save_to_file = anim_settings["slice_" + itos(i + 1) + "/save_to_file/enabled"];
  845. bool save_to_path = anim_settings["slice_" + itos(i + 1) + "/save_to_file/path"];
  846. bool save_to_file_keep_custom = anim_settings["slice_" + itos(i + 1) + "/save_to_file/keep_custom_tracks"];
  847. animation_slices.push_back(slice_name);
  848. animation_slices.push_back(from_frame / p_animation_fps);
  849. animation_slices.push_back(end_frame / p_animation_fps);
  850. animation_slices.push_back(loop_mode);
  851. animation_slices.push_back(save_to_file);
  852. animation_slices.push_back(save_to_path);
  853. animation_slices.push_back(save_to_file_keep_custom);
  854. }
  855. }
  856. if (animation_slices.size() > 0) {
  857. _create_slices(ap, anim, animation_slices, true);
  858. }
  859. }
  860. AnimationImportTracks import_tracks_mode[TRACK_CHANNEL_MAX] = {
  861. AnimationImportTracks(int(node_settings["import_tracks/position"])),
  862. AnimationImportTracks(int(node_settings["import_tracks/rotation"])),
  863. AnimationImportTracks(int(node_settings["import_tracks/scale"]))
  864. };
  865. if (!anims.is_empty() && (import_tracks_mode[0] != ANIMATION_IMPORT_TRACKS_IF_PRESENT || import_tracks_mode[1] != ANIMATION_IMPORT_TRACKS_IF_PRESENT || import_tracks_mode[2] != ANIMATION_IMPORT_TRACKS_IF_PRESENT)) {
  866. _optimize_track_usage(ap, import_tracks_mode);
  867. }
  868. }
  869. return p_node;
  870. }
  871. Node *ResourceImporterScene::_post_fix_animations(Node *p_node, Node *p_root, const Dictionary &p_node_data, const Dictionary &p_animation_data, float p_animation_fps) {
  872. // children first
  873. for (int i = 0; i < p_node->get_child_count(); i++) {
  874. Node *r = _post_fix_animations(p_node->get_child(i), p_root, p_node_data, p_animation_data, p_animation_fps);
  875. if (!r) {
  876. i--; //was erased
  877. }
  878. }
  879. String import_id = p_node->get_meta("import_id", "PATH:" + p_root->get_path_to(p_node));
  880. Dictionary node_settings;
  881. if (p_node_data.has(import_id)) {
  882. node_settings = p_node_data[import_id];
  883. }
  884. {
  885. //make sure this is unique
  886. node_settings = node_settings.duplicate(true);
  887. //fill node settings for this node with default values
  888. List<ImportOption> iopts;
  889. get_internal_import_options(INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE, &iopts);
  890. for (const ImportOption &E : iopts) {
  891. if (!node_settings.has(E.option.name)) {
  892. node_settings[E.option.name] = E.default_value;
  893. }
  894. }
  895. }
  896. if (Object::cast_to<AnimationPlayer>(p_node)) {
  897. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
  898. bool use_optimizer = node_settings["optimizer/enabled"];
  899. float anim_optimizer_linerr = node_settings["optimizer/max_velocity_error"];
  900. float anim_optimizer_angerr = node_settings["optimizer/max_angular_error"];
  901. int anim_optimizer_preerr = node_settings["optimizer/max_precision_error"];
  902. if (use_optimizer) {
  903. _optimize_animations(ap, anim_optimizer_linerr, anim_optimizer_angerr, anim_optimizer_preerr);
  904. }
  905. bool use_compression = node_settings["compression/enabled"];
  906. int anim_compression_page_size = node_settings["compression/page_size"];
  907. if (use_compression) {
  908. _compress_animations(ap, anim_compression_page_size);
  909. }
  910. List<StringName> anims;
  911. ap->get_animation_list(&anims);
  912. for (const StringName &name : anims) {
  913. Ref<Animation> anim = ap->get_animation(name);
  914. if (p_animation_data.has(name)) {
  915. Dictionary anim_settings = p_animation_data[name];
  916. {
  917. //fill with default values
  918. List<ImportOption> iopts;
  919. get_internal_import_options(INTERNAL_IMPORT_CATEGORY_ANIMATION, &iopts);
  920. for (const ImportOption &F : iopts) {
  921. if (!anim_settings.has(F.option.name)) {
  922. anim_settings[F.option.name] = F.default_value;
  923. }
  924. }
  925. }
  926. anim->set_loop_mode(static_cast<Animation::LoopMode>((int)anim_settings["settings/loop_mode"]));
  927. bool save = anim_settings["save_to_file/enabled"];
  928. String path = anim_settings["save_to_file/path"];
  929. bool keep_custom = anim_settings["save_to_file/keep_custom_tracks"];
  930. Ref<Animation> saved_anim = _save_animation_to_file(anim, save, path, keep_custom);
  931. if (saved_anim != anim) {
  932. Ref<AnimationLibrary> al = ap->get_animation_library(ap->find_animation_library(anim));
  933. al->add_animation(name, saved_anim); //replace
  934. }
  935. }
  936. }
  937. }
  938. return p_node;
  939. }
  940. Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> &collision_map, Pair<PackedVector3Array, PackedInt32Array> &r_occluder_arrays, HashSet<Ref<ImporterMesh>> &r_scanned_meshes, const Dictionary &p_node_data, const Dictionary &p_material_data, const Dictionary &p_animation_data, float p_animation_fps, float p_applied_root_scale) {
  941. // children first
  942. for (int i = 0; i < p_node->get_child_count(); i++) {
  943. Node *r = _post_fix_node(p_node->get_child(i), p_root, collision_map, r_occluder_arrays, r_scanned_meshes, p_node_data, p_material_data, p_animation_data, p_animation_fps, p_applied_root_scale);
  944. if (!r) {
  945. i--; //was erased
  946. }
  947. }
  948. bool isroot = p_node == p_root;
  949. String import_id = p_node->get_meta("import_id", "PATH:" + p_root->get_path_to(p_node));
  950. Dictionary node_settings;
  951. if (p_node_data.has(import_id)) {
  952. node_settings = p_node_data[import_id];
  953. }
  954. if (!isroot && (node_settings.has("import/skip_import") && bool(node_settings["import/skip_import"]))) {
  955. p_node->set_owner(nullptr);
  956. memdelete(p_node);
  957. return nullptr;
  958. }
  959. {
  960. //make sure this is unique
  961. node_settings = node_settings.duplicate(true);
  962. //fill node settings for this node with default values
  963. List<ImportOption> iopts;
  964. if (Object::cast_to<ImporterMeshInstance3D>(p_node)) {
  965. get_internal_import_options(INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE, &iopts);
  966. } else if (Object::cast_to<AnimationPlayer>(p_node)) {
  967. get_internal_import_options(INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE, &iopts);
  968. } else if (Object::cast_to<Skeleton3D>(p_node)) {
  969. get_internal_import_options(INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE, &iopts);
  970. } else {
  971. get_internal_import_options(INTERNAL_IMPORT_CATEGORY_NODE, &iopts);
  972. }
  973. for (const ImportOption &E : iopts) {
  974. if (!node_settings.has(E.option.name)) {
  975. node_settings[E.option.name] = E.default_value;
  976. }
  977. }
  978. }
  979. {
  980. ObjectID node_id = p_node->get_instance_id();
  981. for (int i = 0; i < post_importer_plugins.size(); i++) {
  982. post_importer_plugins.write[i]->internal_process(EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_NODE, p_root, p_node, Ref<Resource>(), node_settings);
  983. if (ObjectDB::get_instance(node_id) == nullptr) { //may have been erased, so do not continue
  984. break;
  985. }
  986. }
  987. }
  988. if (Object::cast_to<ImporterMeshInstance3D>(p_node)) {
  989. ObjectID node_id = p_node->get_instance_id();
  990. for (int i = 0; i < post_importer_plugins.size(); i++) {
  991. post_importer_plugins.write[i]->internal_process(EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE, p_root, p_node, Ref<Resource>(), node_settings);
  992. if (ObjectDB::get_instance(node_id) == nullptr) { //may have been erased, so do not continue
  993. break;
  994. }
  995. }
  996. }
  997. if (Object::cast_to<Skeleton3D>(p_node)) {
  998. ObjectID node_id = p_node->get_instance_id();
  999. for (int i = 0; i < post_importer_plugins.size(); i++) {
  1000. post_importer_plugins.write[i]->internal_process(EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE, p_root, p_node, Ref<Resource>(), node_settings);
  1001. if (ObjectDB::get_instance(node_id) == nullptr) { //may have been erased, so do not continue
  1002. break;
  1003. }
  1004. }
  1005. }
  1006. if (Object::cast_to<ImporterMeshInstance3D>(p_node)) {
  1007. ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node);
  1008. Ref<ImporterMesh> m = mi->get_mesh();
  1009. if (m.is_valid()) {
  1010. if (!r_scanned_meshes.has(m)) {
  1011. for (int i = 0; i < m->get_surface_count(); i++) {
  1012. Ref<Material> mat = m->get_surface_material(i);
  1013. if (mat.is_valid()) {
  1014. String mat_id = mat->get_meta("import_id", mat->get_name());
  1015. if (!mat_id.is_empty() && p_material_data.has(mat_id)) {
  1016. Dictionary matdata = p_material_data[mat_id];
  1017. {
  1018. //fill node settings for this node with default values
  1019. List<ImportOption> iopts;
  1020. get_internal_import_options(INTERNAL_IMPORT_CATEGORY_MATERIAL, &iopts);
  1021. for (const ImportOption &E : iopts) {
  1022. if (!matdata.has(E.option.name)) {
  1023. matdata[E.option.name] = E.default_value;
  1024. }
  1025. }
  1026. }
  1027. for (int j = 0; j < post_importer_plugins.size(); j++) {
  1028. post_importer_plugins.write[j]->internal_process(EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_MATERIAL, p_root, p_node, mat, matdata);
  1029. }
  1030. if (matdata.has("use_external/enabled") && bool(matdata["use_external/enabled"]) && matdata.has("use_external/path")) {
  1031. String path = matdata["use_external/path"];
  1032. Ref<Material> external_mat = ResourceLoader::load(path);
  1033. if (external_mat.is_valid()) {
  1034. m->set_surface_material(i, external_mat);
  1035. }
  1036. }
  1037. }
  1038. }
  1039. }
  1040. r_scanned_meshes.insert(m);
  1041. }
  1042. if (node_settings.has("generate/physics")) {
  1043. int mesh_physics_mode = MeshPhysicsMode::MESH_PHYSICS_DISABLED;
  1044. const bool generate_collider = node_settings["generate/physics"];
  1045. if (generate_collider) {
  1046. mesh_physics_mode = MeshPhysicsMode::MESH_PHYSICS_MESH_AND_STATIC_COLLIDER;
  1047. if (node_settings.has("physics/body_type")) {
  1048. const BodyType body_type = (BodyType)node_settings["physics/body_type"].operator int();
  1049. switch (body_type) {
  1050. case BODY_TYPE_STATIC:
  1051. mesh_physics_mode = MeshPhysicsMode::MESH_PHYSICS_MESH_AND_STATIC_COLLIDER;
  1052. break;
  1053. case BODY_TYPE_DYNAMIC:
  1054. mesh_physics_mode = MeshPhysicsMode::MESH_PHYSICS_RIGID_BODY_AND_MESH;
  1055. break;
  1056. case BODY_TYPE_AREA:
  1057. mesh_physics_mode = MeshPhysicsMode::MESH_PHYSICS_AREA_ONLY;
  1058. break;
  1059. }
  1060. }
  1061. }
  1062. if (mesh_physics_mode != MeshPhysicsMode::MESH_PHYSICS_DISABLED) {
  1063. Vector<Ref<Shape3D>> shapes;
  1064. if (collision_map.has(m)) {
  1065. shapes = collision_map[m];
  1066. } else {
  1067. shapes = get_collision_shapes(
  1068. m,
  1069. node_settings,
  1070. p_applied_root_scale);
  1071. }
  1072. if (shapes.size()) {
  1073. CollisionObject3D *base = nullptr;
  1074. switch (mesh_physics_mode) {
  1075. case MESH_PHYSICS_MESH_AND_STATIC_COLLIDER: {
  1076. StaticBody3D *col = memnew(StaticBody3D);
  1077. p_node->add_child(col, true);
  1078. col->set_owner(p_node->get_owner());
  1079. col->set_transform(get_collision_shapes_transform(node_settings));
  1080. col->set_position(p_applied_root_scale * col->get_position());
  1081. base = col;
  1082. } break;
  1083. case MESH_PHYSICS_RIGID_BODY_AND_MESH: {
  1084. RigidBody3D *rigid_body = memnew(RigidBody3D);
  1085. rigid_body->set_name(p_node->get_name());
  1086. p_node->replace_by(rigid_body);
  1087. rigid_body->set_transform(mi->get_transform() * get_collision_shapes_transform(node_settings));
  1088. rigid_body->set_position(p_applied_root_scale * rigid_body->get_position());
  1089. p_node = rigid_body;
  1090. mi->set_transform(Transform3D());
  1091. rigid_body->add_child(mi, true);
  1092. mi->set_owner(rigid_body->get_owner());
  1093. base = rigid_body;
  1094. } break;
  1095. case MESH_PHYSICS_STATIC_COLLIDER_ONLY: {
  1096. StaticBody3D *col = memnew(StaticBody3D);
  1097. col->set_transform(mi->get_transform() * get_collision_shapes_transform(node_settings));
  1098. col->set_position(p_applied_root_scale * col->get_position());
  1099. col->set_name(p_node->get_name());
  1100. p_node->replace_by(col);
  1101. p_node->set_owner(nullptr);
  1102. memdelete(p_node);
  1103. p_node = col;
  1104. base = col;
  1105. } break;
  1106. case MESH_PHYSICS_AREA_ONLY: {
  1107. Area3D *area = memnew(Area3D);
  1108. area->set_transform(mi->get_transform() * get_collision_shapes_transform(node_settings));
  1109. area->set_position(p_applied_root_scale * area->get_position());
  1110. area->set_name(p_node->get_name());
  1111. p_node->replace_by(area);
  1112. p_node->set_owner(nullptr);
  1113. memdelete(p_node);
  1114. p_node = area;
  1115. base = area;
  1116. } break;
  1117. }
  1118. for (const Ref<Shape3D> &E : shapes) {
  1119. CollisionShape3D *cshape = memnew(CollisionShape3D);
  1120. cshape->set_shape(E);
  1121. base->add_child(cshape, true);
  1122. cshape->set_owner(base->get_owner());
  1123. }
  1124. }
  1125. }
  1126. }
  1127. }
  1128. }
  1129. //navmesh (node may have changed type above)
  1130. if (Object::cast_to<ImporterMeshInstance3D>(p_node)) {
  1131. ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node);
  1132. Ref<ImporterMesh> m = mi->get_mesh();
  1133. if (m.is_valid()) {
  1134. if (node_settings.has("generate/navmesh")) {
  1135. int navmesh_mode = node_settings["generate/navmesh"];
  1136. if (navmesh_mode != NAVMESH_DISABLED) {
  1137. NavigationRegion3D *nmi = memnew(NavigationRegion3D);
  1138. Ref<NavigationMesh> nmesh = m->create_navigation_mesh();
  1139. nmi->set_navigation_mesh(nmesh);
  1140. if (navmesh_mode == NAVMESH_NAVMESH_ONLY) {
  1141. nmi->set_transform(mi->get_transform());
  1142. p_node->replace_by(nmi);
  1143. p_node->set_owner(nullptr);
  1144. memdelete(p_node);
  1145. p_node = nmi;
  1146. } else {
  1147. mi->add_child(nmi, true);
  1148. nmi->set_owner(mi->get_owner());
  1149. }
  1150. }
  1151. }
  1152. }
  1153. }
  1154. if (Object::cast_to<ImporterMeshInstance3D>(p_node)) {
  1155. ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(p_node);
  1156. Ref<ImporterMesh> m = mi->get_mesh();
  1157. if (m.is_valid()) {
  1158. if (node_settings.has("generate/occluder")) {
  1159. int occluder_mode = node_settings["generate/occluder"];
  1160. if (occluder_mode != OCCLUDER_DISABLED) {
  1161. float simplification_dist = 0.0f;
  1162. if (node_settings.has("occluder/simplification_distance")) {
  1163. simplification_dist = node_settings["occluder/simplification_distance"];
  1164. }
  1165. OccluderInstance3D::bake_single_node(mi, simplification_dist, r_occluder_arrays.first, r_occluder_arrays.second);
  1166. if (occluder_mode == OCCLUDER_OCCLUDER_ONLY) {
  1167. p_node->set_owner(nullptr);
  1168. memdelete(p_node);
  1169. p_node = nullptr;
  1170. }
  1171. }
  1172. }
  1173. }
  1174. }
  1175. if (Object::cast_to<AnimationPlayer>(p_node)) {
  1176. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node);
  1177. for (int i = 0; i < post_importer_plugins.size(); i++) {
  1178. post_importer_plugins.write[i]->internal_process(EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE, p_root, p_node, Ref<Resource>(), node_settings);
  1179. }
  1180. if (post_importer_plugins.size()) {
  1181. List<StringName> anims;
  1182. ap->get_animation_list(&anims);
  1183. for (const StringName &name : anims) {
  1184. if (p_animation_data.has(name)) {
  1185. Ref<Animation> anim = ap->get_animation(name);
  1186. Dictionary anim_settings = p_animation_data[name];
  1187. {
  1188. //fill with default values
  1189. List<ImportOption> iopts;
  1190. get_internal_import_options(INTERNAL_IMPORT_CATEGORY_ANIMATION, &iopts);
  1191. for (const ImportOption &F : iopts) {
  1192. if (!anim_settings.has(F.option.name)) {
  1193. anim_settings[F.option.name] = F.default_value;
  1194. }
  1195. }
  1196. }
  1197. for (int i = 0; i < post_importer_plugins.size(); i++) {
  1198. post_importer_plugins.write[i]->internal_process(EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_ANIMATION, p_root, p_node, anim, anim_settings);
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. return p_node;
  1205. }
  1206. Ref<Animation> ResourceImporterScene::_save_animation_to_file(Ref<Animation> anim, bool p_save_to_file, String p_save_to_path, bool p_keep_custom_tracks) {
  1207. if (!p_save_to_file || !p_save_to_path.is_resource_file()) {
  1208. return anim;
  1209. }
  1210. if (FileAccess::exists(p_save_to_path) && p_keep_custom_tracks) {
  1211. // Copy custom animation tracks from previously imported files.
  1212. Ref<Animation> old_anim = ResourceLoader::load(p_save_to_path, "Animation", ResourceFormatLoader::CACHE_MODE_IGNORE);
  1213. if (old_anim.is_valid()) {
  1214. for (int i = 0; i < old_anim->get_track_count(); i++) {
  1215. if (!old_anim->track_is_imported(i)) {
  1216. old_anim->copy_track(i, anim);
  1217. }
  1218. }
  1219. anim->set_loop_mode(old_anim->get_loop_mode());
  1220. }
  1221. }
  1222. if (ResourceCache::has(p_save_to_path)) {
  1223. Ref<Animation> old_anim = ResourceCache::get_ref(p_save_to_path);
  1224. if (old_anim.is_valid()) {
  1225. old_anim->copy_from(anim);
  1226. anim = old_anim;
  1227. }
  1228. }
  1229. anim->set_path(p_save_to_path, true); // Set path to save externally.
  1230. Error err = ResourceSaver::save(anim, p_save_to_path, ResourceSaver::FLAG_CHANGE_PATH);
  1231. ERR_FAIL_COND_V_MSG(err != OK, anim, "Saving of animation failed: " + p_save_to_path);
  1232. return anim;
  1233. }
  1234. void ResourceImporterScene::_create_slices(AnimationPlayer *ap, Ref<Animation> anim, const Array &p_slices, bool p_bake_all) {
  1235. Ref<AnimationLibrary> al = ap->get_animation_library(ap->find_animation_library(anim));
  1236. for (int i = 0; i < p_slices.size(); i += 7) {
  1237. String name = p_slices[i];
  1238. float from = p_slices[i + 1];
  1239. float to = p_slices[i + 2];
  1240. Animation::LoopMode loop_mode = static_cast<Animation::LoopMode>((int)p_slices[i + 3]);
  1241. bool save_to_file = p_slices[i + 4];
  1242. String save_to_path = p_slices[i + 5];
  1243. bool keep_current = p_slices[i + 6];
  1244. if (from >= to) {
  1245. continue;
  1246. }
  1247. Ref<Animation> new_anim = memnew(Animation);
  1248. for (int j = 0; j < anim->get_track_count(); j++) {
  1249. List<float> keys;
  1250. int kc = anim->track_get_key_count(j);
  1251. int dtrack = -1;
  1252. for (int k = 0; k < kc; k++) {
  1253. float kt = anim->track_get_key_time(j, k);
  1254. if (kt >= from && kt < to) {
  1255. //found a key within range, so create track
  1256. if (dtrack == -1) {
  1257. new_anim->add_track(anim->track_get_type(j));
  1258. dtrack = new_anim->get_track_count() - 1;
  1259. new_anim->track_set_path(dtrack, anim->track_get_path(j));
  1260. if (kt > (from + 0.01) && k > 0) {
  1261. if (anim->track_get_type(j) == Animation::TYPE_POSITION_3D) {
  1262. Vector3 p;
  1263. anim->try_position_track_interpolate(j, from, &p);
  1264. new_anim->position_track_insert_key(dtrack, 0, p);
  1265. } else if (anim->track_get_type(j) == Animation::TYPE_ROTATION_3D) {
  1266. Quaternion r;
  1267. anim->try_rotation_track_interpolate(j, from, &r);
  1268. new_anim->rotation_track_insert_key(dtrack, 0, r);
  1269. } else if (anim->track_get_type(j) == Animation::TYPE_SCALE_3D) {
  1270. Vector3 s;
  1271. anim->try_scale_track_interpolate(j, from, &s);
  1272. new_anim->scale_track_insert_key(dtrack, 0, s);
  1273. } else if (anim->track_get_type(j) == Animation::TYPE_VALUE) {
  1274. Variant var = anim->value_track_interpolate(j, from);
  1275. new_anim->track_insert_key(dtrack, 0, var);
  1276. } else if (anim->track_get_type(j) == Animation::TYPE_BLEND_SHAPE) {
  1277. float interp;
  1278. anim->try_blend_shape_track_interpolate(j, from, &interp);
  1279. new_anim->blend_shape_track_insert_key(dtrack, 0, interp);
  1280. }
  1281. }
  1282. }
  1283. if (anim->track_get_type(j) == Animation::TYPE_POSITION_3D) {
  1284. Vector3 p;
  1285. anim->position_track_get_key(j, k, &p);
  1286. new_anim->position_track_insert_key(dtrack, kt - from, p);
  1287. } else if (anim->track_get_type(j) == Animation::TYPE_ROTATION_3D) {
  1288. Quaternion r;
  1289. anim->rotation_track_get_key(j, k, &r);
  1290. new_anim->rotation_track_insert_key(dtrack, kt - from, r);
  1291. } else if (anim->track_get_type(j) == Animation::TYPE_SCALE_3D) {
  1292. Vector3 s;
  1293. anim->scale_track_get_key(j, k, &s);
  1294. new_anim->scale_track_insert_key(dtrack, kt - from, s);
  1295. } else if (anim->track_get_type(j) == Animation::TYPE_VALUE) {
  1296. Variant var = anim->track_get_key_value(j, k);
  1297. new_anim->track_insert_key(dtrack, kt - from, var);
  1298. } else if (anim->track_get_type(j) == Animation::TYPE_BLEND_SHAPE) {
  1299. float interp;
  1300. anim->blend_shape_track_get_key(j, k, &interp);
  1301. new_anim->blend_shape_track_insert_key(dtrack, kt - from, interp);
  1302. }
  1303. }
  1304. if (dtrack != -1 && kt >= to) {
  1305. if (anim->track_get_type(j) == Animation::TYPE_POSITION_3D) {
  1306. Vector3 p;
  1307. anim->try_position_track_interpolate(j, to, &p);
  1308. new_anim->position_track_insert_key(dtrack, to - from, p);
  1309. } else if (anim->track_get_type(j) == Animation::TYPE_ROTATION_3D) {
  1310. Quaternion r;
  1311. anim->try_rotation_track_interpolate(j, to, &r);
  1312. new_anim->rotation_track_insert_key(dtrack, to - from, r);
  1313. } else if (anim->track_get_type(j) == Animation::TYPE_SCALE_3D) {
  1314. Vector3 s;
  1315. anim->try_scale_track_interpolate(j, to, &s);
  1316. new_anim->scale_track_insert_key(dtrack, to - from, s);
  1317. } else if (anim->track_get_type(j) == Animation::TYPE_VALUE) {
  1318. Variant var = anim->value_track_interpolate(j, to);
  1319. new_anim->track_insert_key(dtrack, to - from, var);
  1320. } else if (anim->track_get_type(j) == Animation::TYPE_BLEND_SHAPE) {
  1321. float interp;
  1322. anim->try_blend_shape_track_interpolate(j, to, &interp);
  1323. new_anim->blend_shape_track_insert_key(dtrack, to - from, interp);
  1324. }
  1325. }
  1326. }
  1327. if (dtrack == -1 && p_bake_all) {
  1328. new_anim->add_track(anim->track_get_type(j));
  1329. dtrack = new_anim->get_track_count() - 1;
  1330. new_anim->track_set_path(dtrack, anim->track_get_path(j));
  1331. if (anim->track_get_type(j) == Animation::TYPE_POSITION_3D) {
  1332. Vector3 p;
  1333. anim->try_position_track_interpolate(j, from, &p);
  1334. new_anim->position_track_insert_key(dtrack, 0, p);
  1335. anim->try_position_track_interpolate(j, to, &p);
  1336. new_anim->position_track_insert_key(dtrack, to - from, p);
  1337. } else if (anim->track_get_type(j) == Animation::TYPE_ROTATION_3D) {
  1338. Quaternion r;
  1339. anim->try_rotation_track_interpolate(j, from, &r);
  1340. new_anim->rotation_track_insert_key(dtrack, 0, r);
  1341. anim->try_rotation_track_interpolate(j, to, &r);
  1342. new_anim->rotation_track_insert_key(dtrack, to - from, r);
  1343. } else if (anim->track_get_type(j) == Animation::TYPE_SCALE_3D) {
  1344. Vector3 s;
  1345. anim->try_scale_track_interpolate(j, from, &s);
  1346. new_anim->scale_track_insert_key(dtrack, 0, s);
  1347. anim->try_scale_track_interpolate(j, to, &s);
  1348. new_anim->scale_track_insert_key(dtrack, to - from, s);
  1349. } else if (anim->track_get_type(j) == Animation::TYPE_VALUE) {
  1350. Variant var = anim->value_track_interpolate(j, from);
  1351. new_anim->track_insert_key(dtrack, 0, var);
  1352. Variant to_var = anim->value_track_interpolate(j, to);
  1353. new_anim->track_insert_key(dtrack, to - from, to_var);
  1354. } else if (anim->track_get_type(j) == Animation::TYPE_BLEND_SHAPE) {
  1355. float interp;
  1356. anim->try_blend_shape_track_interpolate(j, from, &interp);
  1357. new_anim->blend_shape_track_insert_key(dtrack, 0, interp);
  1358. anim->try_blend_shape_track_interpolate(j, to, &interp);
  1359. new_anim->blend_shape_track_insert_key(dtrack, to - from, interp);
  1360. }
  1361. }
  1362. }
  1363. new_anim->set_loop_mode(loop_mode);
  1364. new_anim->set_length(to - from);
  1365. al->add_animation(name, new_anim);
  1366. Ref<Animation> saved_anim = _save_animation_to_file(new_anim, save_to_file, save_to_path, keep_current);
  1367. if (saved_anim != new_anim) {
  1368. al->add_animation(name, saved_anim);
  1369. }
  1370. }
  1371. al->remove_animation(ap->find_animation(anim)); // Remove original animation (no longer needed).
  1372. }
  1373. void ResourceImporterScene::_optimize_animations(AnimationPlayer *anim, float p_max_vel_error, float p_max_ang_error, int p_prc_error) {
  1374. List<StringName> anim_names;
  1375. anim->get_animation_list(&anim_names);
  1376. for (const StringName &E : anim_names) {
  1377. Ref<Animation> a = anim->get_animation(E);
  1378. a->optimize(p_max_vel_error, p_max_ang_error, p_prc_error);
  1379. }
  1380. }
  1381. void ResourceImporterScene::_compress_animations(AnimationPlayer *anim, int p_page_size_kb) {
  1382. List<StringName> anim_names;
  1383. anim->get_animation_list(&anim_names);
  1384. for (const StringName &E : anim_names) {
  1385. Ref<Animation> a = anim->get_animation(E);
  1386. a->compress(p_page_size_kb * 1024);
  1387. }
  1388. }
  1389. void ResourceImporterScene::get_internal_import_options(InternalImportCategory p_category, List<ImportOption> *r_options) const {
  1390. switch (p_category) {
  1391. case INTERNAL_IMPORT_CATEGORY_NODE: {
  1392. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "import/skip_import", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
  1393. } break;
  1394. case INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE: {
  1395. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "import/skip_import", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
  1396. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "generate/physics", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
  1397. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/navmesh", PROPERTY_HINT_ENUM, "Disabled,Mesh + NavMesh,NavMesh Only"), 0));
  1398. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "physics/body_type", PROPERTY_HINT_ENUM, "Static,Dynamic,Area"), 0));
  1399. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "physics/shape_type", PROPERTY_HINT_ENUM, "Decompose Convex,Simple Convex,Trimesh,Box,Sphere,Cylinder,Capsule", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0));
  1400. // Decomposition
  1401. Ref<MeshConvexDecompositionSettings> decomposition_default = Ref<MeshConvexDecompositionSettings>();
  1402. decomposition_default.instantiate();
  1403. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "decomposition/advanced", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
  1404. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "decomposition/precision", PROPERTY_HINT_RANGE, "1,10,1"), 5));
  1405. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "decomposition/max_concavity", PROPERTY_HINT_RANGE, "0.0,1.0,0.001", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_max_concavity()));
  1406. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "decomposition/symmetry_planes_clipping_bias", PROPERTY_HINT_RANGE, "0.0,1.0,0.001", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_symmetry_planes_clipping_bias()));
  1407. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "decomposition/revolution_axes_clipping_bias", PROPERTY_HINT_RANGE, "0.0,1.0,0.001", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_revolution_axes_clipping_bias()));
  1408. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "decomposition/min_volume_per_convex_hull", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_min_volume_per_convex_hull()));
  1409. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "decomposition/resolution", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_resolution()));
  1410. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "decomposition/max_num_vertices_per_convex_hull", PROPERTY_HINT_RANGE, "5,512,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_max_num_vertices_per_convex_hull()));
  1411. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "decomposition/plane_downsampling", PROPERTY_HINT_RANGE, "1,16,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_plane_downsampling()));
  1412. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "decomposition/convexhull_downsampling", PROPERTY_HINT_RANGE, "1,16,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_convex_hull_downsampling()));
  1413. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "decomposition/normalize_mesh", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_normalize_mesh()));
  1414. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "decomposition/mode", PROPERTY_HINT_ENUM, "Voxel,Tetrahedron", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), static_cast<int>(decomposition_default->get_mode())));
  1415. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "decomposition/convexhull_approximation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_convex_hull_approximation()));
  1416. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "decomposition/max_convex_hulls", PROPERTY_HINT_RANGE, "1,100,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_max_convex_hulls()));
  1417. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "decomposition/project_hull_vertices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), decomposition_default->get_project_hull_vertices()));
  1418. // Primitives: Box, Sphere, Cylinder, Capsule.
  1419. r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "primitive/size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), Vector3(2.0, 2.0, 2.0)));
  1420. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "primitive/height", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 1.0));
  1421. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "primitive/radius", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 1.0));
  1422. r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "primitive/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), Vector3()));
  1423. r_options->push_back(ImportOption(PropertyInfo(Variant::VECTOR3, "primitive/rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), Vector3()));
  1424. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/occluder", PROPERTY_HINT_ENUM, "Disabled,Mesh + Occluder,Occluder Only", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0));
  1425. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "occluder/simplification_distance", PROPERTY_HINT_RANGE, "0.0,2.0,0.01", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0.1f));
  1426. } break;
  1427. case INTERNAL_IMPORT_CATEGORY_MESH: {
  1428. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "save_to_file/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
  1429. r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "save_to_file/path", PROPERTY_HINT_SAVE_FILE, "*.res,*.tres"), ""));
  1430. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "save_to_file/make_streamable"), ""));
  1431. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/shadow_meshes", PROPERTY_HINT_ENUM, "Default,Enable,Disable"), 0));
  1432. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/lightmap_uv", PROPERTY_HINT_ENUM, "Default,Enable,Disable"), 0));
  1433. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/lods", PROPERTY_HINT_ENUM, "Default,Enable,Disable"), 0));
  1434. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "lods/normal_split_angle", PROPERTY_HINT_RANGE, "0,180,0.1,degrees"), 25.0f));
  1435. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "lods/normal_merge_angle", PROPERTY_HINT_RANGE, "0,180,0.1,degrees"), 60.0f));
  1436. } break;
  1437. case INTERNAL_IMPORT_CATEGORY_MATERIAL: {
  1438. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "use_external/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
  1439. r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "use_external/path", PROPERTY_HINT_FILE, "*.material,*.res,*.tres"), ""));
  1440. } break;
  1441. case INTERNAL_IMPORT_CATEGORY_ANIMATION: {
  1442. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "settings/loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Pingpong"), 0));
  1443. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "save_to_file/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
  1444. r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "save_to_file/path", PROPERTY_HINT_SAVE_FILE, "*.res,*.tres"), ""));
  1445. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "save_to_file/keep_custom_tracks"), ""));
  1446. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slices/amount", PROPERTY_HINT_RANGE, "0,256,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0));
  1447. for (int i = 0; i < 256; i++) {
  1448. r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "slice_" + itos(i + 1) + "/name"), ""));
  1449. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slice_" + itos(i + 1) + "/start_frame"), 0));
  1450. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slice_" + itos(i + 1) + "/end_frame"), 0));
  1451. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slice_" + itos(i + 1) + "/loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Pingpong"), 0));
  1452. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "slice_" + itos(i + 1) + "/save_to_file/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
  1453. r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "slice_" + itos(i + 1) + "/save_to_file/path", PROPERTY_HINT_SAVE_FILE, ".res,*.tres"), ""));
  1454. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "slice_" + itos(i + 1) + "/save_to_file/keep_custom_tracks"), false));
  1455. }
  1456. } break;
  1457. case INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE: {
  1458. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "import/skip_import", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
  1459. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "optimizer/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), true));
  1460. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "optimizer/max_velocity_error", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.01));
  1461. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "optimizer/max_angular_error", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.01));
  1462. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "optimizer/max_precision_error", PROPERTY_HINT_NONE, "1,6,1"), 3));
  1463. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compression/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
  1464. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compression/page_size", PROPERTY_HINT_RANGE, "4,512,1,suffix:kb"), 8));
  1465. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "import_tracks/position", PROPERTY_HINT_ENUM, "IfPresent,IfPresentForAll,Never"), 1));
  1466. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "import_tracks/rotation", PROPERTY_HINT_ENUM, "IfPresent,IfPresentForAll,Never"), 1));
  1467. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "import_tracks/scale", PROPERTY_HINT_ENUM, "IfPresent,IfPresentForAll,Never"), 1));
  1468. } break;
  1469. case INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE: {
  1470. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "import/skip_import", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false));
  1471. r_options->push_back(ImportOption(PropertyInfo(Variant::OBJECT, "retarget/bone_map", PROPERTY_HINT_RESOURCE_TYPE, "BoneMap", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), Variant()));
  1472. } break;
  1473. default: {
  1474. }
  1475. }
  1476. for (int i = 0; i < post_importer_plugins.size(); i++) {
  1477. post_importer_plugins.write[i]->get_internal_import_options(EditorScenePostImportPlugin::InternalImportCategory(p_category), r_options);
  1478. }
  1479. }
  1480. bool ResourceImporterScene::get_internal_option_visibility(InternalImportCategory p_category, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  1481. if (p_options.has("import/skip_import") && p_option != "import/skip_import" && bool(p_options["import/skip_import"])) {
  1482. return false; //if skip import
  1483. }
  1484. switch (p_category) {
  1485. case INTERNAL_IMPORT_CATEGORY_NODE: {
  1486. } break;
  1487. case INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE: {
  1488. const bool generate_physics =
  1489. p_options.has("generate/physics") &&
  1490. p_options["generate/physics"].operator bool();
  1491. if (
  1492. p_option == "physics/body_type" ||
  1493. p_option == "physics/shape_type") {
  1494. // Show if need to generate collisions.
  1495. return generate_physics;
  1496. }
  1497. if (p_option.find("decomposition/") >= 0) {
  1498. // Show if need to generate collisions.
  1499. if (generate_physics &&
  1500. // Show if convex is enabled.
  1501. p_options["physics/shape_type"] == Variant(SHAPE_TYPE_DECOMPOSE_CONVEX)) {
  1502. if (p_option == "decomposition/advanced") {
  1503. return true;
  1504. }
  1505. const bool decomposition_advanced =
  1506. p_options.has("decomposition/advanced") &&
  1507. p_options["decomposition/advanced"].operator bool();
  1508. if (p_option == "decomposition/precision") {
  1509. return !decomposition_advanced;
  1510. } else {
  1511. return decomposition_advanced;
  1512. }
  1513. }
  1514. return false;
  1515. }
  1516. if (p_option == "primitive/position" || p_option == "primitive/rotation") {
  1517. const ShapeType physics_shape = (ShapeType)p_options["physics/shape_type"].operator int();
  1518. return generate_physics &&
  1519. physics_shape >= SHAPE_TYPE_BOX;
  1520. }
  1521. if (p_option == "primitive/size") {
  1522. const ShapeType physics_shape = (ShapeType)p_options["physics/shape_type"].operator int();
  1523. return generate_physics &&
  1524. physics_shape == SHAPE_TYPE_BOX;
  1525. }
  1526. if (p_option == "primitive/radius") {
  1527. const ShapeType physics_shape = (ShapeType)p_options["physics/shape_type"].operator int();
  1528. return generate_physics &&
  1529. (physics_shape == SHAPE_TYPE_SPHERE ||
  1530. physics_shape == SHAPE_TYPE_CYLINDER ||
  1531. physics_shape == SHAPE_TYPE_CAPSULE);
  1532. }
  1533. if (p_option == "primitive/height") {
  1534. const ShapeType physics_shape = (ShapeType)p_options["physics/shape_type"].operator int();
  1535. return generate_physics &&
  1536. (physics_shape == SHAPE_TYPE_CYLINDER ||
  1537. physics_shape == SHAPE_TYPE_CAPSULE);
  1538. }
  1539. if (p_option == "occluder/simplification_distance") {
  1540. // Show only if occluder generation is enabled
  1541. return p_options.has("generate/occluder") && p_options["generate/occluder"].operator signed int() != OCCLUDER_DISABLED;
  1542. }
  1543. } break;
  1544. case INTERNAL_IMPORT_CATEGORY_MESH: {
  1545. if (p_option == "save_to_file/path" || p_option == "save_to_file/make_streamable") {
  1546. return p_options["save_to_file/enabled"];
  1547. }
  1548. } break;
  1549. case INTERNAL_IMPORT_CATEGORY_MATERIAL: {
  1550. if (p_option == "use_external/path") {
  1551. return p_options["use_external/enabled"];
  1552. }
  1553. } break;
  1554. case INTERNAL_IMPORT_CATEGORY_ANIMATION: {
  1555. if (p_option == "save_to_file/path" || p_option == "save_to_file/keep_custom_tracks") {
  1556. return p_options["save_to_file/enabled"];
  1557. }
  1558. if (p_option.begins_with("slice_")) {
  1559. int max_slice = p_options["slices/amount"];
  1560. int slice = p_option.get_slice("_", 1).to_int() - 1;
  1561. if (slice >= max_slice) {
  1562. return false;
  1563. }
  1564. }
  1565. } break;
  1566. case INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE: {
  1567. if (p_option.begins_with("optimizer/") && p_option != "optimizer/enabled" && !bool(p_options["optimizer/enabled"])) {
  1568. return false;
  1569. }
  1570. if (p_option.begins_with("compression/") && p_option != "compression/enabled" && !bool(p_options["compression/enabled"])) {
  1571. return false;
  1572. }
  1573. } break;
  1574. case INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE: {
  1575. const bool use_retarget = p_options["retarget/bone_map"].get_validated_object() != nullptr;
  1576. if (p_option != "retarget/bone_map" && p_option.begins_with("retarget/")) {
  1577. return use_retarget;
  1578. }
  1579. } break;
  1580. default: {
  1581. }
  1582. }
  1583. for (int i = 0; i < post_importer_plugins.size(); i++) {
  1584. Variant ret = post_importer_plugins.write[i]->get_internal_option_visibility(EditorScenePostImportPlugin::InternalImportCategory(p_category), animation_importer, p_option, p_options);
  1585. if (ret.get_type() == Variant::BOOL) {
  1586. return ret;
  1587. }
  1588. }
  1589. return true;
  1590. }
  1591. bool ResourceImporterScene::get_internal_option_update_view_required(InternalImportCategory p_category, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
  1592. switch (p_category) {
  1593. case INTERNAL_IMPORT_CATEGORY_NODE: {
  1594. } break;
  1595. case INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE: {
  1596. if (
  1597. p_option == "generate/physics" ||
  1598. p_option == "physics/shape_type" ||
  1599. p_option.find("decomposition/") >= 0 ||
  1600. p_option.find("primitive/") >= 0) {
  1601. return true;
  1602. }
  1603. } break;
  1604. case INTERNAL_IMPORT_CATEGORY_MESH: {
  1605. } break;
  1606. case INTERNAL_IMPORT_CATEGORY_MATERIAL: {
  1607. } break;
  1608. case INTERNAL_IMPORT_CATEGORY_ANIMATION: {
  1609. } break;
  1610. case INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE: {
  1611. } break;
  1612. case INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE: {
  1613. } break;
  1614. default: {
  1615. }
  1616. }
  1617. for (int i = 0; i < post_importer_plugins.size(); i++) {
  1618. Variant ret = post_importer_plugins.write[i]->get_internal_option_update_view_required(EditorScenePostImportPlugin::InternalImportCategory(p_category), p_option, p_options);
  1619. if (ret.get_type() == Variant::BOOL) {
  1620. return ret;
  1621. }
  1622. }
  1623. return false;
  1624. }
  1625. void ResourceImporterScene::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
  1626. r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "nodes/root_type", PROPERTY_HINT_TYPE_STRING, "Node"), "Node3D"));
  1627. r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "nodes/root_name"), "Scene Root"));
  1628. List<String> script_extentions;
  1629. ResourceLoader::get_recognized_extensions_for_type("Script", &script_extentions);
  1630. String script_ext_hint;
  1631. for (const String &E : script_extentions) {
  1632. if (!script_ext_hint.is_empty()) {
  1633. script_ext_hint += ",";
  1634. }
  1635. script_ext_hint += "*." + E;
  1636. }
  1637. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "nodes/apply_root_scale"), true));
  1638. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "nodes/root_scale", PROPERTY_HINT_RANGE, "0.001,1000,0.001"), 1.0));
  1639. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "meshes/ensure_tangents"), true));
  1640. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "meshes/generate_lods"), true));
  1641. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "meshes/create_shadow_meshes"), true));
  1642. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "meshes/light_baking", PROPERTY_HINT_ENUM, "Disabled,Static (VoxelGI/SDFGI),Static Lightmaps (VoxelGI/SDFGI/LightmapGI),Dynamic (VoxelGI only)", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 1));
  1643. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "meshes/lightmap_texel_size", PROPERTY_HINT_RANGE, "0.001,100,0.001"), 0.2));
  1644. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "skins/use_named_skins"), true));
  1645. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/import"), true));
  1646. r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "animation/fps", PROPERTY_HINT_RANGE, "1,120,1"), 30));
  1647. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/trimming"), false));
  1648. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/remove_immutable_tracks"), true));
  1649. r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "import_script/path", PROPERTY_HINT_FILE, script_ext_hint), ""));
  1650. r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "_subresources", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), Dictionary()));
  1651. for (int i = 0; i < post_importer_plugins.size(); i++) {
  1652. post_importer_plugins.write[i]->get_import_options(p_path, r_options);
  1653. }
  1654. for (Ref<EditorSceneFormatImporter> importer_elem : importers) {
  1655. importer_elem->get_import_options(p_path, r_options);
  1656. }
  1657. }
  1658. void ResourceImporterScene::_replace_owner(Node *p_node, Node *p_scene, Node *p_new_owner) {
  1659. if (p_node != p_new_owner && p_node->get_owner() == p_scene) {
  1660. p_node->set_owner(p_new_owner);
  1661. }
  1662. for (int i = 0; i < p_node->get_child_count(); i++) {
  1663. Node *n = p_node->get_child(i);
  1664. _replace_owner(n, p_scene, p_new_owner);
  1665. }
  1666. }
  1667. Array ResourceImporterScene::_get_skinned_pose_transforms(ImporterMeshInstance3D *p_src_mesh_node) {
  1668. Array skin_pose_transform_array;
  1669. const Ref<Skin> skin = p_src_mesh_node->get_skin();
  1670. if (skin.is_valid()) {
  1671. NodePath skeleton_path = p_src_mesh_node->get_skeleton_path();
  1672. const Node *node = p_src_mesh_node->get_node_or_null(skeleton_path);
  1673. const Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
  1674. if (skeleton) {
  1675. int bind_count = skin->get_bind_count();
  1676. for (int i = 0; i < bind_count; i++) {
  1677. Transform3D bind_pose = skin->get_bind_pose(i);
  1678. String bind_name = skin->get_bind_name(i);
  1679. int bone_idx = bind_name.is_empty() ? skin->get_bind_bone(i) : skeleton->find_bone(bind_name);
  1680. ERR_FAIL_COND_V(bone_idx >= skeleton->get_bone_count(), Array());
  1681. Transform3D bp_global_rest;
  1682. if (bone_idx >= 0) {
  1683. bp_global_rest = skeleton->get_bone_global_pose(bone_idx);
  1684. } else {
  1685. bp_global_rest = skeleton->get_bone_global_pose(i);
  1686. }
  1687. skin_pose_transform_array.push_back(bp_global_rest * bind_pose);
  1688. }
  1689. }
  1690. }
  1691. return skin_pose_transform_array;
  1692. }
  1693. void ResourceImporterScene::_generate_meshes(Node *p_node, const Dictionary &p_mesh_data, bool p_generate_lods, bool p_create_shadow_meshes, LightBakeMode p_light_bake_mode, float p_lightmap_texel_size, const Vector<uint8_t> &p_src_lightmap_cache, Vector<Vector<uint8_t>> &r_lightmap_caches) {
  1694. ImporterMeshInstance3D *src_mesh_node = Object::cast_to<ImporterMeshInstance3D>(p_node);
  1695. if (src_mesh_node) {
  1696. //is mesh
  1697. MeshInstance3D *mesh_node = memnew(MeshInstance3D);
  1698. mesh_node->set_name(src_mesh_node->get_name());
  1699. mesh_node->set_transform(src_mesh_node->get_transform());
  1700. mesh_node->set_skin(src_mesh_node->get_skin());
  1701. mesh_node->set_skeleton_path(src_mesh_node->get_skeleton_path());
  1702. if (src_mesh_node->get_mesh().is_valid()) {
  1703. Ref<ArrayMesh> mesh;
  1704. if (!src_mesh_node->get_mesh()->has_mesh()) {
  1705. //do mesh processing
  1706. bool generate_lods = p_generate_lods;
  1707. float split_angle = 25.0f;
  1708. float merge_angle = 60.0f;
  1709. bool create_shadow_meshes = p_create_shadow_meshes;
  1710. bool bake_lightmaps = p_light_bake_mode == LIGHT_BAKE_STATIC_LIGHTMAPS;
  1711. String save_to_file;
  1712. String mesh_id = src_mesh_node->get_mesh()->get_meta("import_id", src_mesh_node->get_mesh()->get_name());
  1713. if (!mesh_id.is_empty() && p_mesh_data.has(mesh_id)) {
  1714. Dictionary mesh_settings = p_mesh_data[mesh_id];
  1715. {
  1716. //fill node settings for this node with default values
  1717. List<ImportOption> iopts;
  1718. get_internal_import_options(INTERNAL_IMPORT_CATEGORY_MESH, &iopts);
  1719. for (const ImportOption &E : iopts) {
  1720. if (!mesh_settings.has(E.option.name)) {
  1721. mesh_settings[E.option.name] = E.default_value;
  1722. }
  1723. }
  1724. }
  1725. if (mesh_settings.has("generate/shadow_meshes")) {
  1726. int shadow_meshes = mesh_settings["generate/shadow_meshes"];
  1727. if (shadow_meshes == MESH_OVERRIDE_ENABLE) {
  1728. create_shadow_meshes = true;
  1729. } else if (shadow_meshes == MESH_OVERRIDE_DISABLE) {
  1730. create_shadow_meshes = false;
  1731. }
  1732. }
  1733. if (mesh_settings.has("generate/lightmap_uv")) {
  1734. int lightmap_uv = mesh_settings["generate/lightmap_uv"];
  1735. if (lightmap_uv == MESH_OVERRIDE_ENABLE) {
  1736. bake_lightmaps = true;
  1737. } else if (lightmap_uv == MESH_OVERRIDE_DISABLE) {
  1738. bake_lightmaps = false;
  1739. }
  1740. }
  1741. if (mesh_settings.has("generate/lods")) {
  1742. int lods = mesh_settings["generate/lods"];
  1743. if (lods == MESH_OVERRIDE_ENABLE) {
  1744. generate_lods = true;
  1745. } else if (lods == MESH_OVERRIDE_DISABLE) {
  1746. generate_lods = false;
  1747. }
  1748. }
  1749. if (mesh_settings.has("lods/normal_split_angle")) {
  1750. split_angle = mesh_settings["lods/normal_split_angle"];
  1751. }
  1752. if (mesh_settings.has("lods/normal_merge_angle")) {
  1753. merge_angle = mesh_settings["lods/normal_merge_angle"];
  1754. }
  1755. if (mesh_settings.has("save_to_file/enabled") && bool(mesh_settings["save_to_file/enabled"]) && mesh_settings.has("save_to_file/path")) {
  1756. save_to_file = mesh_settings["save_to_file/path"];
  1757. if (!save_to_file.is_resource_file()) {
  1758. save_to_file = "";
  1759. }
  1760. }
  1761. for (int i = 0; i < post_importer_plugins.size(); i++) {
  1762. post_importer_plugins.write[i]->internal_process(EditorScenePostImportPlugin::INTERNAL_IMPORT_CATEGORY_MESH, nullptr, src_mesh_node, src_mesh_node->get_mesh(), mesh_settings);
  1763. }
  1764. }
  1765. if (bake_lightmaps) {
  1766. Transform3D xf;
  1767. Node3D *n = src_mesh_node;
  1768. while (n) {
  1769. xf = n->get_transform() * xf;
  1770. n = n->get_parent_node_3d();
  1771. }
  1772. Vector<uint8_t> lightmap_cache;
  1773. src_mesh_node->get_mesh()->lightmap_unwrap_cached(xf, p_lightmap_texel_size, p_src_lightmap_cache, lightmap_cache);
  1774. if (!lightmap_cache.is_empty()) {
  1775. if (r_lightmap_caches.is_empty()) {
  1776. r_lightmap_caches.push_back(lightmap_cache);
  1777. } else {
  1778. String new_md5 = String::md5(lightmap_cache.ptr()); // MD5 is stored at the beginning of the cache data
  1779. for (int i = 0; i < r_lightmap_caches.size(); i++) {
  1780. String md5 = String::md5(r_lightmap_caches[i].ptr());
  1781. if (new_md5 < md5) {
  1782. r_lightmap_caches.insert(i, lightmap_cache);
  1783. break;
  1784. }
  1785. if (new_md5 == md5) {
  1786. break;
  1787. }
  1788. }
  1789. }
  1790. }
  1791. }
  1792. if (generate_lods) {
  1793. Array skin_pose_transform_array = _get_skinned_pose_transforms(src_mesh_node);
  1794. src_mesh_node->get_mesh()->generate_lods(merge_angle, split_angle, skin_pose_transform_array);
  1795. }
  1796. if (create_shadow_meshes) {
  1797. src_mesh_node->get_mesh()->create_shadow_mesh();
  1798. }
  1799. if (!save_to_file.is_empty()) {
  1800. Ref<Mesh> existing = ResourceCache::get_ref(save_to_file);
  1801. if (existing.is_valid()) {
  1802. //if somehow an existing one is useful, create
  1803. existing->reset_state();
  1804. }
  1805. mesh = src_mesh_node->get_mesh()->get_mesh(existing);
  1806. ResourceSaver::save(mesh, save_to_file); //override
  1807. mesh->set_path(save_to_file, true); //takeover existing, if needed
  1808. } else {
  1809. mesh = src_mesh_node->get_mesh()->get_mesh();
  1810. }
  1811. } else {
  1812. mesh = src_mesh_node->get_mesh()->get_mesh();
  1813. }
  1814. if (mesh.is_valid()) {
  1815. mesh_node->set_mesh(mesh);
  1816. for (int i = 0; i < mesh->get_surface_count(); i++) {
  1817. mesh_node->set_surface_override_material(i, src_mesh_node->get_surface_material(i));
  1818. }
  1819. }
  1820. }
  1821. switch (p_light_bake_mode) {
  1822. case LIGHT_BAKE_DISABLED: {
  1823. mesh_node->set_gi_mode(GeometryInstance3D::GI_MODE_DISABLED);
  1824. } break;
  1825. case LIGHT_BAKE_DYNAMIC: {
  1826. mesh_node->set_gi_mode(GeometryInstance3D::GI_MODE_DYNAMIC);
  1827. } break;
  1828. case LIGHT_BAKE_STATIC:
  1829. case LIGHT_BAKE_STATIC_LIGHTMAPS: {
  1830. mesh_node->set_gi_mode(GeometryInstance3D::GI_MODE_STATIC);
  1831. } break;
  1832. }
  1833. mesh_node->set_visible(src_mesh_node->is_visible());
  1834. p_node->replace_by(mesh_node);
  1835. p_node->set_owner(nullptr);
  1836. memdelete(p_node);
  1837. p_node = mesh_node;
  1838. }
  1839. for (int i = 0; i < p_node->get_child_count(); i++) {
  1840. _generate_meshes(p_node->get_child(i), p_mesh_data, p_generate_lods, p_create_shadow_meshes, p_light_bake_mode, p_lightmap_texel_size, p_src_lightmap_cache, r_lightmap_caches);
  1841. }
  1842. }
  1843. void ResourceImporterScene::_add_shapes(Node *p_node, const Vector<Ref<Shape3D>> &p_shapes) {
  1844. for (const Ref<Shape3D> &E : p_shapes) {
  1845. CollisionShape3D *cshape = memnew(CollisionShape3D);
  1846. cshape->set_shape(E);
  1847. p_node->add_child(cshape, true);
  1848. cshape->set_owner(p_node->get_owner());
  1849. }
  1850. }
  1851. void ResourceImporterScene::_optimize_track_usage(AnimationPlayer *p_player, AnimationImportTracks *p_track_actions) {
  1852. List<StringName> anims;
  1853. p_player->get_animation_list(&anims);
  1854. Node *parent = p_player->get_parent();
  1855. ERR_FAIL_COND(parent == nullptr);
  1856. HashMap<NodePath, uint32_t> used_tracks[TRACK_CHANNEL_MAX];
  1857. bool tracks_to_add = false;
  1858. static const Animation::TrackType track_types[TRACK_CHANNEL_MAX] = { Animation::TYPE_POSITION_3D, Animation::TYPE_ROTATION_3D, Animation::TYPE_SCALE_3D, Animation::TYPE_BLEND_SHAPE };
  1859. for (const StringName &I : anims) {
  1860. Ref<Animation> anim = p_player->get_animation(I);
  1861. for (int i = 0; i < anim->get_track_count(); i++) {
  1862. for (int j = 0; j < TRACK_CHANNEL_MAX; j++) {
  1863. if (anim->track_get_type(i) != track_types[j]) {
  1864. continue;
  1865. }
  1866. switch (p_track_actions[j]) {
  1867. case ANIMATION_IMPORT_TRACKS_IF_PRESENT: {
  1868. // Do Nothing.
  1869. } break;
  1870. case ANIMATION_IMPORT_TRACKS_IF_PRESENT_FOR_ALL: {
  1871. used_tracks[j].insert(anim->track_get_path(i), 0);
  1872. tracks_to_add = true;
  1873. } break;
  1874. case ANIMATION_IMPORT_TRACKS_NEVER: {
  1875. anim->remove_track(i);
  1876. i--;
  1877. } break;
  1878. }
  1879. }
  1880. }
  1881. }
  1882. if (!tracks_to_add) {
  1883. return;
  1884. }
  1885. uint32_t pass = 0;
  1886. for (const StringName &I : anims) {
  1887. Ref<Animation> anim = p_player->get_animation(I);
  1888. for (int j = 0; j < TRACK_CHANNEL_MAX; j++) {
  1889. if (p_track_actions[j] != ANIMATION_IMPORT_TRACKS_IF_PRESENT_FOR_ALL) {
  1890. continue;
  1891. }
  1892. pass++;
  1893. for (int i = 0; i < anim->get_track_count(); i++) {
  1894. if (anim->track_get_type(i) != track_types[j]) {
  1895. continue;
  1896. }
  1897. NodePath path = anim->track_get_path(i);
  1898. ERR_CONTINUE(!used_tracks[j].has(path)); // Should never happen.
  1899. used_tracks[j][path] = pass;
  1900. }
  1901. for (const KeyValue<NodePath, uint32_t> &J : used_tracks[j]) {
  1902. if (J.value == pass) {
  1903. continue;
  1904. }
  1905. NodePath path = J.key;
  1906. Node *n = parent->get_node(path);
  1907. if (j == TRACK_CHANNEL_BLEND_SHAPE) {
  1908. MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(n);
  1909. if (mi && path.get_subname_count() > 0) {
  1910. StringName bs = path.get_subname(0);
  1911. bool valid;
  1912. float value = mi->get(bs, &valid);
  1913. if (valid) {
  1914. int track_idx = anim->add_track(track_types[j]);
  1915. anim->track_set_path(track_idx, path);
  1916. anim->track_set_imported(track_idx, true);
  1917. anim->blend_shape_track_insert_key(track_idx, 0, value);
  1918. }
  1919. }
  1920. } else {
  1921. Skeleton3D *skel = Object::cast_to<Skeleton3D>(n);
  1922. Node3D *n3d = Object::cast_to<Node3D>(n);
  1923. Vector3 loc;
  1924. Quaternion rot;
  1925. Vector3 scale;
  1926. if (skel && path.get_subname_count() > 0) {
  1927. StringName bone = path.get_subname(0);
  1928. int bone_idx = skel->find_bone(bone);
  1929. if (bone_idx == -1) {
  1930. continue;
  1931. }
  1932. // Note that this is using get_bone_pose to update the bone pose cache.
  1933. _ALLOW_DISCARD_ skel->get_bone_pose(bone_idx);
  1934. loc = skel->get_bone_pose_position(bone_idx);
  1935. rot = skel->get_bone_pose_rotation(bone_idx);
  1936. scale = skel->get_bone_pose_scale(bone_idx);
  1937. } else if (n3d) {
  1938. loc = n3d->get_position();
  1939. rot = n3d->get_transform().basis.get_rotation_quaternion();
  1940. scale = n3d->get_scale();
  1941. } else {
  1942. continue;
  1943. }
  1944. // Ensure insertion keeps tracks together and ordered by type (loc/rot/scale)
  1945. int insert_at_pos = -1;
  1946. for (int k = 0; k < anim->get_track_count(); k++) {
  1947. NodePath tpath = anim->track_get_path(k);
  1948. if (path == tpath) {
  1949. Animation::TrackType ttype = anim->track_get_type(k);
  1950. if (insert_at_pos == -1) {
  1951. // First insert, determine whether replacing or kicking back
  1952. if (track_types[j] < ttype) {
  1953. insert_at_pos = k;
  1954. break; // No point in continuing.
  1955. } else {
  1956. insert_at_pos = k + 1;
  1957. }
  1958. } else if (ttype < track_types[j]) {
  1959. // Kick back.
  1960. insert_at_pos = k + 1;
  1961. }
  1962. } else if (insert_at_pos >= 0) {
  1963. break;
  1964. }
  1965. }
  1966. int track_idx = anim->add_track(track_types[j], insert_at_pos);
  1967. anim->track_set_path(track_idx, path);
  1968. anim->track_set_imported(track_idx, true);
  1969. switch (j) {
  1970. case TRACK_CHANNEL_POSITION: {
  1971. anim->position_track_insert_key(track_idx, 0, loc);
  1972. } break;
  1973. case TRACK_CHANNEL_ROTATION: {
  1974. anim->rotation_track_insert_key(track_idx, 0, rot);
  1975. } break;
  1976. case TRACK_CHANNEL_SCALE: {
  1977. anim->scale_track_insert_key(track_idx, 0, scale);
  1978. } break;
  1979. default: {
  1980. }
  1981. }
  1982. }
  1983. }
  1984. }
  1985. }
  1986. }
  1987. Node *ResourceImporterScene::pre_import(const String &p_source_file, const HashMap<StringName, Variant> &p_options) {
  1988. Ref<EditorSceneFormatImporter> importer;
  1989. String ext = p_source_file.get_extension().to_lower();
  1990. // TRANSLATORS: This is an editor progress label.
  1991. EditorProgress progress("pre-import", TTR("Pre-Import Scene"), 0);
  1992. progress.step(TTR("Importing Scene..."), 0);
  1993. for (Ref<EditorSceneFormatImporter> importer_elem : importers) {
  1994. List<String> extensions;
  1995. importer_elem->get_extensions(&extensions);
  1996. for (const String &F : extensions) {
  1997. if (F.to_lower() == ext) {
  1998. importer = importer_elem;
  1999. break;
  2000. }
  2001. }
  2002. if (importer.is_valid()) {
  2003. break;
  2004. }
  2005. }
  2006. ERR_FAIL_COND_V(!importer.is_valid(), nullptr);
  2007. Error err = OK;
  2008. HashMap<StringName, Variant> options_dupe = p_options;
  2009. Node *scene = importer->import_scene(p_source_file, EditorSceneFormatImporter::IMPORT_ANIMATION | EditorSceneFormatImporter::IMPORT_GENERATE_TANGENT_ARRAYS, options_dupe, nullptr, &err);
  2010. if (!scene || err != OK) {
  2011. return nullptr;
  2012. }
  2013. HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> collision_map;
  2014. List<Pair<NodePath, Node *>> node_renames;
  2015. _pre_fix_node(scene, scene, collision_map, nullptr, node_renames);
  2016. return scene;
  2017. }
  2018. Error ResourceImporterScene::import(const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
  2019. const String &src_path = p_source_file;
  2020. Ref<EditorSceneFormatImporter> importer;
  2021. String ext = src_path.get_extension().to_lower();
  2022. EditorProgress progress("import", TTR("Import Scene"), 104);
  2023. progress.step(TTR("Importing Scene..."), 0);
  2024. for (Ref<EditorSceneFormatImporter> importer_elem : importers) {
  2025. List<String> extensions;
  2026. importer_elem->get_extensions(&extensions);
  2027. for (const String &F : extensions) {
  2028. if (F.to_lower() == ext) {
  2029. importer = importer_elem;
  2030. break;
  2031. }
  2032. }
  2033. if (importer.is_valid()) {
  2034. break;
  2035. }
  2036. }
  2037. ERR_FAIL_COND_V(!importer.is_valid(), ERR_FILE_UNRECOGNIZED);
  2038. int import_flags = 0;
  2039. if (animation_importer) {
  2040. import_flags |= EditorSceneFormatImporter::IMPORT_ANIMATION;
  2041. import_flags |= EditorSceneFormatImporter::IMPORT_DISCARD_MESHES_AND_MATERIALS;
  2042. } else {
  2043. if (bool(p_options["animation/import"])) {
  2044. import_flags |= EditorSceneFormatImporter::IMPORT_ANIMATION;
  2045. }
  2046. }
  2047. if (bool(p_options["skins/use_named_skins"])) {
  2048. import_flags |= EditorSceneFormatImporter::IMPORT_USE_NAMED_SKIN_BINDS;
  2049. }
  2050. bool ensure_tangents = p_options["meshes/ensure_tangents"];
  2051. if (ensure_tangents) {
  2052. import_flags |= EditorSceneFormatImporter::IMPORT_GENERATE_TANGENT_ARRAYS;
  2053. }
  2054. Error err = OK;
  2055. List<String> missing_deps; // for now, not much will be done with this
  2056. Node *scene = importer->import_scene(src_path, import_flags, p_options, &missing_deps, &err);
  2057. if (!scene || err != OK) {
  2058. return err;
  2059. }
  2060. bool apply_root = true;
  2061. if (p_options.has("nodes/apply_root_scale")) {
  2062. apply_root = p_options["nodes/apply_root_scale"];
  2063. }
  2064. real_t root_scale = 1;
  2065. if (p_options.has("nodes/root_scale")) {
  2066. root_scale = p_options["nodes/root_scale"];
  2067. }
  2068. if (Object::cast_to<Node3D>(scene)) {
  2069. Node3D *scene_3d = Object::cast_to<Node3D>(scene);
  2070. Vector3 scale = Vector3(root_scale, root_scale, root_scale);
  2071. if (apply_root) {
  2072. _apply_permanent_scale_to_descendants(scene, scale);
  2073. } else {
  2074. scene_3d->scale(scale);
  2075. }
  2076. }
  2077. Dictionary subresources = p_options["_subresources"];
  2078. Dictionary node_data;
  2079. if (subresources.has("nodes")) {
  2080. node_data = subresources["nodes"];
  2081. }
  2082. Dictionary material_data;
  2083. if (subresources.has("materials")) {
  2084. material_data = subresources["materials"];
  2085. }
  2086. Dictionary animation_data;
  2087. if (subresources.has("animations")) {
  2088. animation_data = subresources["animations"];
  2089. }
  2090. HashSet<Ref<ImporterMesh>> scanned_meshes;
  2091. HashMap<Ref<ImporterMesh>, Vector<Ref<Shape3D>>> collision_map;
  2092. Pair<PackedVector3Array, PackedInt32Array> occluder_arrays;
  2093. List<Pair<NodePath, Node *>> node_renames;
  2094. _pre_fix_node(scene, scene, collision_map, &occluder_arrays, node_renames);
  2095. for (int i = 0; i < post_importer_plugins.size(); i++) {
  2096. post_importer_plugins.write[i]->pre_process(scene, p_options);
  2097. }
  2098. float fps = 30;
  2099. if (p_options.has(SNAME("animation/fps"))) {
  2100. fps = (float)p_options[SNAME("animation/fps")];
  2101. }
  2102. _pre_fix_animations(scene, scene, node_data, animation_data, fps);
  2103. _post_fix_node(scene, scene, collision_map, occluder_arrays, scanned_meshes, node_data, material_data, animation_data, fps, apply_root ? root_scale : 1.0);
  2104. _post_fix_animations(scene, scene, node_data, animation_data, fps);
  2105. String root_type = p_options["nodes/root_type"];
  2106. root_type = root_type.split(" ")[0]; // full root_type is "ClassName (filename.gd)" for a script global class.
  2107. Ref<Script> root_script = nullptr;
  2108. if (ScriptServer::is_global_class(root_type)) {
  2109. root_script = ResourceLoader::load(ScriptServer::get_global_class_path(root_type));
  2110. root_type = ScriptServer::get_global_class_base(root_type);
  2111. }
  2112. if (root_type != "Node3D") {
  2113. Node *base_node = Object::cast_to<Node>(ClassDB::instantiate(root_type));
  2114. if (base_node) {
  2115. scene->replace_by(base_node);
  2116. scene->set_owner(nullptr);
  2117. memdelete(scene);
  2118. scene = base_node;
  2119. }
  2120. }
  2121. if (root_script.is_valid()) {
  2122. scene->set_script(Variant(root_script));
  2123. }
  2124. if (p_options["nodes/root_name"] != "Scene Root") {
  2125. scene->set_name(p_options["nodes/root_name"]);
  2126. } else {
  2127. scene->set_name(p_save_path.get_file().get_basename());
  2128. }
  2129. if (!occluder_arrays.first.is_empty() && !occluder_arrays.second.is_empty()) {
  2130. Ref<ArrayOccluder3D> occ = memnew(ArrayOccluder3D);
  2131. occ->set_arrays(occluder_arrays.first, occluder_arrays.second);
  2132. OccluderInstance3D *occluder_instance = memnew(OccluderInstance3D);
  2133. occluder_instance->set_occluder(occ);
  2134. scene->add_child(occluder_instance, true);
  2135. occluder_instance->set_owner(scene);
  2136. }
  2137. bool gen_lods = bool(p_options["meshes/generate_lods"]);
  2138. bool create_shadow_meshes = bool(p_options["meshes/create_shadow_meshes"]);
  2139. int light_bake_mode = p_options["meshes/light_baking"];
  2140. float texel_size = p_options["meshes/lightmap_texel_size"];
  2141. float lightmap_texel_size = MAX(0.001, texel_size);
  2142. Vector<uint8_t> src_lightmap_cache;
  2143. Vector<Vector<uint8_t>> mesh_lightmap_caches;
  2144. {
  2145. src_lightmap_cache = FileAccess::get_file_as_bytes(p_source_file + ".unwrap_cache", &err);
  2146. if (err != OK) {
  2147. src_lightmap_cache.clear();
  2148. }
  2149. }
  2150. Dictionary mesh_data;
  2151. if (subresources.has("meshes")) {
  2152. mesh_data = subresources["meshes"];
  2153. }
  2154. _generate_meshes(scene, mesh_data, gen_lods, create_shadow_meshes, LightBakeMode(light_bake_mode), lightmap_texel_size, src_lightmap_cache, mesh_lightmap_caches);
  2155. if (mesh_lightmap_caches.size()) {
  2156. Ref<FileAccess> f = FileAccess::open(p_source_file + ".unwrap_cache", FileAccess::WRITE);
  2157. if (f.is_valid()) {
  2158. f->store_32(mesh_lightmap_caches.size());
  2159. for (int i = 0; i < mesh_lightmap_caches.size(); i++) {
  2160. String md5 = String::md5(mesh_lightmap_caches[i].ptr());
  2161. f->store_buffer(mesh_lightmap_caches[i].ptr(), mesh_lightmap_caches[i].size());
  2162. }
  2163. }
  2164. }
  2165. err = OK;
  2166. progress.step(TTR("Running Custom Script..."), 2);
  2167. String post_import_script_path = p_options["import_script/path"];
  2168. Ref<EditorScenePostImport> post_import_script;
  2169. if (!post_import_script_path.is_empty()) {
  2170. Ref<Script> scr = ResourceLoader::load(post_import_script_path);
  2171. if (!scr.is_valid()) {
  2172. EditorNode::add_io_error(TTR("Couldn't load post-import script:") + " " + post_import_script_path);
  2173. } else {
  2174. post_import_script = Ref<EditorScenePostImport>(memnew(EditorScenePostImport));
  2175. post_import_script->set_script(scr);
  2176. if (!post_import_script->get_script_instance()) {
  2177. EditorNode::add_io_error(TTR("Invalid/broken script for post-import (check console):") + " " + post_import_script_path);
  2178. post_import_script.unref();
  2179. return ERR_CANT_CREATE;
  2180. }
  2181. }
  2182. }
  2183. if (post_import_script.is_valid()) {
  2184. post_import_script->init(p_source_file);
  2185. scene = post_import_script->post_import(scene);
  2186. if (!scene) {
  2187. EditorNode::add_io_error(
  2188. TTR("Error running post-import script:") + " " + post_import_script_path + "\n" +
  2189. TTR("Did you return a Node-derived object in the `_post_import()` method?"));
  2190. return err;
  2191. }
  2192. }
  2193. for (int i = 0; i < post_importer_plugins.size(); i++) {
  2194. post_importer_plugins.write[i]->post_process(scene, p_options);
  2195. }
  2196. progress.step(TTR("Saving..."), 104);
  2197. int flags = 0;
  2198. if (EDITOR_GET("filesystem/on_save/compress_binary_resources")) {
  2199. flags |= ResourceSaver::FLAG_COMPRESS;
  2200. }
  2201. if (animation_importer) {
  2202. Ref<AnimationLibrary> library;
  2203. for (int i = 0; i < scene->get_child_count(); i++) {
  2204. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(scene->get_child(i));
  2205. if (ap) {
  2206. List<StringName> libs;
  2207. ap->get_animation_library_list(&libs);
  2208. if (libs.size()) {
  2209. library = ap->get_animation_library(libs.front()->get());
  2210. break;
  2211. }
  2212. }
  2213. }
  2214. if (!library.is_valid()) {
  2215. library.instantiate(); // Will be empty
  2216. }
  2217. print_verbose("Saving animation to: " + p_save_path + ".res");
  2218. err = ResourceSaver::save(library, p_save_path + ".res", flags); //do not take over, let the changed files reload themselves
  2219. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save animation to file '" + p_save_path + ".res'.");
  2220. } else {
  2221. Ref<PackedScene> packer = memnew(PackedScene);
  2222. packer->pack(scene);
  2223. print_verbose("Saving scene to: " + p_save_path + ".scn");
  2224. err = ResourceSaver::save(packer, p_save_path + ".scn", flags); //do not take over, let the changed files reload themselves
  2225. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save scene to file '" + p_save_path + ".scn'.");
  2226. }
  2227. memdelete(scene);
  2228. //this is not the time to reimport, wait until import process is done, import file is saved, etc.
  2229. //EditorNode::get_singleton()->reload_scene(p_source_file);
  2230. return OK;
  2231. }
  2232. ResourceImporterScene *ResourceImporterScene::scene_singleton = nullptr;
  2233. ResourceImporterScene *ResourceImporterScene::animation_singleton = nullptr;
  2234. Vector<Ref<EditorSceneFormatImporter>> ResourceImporterScene::importers;
  2235. Vector<Ref<EditorScenePostImportPlugin>> ResourceImporterScene::post_importer_plugins;
  2236. bool ResourceImporterScene::ResourceImporterScene::has_advanced_options() const {
  2237. return true;
  2238. }
  2239. void ResourceImporterScene::ResourceImporterScene::show_advanced_options(const String &p_path) {
  2240. SceneImportSettings::get_singleton()->open_settings(p_path, animation_importer);
  2241. }
  2242. ResourceImporterScene::ResourceImporterScene(bool p_animation_import) {
  2243. if (p_animation_import) {
  2244. animation_singleton = this;
  2245. } else {
  2246. scene_singleton = this;
  2247. }
  2248. animation_importer = p_animation_import;
  2249. }
  2250. void ResourceImporterScene::add_importer(Ref<EditorSceneFormatImporter> p_importer, bool p_first_priority) {
  2251. ERR_FAIL_COND(p_importer.is_null());
  2252. if (p_first_priority) {
  2253. importers.insert(0, p_importer);
  2254. } else {
  2255. importers.push_back(p_importer);
  2256. }
  2257. }
  2258. void ResourceImporterScene::remove_post_importer_plugin(const Ref<EditorScenePostImportPlugin> &p_plugin) {
  2259. post_importer_plugins.erase(p_plugin);
  2260. }
  2261. void ResourceImporterScene::add_post_importer_plugin(const Ref<EditorScenePostImportPlugin> &p_plugin, bool p_first_priority) {
  2262. ERR_FAIL_COND(p_plugin.is_null());
  2263. if (p_first_priority) {
  2264. post_importer_plugins.insert(0, p_plugin);
  2265. } else {
  2266. post_importer_plugins.push_back(p_plugin);
  2267. }
  2268. }
  2269. void ResourceImporterScene::remove_importer(Ref<EditorSceneFormatImporter> p_importer) {
  2270. importers.erase(p_importer);
  2271. }
  2272. void ResourceImporterScene::clean_up_importer_plugins() {
  2273. importers.clear();
  2274. post_importer_plugins.clear();
  2275. }
  2276. ///////////////////////////////////////
  2277. uint32_t EditorSceneFormatImporterESCN::get_import_flags() const {
  2278. return IMPORT_SCENE;
  2279. }
  2280. void EditorSceneFormatImporterESCN::get_extensions(List<String> *r_extensions) const {
  2281. r_extensions->push_back("escn");
  2282. }
  2283. Node *EditorSceneFormatImporterESCN::import_scene(const String &p_path, uint32_t p_flags, const HashMap<StringName, Variant> &p_options, List<String> *r_missing_deps, Error *r_err) {
  2284. Error error;
  2285. Ref<PackedScene> ps = ResourceFormatLoaderText::singleton->load(p_path, p_path, &error);
  2286. ERR_FAIL_COND_V_MSG(!ps.is_valid(), nullptr, "Cannot load scene as text resource from path '" + p_path + "'.");
  2287. Node *scene = ps->instantiate();
  2288. TypedArray<Node> nodes = scene->find_children("*", "MeshInstance3D");
  2289. for (int32_t node_i = 0; node_i < nodes.size(); node_i++) {
  2290. MeshInstance3D *mesh_3d = cast_to<MeshInstance3D>(nodes[node_i]);
  2291. Ref<ImporterMesh> mesh;
  2292. mesh.instantiate();
  2293. // Ignore the aabb, it will be recomputed.
  2294. ImporterMeshInstance3D *importer_mesh_3d = memnew(ImporterMeshInstance3D);
  2295. importer_mesh_3d->set_name(mesh_3d->get_name());
  2296. importer_mesh_3d->set_transform(mesh_3d->get_relative_transform(mesh_3d->get_parent()));
  2297. importer_mesh_3d->set_skin(mesh_3d->get_skin());
  2298. importer_mesh_3d->set_skeleton_path(mesh_3d->get_skeleton_path());
  2299. Ref<ArrayMesh> array_mesh_3d_mesh = mesh_3d->get_mesh();
  2300. if (array_mesh_3d_mesh.is_valid()) {
  2301. // For the MeshInstance3D nodes, we need to convert the ArrayMesh to an ImporterMesh specially.
  2302. mesh->set_name(array_mesh_3d_mesh->get_name());
  2303. for (int32_t blend_i = 0; blend_i < array_mesh_3d_mesh->get_blend_shape_count(); blend_i++) {
  2304. mesh->add_blend_shape(array_mesh_3d_mesh->get_blend_shape_name(blend_i));
  2305. }
  2306. for (int32_t surface_i = 0; surface_i < array_mesh_3d_mesh->get_surface_count(); surface_i++) {
  2307. mesh->add_surface(array_mesh_3d_mesh->surface_get_primitive_type(surface_i),
  2308. array_mesh_3d_mesh->surface_get_arrays(surface_i),
  2309. array_mesh_3d_mesh->surface_get_blend_shape_arrays(surface_i),
  2310. array_mesh_3d_mesh->surface_get_lods(surface_i),
  2311. array_mesh_3d_mesh->surface_get_material(surface_i),
  2312. array_mesh_3d_mesh->surface_get_name(surface_i),
  2313. array_mesh_3d_mesh->surface_get_format(surface_i));
  2314. }
  2315. mesh->set_blend_shape_mode(array_mesh_3d_mesh->get_blend_shape_mode());
  2316. importer_mesh_3d->set_mesh(mesh);
  2317. mesh_3d->replace_by(importer_mesh_3d);
  2318. continue;
  2319. }
  2320. Ref<Mesh> mesh_3d_mesh = mesh_3d->get_mesh();
  2321. if (mesh_3d_mesh.is_valid()) {
  2322. // For the MeshInstance3D nodes, we need to convert the Mesh to an ImporterMesh specially.
  2323. mesh->set_name(mesh_3d_mesh->get_name());
  2324. for (int32_t surface_i = 0; surface_i < mesh_3d_mesh->get_surface_count(); surface_i++) {
  2325. mesh->add_surface(mesh_3d_mesh->surface_get_primitive_type(surface_i),
  2326. mesh_3d_mesh->surface_get_arrays(surface_i),
  2327. Array(),
  2328. mesh_3d_mesh->surface_get_lods(surface_i),
  2329. mesh_3d_mesh->surface_get_material(surface_i),
  2330. mesh_3d_mesh->surface_get_material(surface_i).is_valid() ? mesh_3d_mesh->surface_get_material(surface_i)->get_name() : String(),
  2331. mesh_3d_mesh->surface_get_format(surface_i));
  2332. }
  2333. importer_mesh_3d->set_mesh(mesh);
  2334. mesh_3d->replace_by(importer_mesh_3d);
  2335. continue;
  2336. }
  2337. }
  2338. ERR_FAIL_COND_V(!scene, nullptr);
  2339. return scene;
  2340. }