resource_importer_scene.cpp 94 KB

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