scene_import_settings.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. /*************************************************************************/
  2. /* scene_import_settings.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 "scene_import_settings.h"
  31. #include "core/config/project_settings.h"
  32. #include "editor/editor_file_dialog.h"
  33. #include "editor/editor_file_system.h"
  34. #include "editor/editor_inspector.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_scale.h"
  37. #include "editor/editor_settings.h"
  38. #include "scene/3d/importer_mesh_instance_3d.h"
  39. #include "scene/animation/animation_player.h"
  40. #include "scene/resources/importer_mesh.h"
  41. #include "scene/resources/surface_tool.h"
  42. class SceneImportSettingsData : public Object {
  43. GDCLASS(SceneImportSettingsData, Object)
  44. friend class SceneImportSettings;
  45. HashMap<StringName, Variant> *settings = nullptr;
  46. HashMap<StringName, Variant> current;
  47. HashMap<StringName, Variant> defaults;
  48. List<ResourceImporter::ImportOption> options;
  49. bool hide_options = false;
  50. String path;
  51. ResourceImporterScene::InternalImportCategory category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX;
  52. bool _set(const StringName &p_name, const Variant &p_value) {
  53. if (settings) {
  54. if (defaults.has(p_name) && defaults[p_name] == p_value) {
  55. settings->erase(p_name);
  56. } else {
  57. (*settings)[p_name] = p_value;
  58. }
  59. current[p_name] = p_value;
  60. if (SceneImportSettings::get_singleton()->is_editing_animation()) {
  61. if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  62. if (ResourceImporterScene::get_animation_singleton()->get_option_visibility(path, p_name, current)) {
  63. SceneImportSettings::get_singleton()->update_view();
  64. }
  65. } else {
  66. if (ResourceImporterScene::get_animation_singleton()->get_internal_option_update_view_required(category, p_name, current)) {
  67. SceneImportSettings::get_singleton()->update_view();
  68. }
  69. }
  70. } else {
  71. if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  72. if (ResourceImporterScene::get_scene_singleton()->get_option_visibility(path, p_name, current)) {
  73. SceneImportSettings::get_singleton()->update_view();
  74. }
  75. } else {
  76. if (ResourceImporterScene::get_scene_singleton()->get_internal_option_update_view_required(category, p_name, current)) {
  77. SceneImportSettings::get_singleton()->update_view();
  78. }
  79. }
  80. }
  81. return true;
  82. }
  83. return false;
  84. }
  85. bool _get(const StringName &p_name, Variant &r_ret) const {
  86. if (settings) {
  87. if (settings->has(p_name)) {
  88. r_ret = (*settings)[p_name];
  89. return true;
  90. }
  91. }
  92. if (defaults.has(p_name)) {
  93. r_ret = defaults[p_name];
  94. return true;
  95. }
  96. return false;
  97. }
  98. void _get_property_list(List<PropertyInfo> *p_list) const {
  99. if (hide_options) {
  100. return;
  101. }
  102. for (const ResourceImporter::ImportOption &E : options) {
  103. if (SceneImportSettings::get_singleton()->is_editing_animation()) {
  104. if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  105. if (ResourceImporterScene::get_animation_singleton()->get_option_visibility(path, E.option.name, current)) {
  106. p_list->push_back(E.option);
  107. }
  108. } else {
  109. if (ResourceImporterScene::get_animation_singleton()->get_internal_option_visibility(category, E.option.name, current)) {
  110. p_list->push_back(E.option);
  111. }
  112. }
  113. } else {
  114. if (category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  115. if (ResourceImporterScene::get_scene_singleton()->get_option_visibility(path, E.option.name, current)) {
  116. p_list->push_back(E.option);
  117. }
  118. } else {
  119. if (ResourceImporterScene::get_scene_singleton()->get_internal_option_visibility(category, E.option.name, current)) {
  120. p_list->push_back(E.option);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. };
  127. void SceneImportSettings::_fill_material(Tree *p_tree, const Ref<Material> &p_material, TreeItem *p_parent) {
  128. String import_id;
  129. bool has_import_id = false;
  130. bool created = false;
  131. if (!material_set.has(p_material)) {
  132. material_set.insert(p_material);
  133. created = true;
  134. }
  135. if (p_material->has_meta("import_id")) {
  136. import_id = p_material->get_meta("import_id");
  137. has_import_id = true;
  138. } else if (!p_material->get_name().is_empty()) {
  139. import_id = p_material->get_name();
  140. has_import_id = true;
  141. } else {
  142. import_id = "@MATERIAL:" + itos(material_set.size() - 1);
  143. }
  144. if (!material_map.has(import_id)) {
  145. MaterialData md;
  146. md.has_import_id = has_import_id;
  147. md.material = p_material;
  148. _load_default_subresource_settings(md.settings, "materials", import_id, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MATERIAL);
  149. material_map[import_id] = md;
  150. }
  151. MaterialData &material_data = material_map[import_id];
  152. Ref<Texture2D> icon = get_theme_icon(SNAME("StandardMaterial3D"), SNAME("EditorIcons"));
  153. TreeItem *item = p_tree->create_item(p_parent);
  154. if (p_material->get_name().is_empty()) {
  155. item->set_text(0, TTR("<Unnamed Material>"));
  156. } else {
  157. item->set_text(0, p_material->get_name());
  158. }
  159. item->set_icon(0, icon);
  160. item->set_meta("type", "Material");
  161. item->set_meta("import_id", import_id);
  162. item->set_tooltip_text(0, vformat(TTR("Import ID: %s"), import_id));
  163. item->set_selectable(0, true);
  164. if (p_tree == scene_tree) {
  165. material_data.scene_node = item;
  166. } else if (p_tree == mesh_tree) {
  167. material_data.mesh_node = item;
  168. } else {
  169. material_data.material_node = item;
  170. }
  171. if (created) {
  172. _fill_material(material_tree, p_material, material_tree->get_root());
  173. }
  174. }
  175. void SceneImportSettings::_fill_mesh(Tree *p_tree, const Ref<Mesh> &p_mesh, TreeItem *p_parent) {
  176. String import_id;
  177. bool has_import_id = false;
  178. if (p_mesh->has_meta("import_id")) {
  179. import_id = p_mesh->get_meta("import_id");
  180. has_import_id = true;
  181. } else if (!p_mesh->get_name().is_empty()) {
  182. import_id = p_mesh->get_name();
  183. has_import_id = true;
  184. } else {
  185. import_id = "@MESH:" + itos(mesh_set.size());
  186. }
  187. if (!mesh_map.has(import_id)) {
  188. MeshData md;
  189. md.has_import_id = has_import_id;
  190. md.mesh = p_mesh;
  191. _load_default_subresource_settings(md.settings, "meshes", import_id, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH);
  192. mesh_map[import_id] = md;
  193. }
  194. MeshData &mesh_data = mesh_map[import_id];
  195. Ref<Texture2D> icon = get_theme_icon(SNAME("Mesh"), SNAME("EditorIcons"));
  196. TreeItem *item = p_tree->create_item(p_parent);
  197. item->set_text(0, p_mesh->get_name());
  198. item->set_icon(0, icon);
  199. bool created = false;
  200. if (!mesh_set.has(p_mesh)) {
  201. mesh_set.insert(p_mesh);
  202. created = true;
  203. }
  204. item->set_meta("type", "Mesh");
  205. item->set_meta("import_id", import_id);
  206. item->set_tooltip_text(0, vformat(TTR("Import ID: %s"), import_id));
  207. item->set_selectable(0, true);
  208. if (p_tree == scene_tree) {
  209. mesh_data.scene_node = item;
  210. } else {
  211. mesh_data.mesh_node = item;
  212. }
  213. item->set_collapsed(true);
  214. for (int i = 0; i < p_mesh->get_surface_count(); i++) {
  215. Ref<Material> mat = p_mesh->surface_get_material(i);
  216. if (mat.is_valid()) {
  217. _fill_material(p_tree, mat, item);
  218. }
  219. }
  220. if (created) {
  221. _fill_mesh(mesh_tree, p_mesh, mesh_tree->get_root());
  222. }
  223. }
  224. void SceneImportSettings::_fill_animation(Tree *p_tree, const Ref<Animation> &p_anim, const String &p_name, TreeItem *p_parent) {
  225. if (!animation_map.has(p_name)) {
  226. AnimationData ad;
  227. ad.animation = p_anim;
  228. _load_default_subresource_settings(ad.settings, "animations", p_name, ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION);
  229. animation_map[p_name] = ad;
  230. }
  231. AnimationData &animation_data = animation_map[p_name];
  232. Ref<Texture2D> icon = get_theme_icon(SNAME("Animation"), SNAME("EditorIcons"));
  233. TreeItem *item = p_tree->create_item(p_parent);
  234. item->set_text(0, p_name);
  235. item->set_icon(0, icon);
  236. item->set_meta("type", "Animation");
  237. item->set_meta("import_id", p_name);
  238. item->set_selectable(0, true);
  239. animation_data.scene_node = item;
  240. }
  241. void SceneImportSettings::_fill_scene(Node *p_node, TreeItem *p_parent_item) {
  242. String import_id;
  243. if (p_node->has_meta("import_id")) {
  244. import_id = p_node->get_meta("import_id");
  245. } else {
  246. import_id = "PATH:" + String(scene->get_path_to(p_node));
  247. p_node->set_meta("import_id", import_id);
  248. }
  249. ImporterMeshInstance3D *src_mesh_node = Object::cast_to<ImporterMeshInstance3D>(p_node);
  250. if (src_mesh_node) {
  251. MeshInstance3D *mesh_node = memnew(MeshInstance3D);
  252. mesh_node->set_name(src_mesh_node->get_name());
  253. mesh_node->set_transform(src_mesh_node->get_transform());
  254. mesh_node->set_skin(src_mesh_node->get_skin());
  255. mesh_node->set_skeleton_path(src_mesh_node->get_skeleton_path());
  256. if (src_mesh_node->get_mesh().is_valid()) {
  257. Ref<ImporterMesh> editor_mesh = src_mesh_node->get_mesh();
  258. mesh_node->set_mesh(editor_mesh->get_mesh());
  259. }
  260. p_node->replace_by(mesh_node);
  261. memdelete(p_node);
  262. p_node = mesh_node;
  263. }
  264. String type = p_node->get_class();
  265. if (!has_theme_icon(type, SNAME("EditorIcons"))) {
  266. type = "Node3D";
  267. }
  268. Ref<Texture2D> icon = get_theme_icon(type, SNAME("EditorIcons"));
  269. TreeItem *item = scene_tree->create_item(p_parent_item);
  270. item->set_text(0, p_node->get_name());
  271. if (p_node == scene) {
  272. icon = get_theme_icon(SNAME("PackedScene"), SNAME("EditorIcons"));
  273. item->set_text(0, "Scene");
  274. }
  275. item->set_icon(0, icon);
  276. item->set_meta("type", "Node");
  277. item->set_meta("class", type);
  278. item->set_meta("import_id", import_id);
  279. item->set_tooltip_text(0, vformat(TTR("Type: %s\nImport ID: %s"), type, import_id));
  280. item->set_selectable(0, true);
  281. if (!node_map.has(import_id)) {
  282. NodeData nd;
  283. if (p_node != scene) {
  284. ResourceImporterScene::InternalImportCategory category;
  285. if (src_mesh_node) {
  286. category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE;
  287. } else if (Object::cast_to<AnimationPlayer>(p_node)) {
  288. category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE;
  289. } else if (Object::cast_to<Skeleton3D>(p_node)) {
  290. category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE;
  291. } else {
  292. category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_NODE;
  293. }
  294. _load_default_subresource_settings(nd.settings, "nodes", import_id, category);
  295. }
  296. node_map[import_id] = nd;
  297. }
  298. NodeData &node_data = node_map[import_id];
  299. node_data.node = p_node;
  300. node_data.scene_node = item;
  301. AnimationPlayer *anim_node = Object::cast_to<AnimationPlayer>(p_node);
  302. if (anim_node) {
  303. List<StringName> animations;
  304. anim_node->get_animation_list(&animations);
  305. for (const StringName &E : animations) {
  306. _fill_animation(scene_tree, anim_node->get_animation(E), E, item);
  307. }
  308. }
  309. for (int i = 0; i < p_node->get_child_count(); i++) {
  310. _fill_scene(p_node->get_child(i), item);
  311. }
  312. MeshInstance3D *mesh_node = Object::cast_to<MeshInstance3D>(p_node);
  313. if (mesh_node && mesh_node->get_mesh().is_valid()) {
  314. if (!editing_animation) {
  315. _fill_mesh(scene_tree, mesh_node->get_mesh(), item);
  316. }
  317. // Add the collider view.
  318. MeshInstance3D *collider_view = memnew(MeshInstance3D);
  319. collider_view->set_name("collider_view");
  320. collider_view->set_visible(false);
  321. mesh_node->add_child(collider_view, true);
  322. collider_view->set_owner(mesh_node);
  323. Transform3D accum_xform;
  324. Node3D *base = mesh_node;
  325. while (base) {
  326. accum_xform = base->get_transform() * accum_xform;
  327. base = Object::cast_to<Node3D>(base->get_parent());
  328. }
  329. AABB aabb = accum_xform.xform(mesh_node->get_mesh()->get_aabb());
  330. if (first_aabb) {
  331. contents_aabb = aabb;
  332. first_aabb = false;
  333. } else {
  334. contents_aabb.merge_with(aabb);
  335. }
  336. }
  337. }
  338. void SceneImportSettings::_update_scene() {
  339. scene_tree->clear();
  340. material_tree->clear();
  341. mesh_tree->clear();
  342. //hidden roots
  343. material_tree->create_item();
  344. mesh_tree->create_item();
  345. _fill_scene(scene, nullptr);
  346. }
  347. void SceneImportSettings::_update_view_gizmos() {
  348. if (!is_visible()) {
  349. return;
  350. }
  351. for (const KeyValue<String, NodeData> &e : node_map) {
  352. bool generate_collider = false;
  353. if (e.value.settings.has(SNAME("generate/physics"))) {
  354. generate_collider = e.value.settings[SNAME("generate/physics")];
  355. }
  356. MeshInstance3D *mesh_node = Object::cast_to<MeshInstance3D>(e.value.node);
  357. if (mesh_node == nullptr || mesh_node->get_mesh().is_null()) {
  358. // Nothing to do
  359. continue;
  360. }
  361. TypedArray<Node> descendants = mesh_node->find_children("collider_view", "MeshInstance3D");
  362. CRASH_COND_MSG(descendants.is_empty(), "This is unreachable, since the collider view is always created even when the collision is not used! If this is triggered there is a bug on the function `_fill_scene`.");
  363. MeshInstance3D *collider_view = static_cast<MeshInstance3D *>(descendants[0].operator Object *());
  364. collider_view->set_visible(generate_collider);
  365. if (generate_collider) {
  366. // This collider_view doesn't have a mesh so we need to generate a new one.
  367. // Generate the mesh collider.
  368. Vector<Ref<Shape3D>> shapes = ResourceImporterScene::get_collision_shapes(mesh_node->get_mesh(), e.value.settings);
  369. const Transform3D transform = ResourceImporterScene::get_collision_shapes_transform(e.value.settings);
  370. Ref<ArrayMesh> collider_view_mesh;
  371. collider_view_mesh.instantiate();
  372. for (Ref<Shape3D> shape : shapes) {
  373. Ref<ArrayMesh> debug_shape_mesh;
  374. if (shape.is_valid()) {
  375. debug_shape_mesh = shape->get_debug_mesh();
  376. }
  377. if (debug_shape_mesh.is_valid()) {
  378. collider_view_mesh->add_surface_from_arrays(
  379. debug_shape_mesh->surface_get_primitive_type(0),
  380. debug_shape_mesh->surface_get_arrays(0));
  381. collider_view_mesh->surface_set_material(
  382. collider_view_mesh->get_surface_count() - 1,
  383. collider_mat);
  384. }
  385. }
  386. collider_view->set_mesh(collider_view_mesh);
  387. collider_view->set_transform(transform);
  388. }
  389. }
  390. }
  391. void SceneImportSettings::_update_camera() {
  392. AABB camera_aabb;
  393. float rot_x = cam_rot_x;
  394. float rot_y = cam_rot_y;
  395. float zoom = cam_zoom;
  396. if (selected_type == "Node" || selected_type.is_empty()) {
  397. camera_aabb = contents_aabb;
  398. } else {
  399. if (mesh_preview->get_mesh().is_valid()) {
  400. camera_aabb = mesh_preview->get_transform().xform(mesh_preview->get_mesh()->get_aabb());
  401. } else {
  402. camera_aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  403. }
  404. if (selected_type == "Mesh" && mesh_map.has(selected_id)) {
  405. const MeshData &md = mesh_map[selected_id];
  406. rot_x = md.cam_rot_x;
  407. rot_y = md.cam_rot_y;
  408. zoom = md.cam_zoom;
  409. } else if (selected_type == "Material" && material_map.has(selected_id)) {
  410. const MaterialData &md = material_map[selected_id];
  411. rot_x = md.cam_rot_x;
  412. rot_y = md.cam_rot_y;
  413. zoom = md.cam_zoom;
  414. }
  415. }
  416. Vector3 center = camera_aabb.get_center();
  417. float camera_size = camera_aabb.get_longest_axis_size();
  418. camera->set_orthogonal(camera_size * zoom, 0.0001, camera_size * 2);
  419. Transform3D xf;
  420. xf.basis = Basis(Vector3(1, 0, 0), rot_x) * Basis(Vector3(0, 1, 0), rot_y);
  421. xf.origin = center;
  422. xf.translate_local(0, 0, camera_size);
  423. camera->set_transform(xf);
  424. }
  425. void SceneImportSettings::_load_default_subresource_settings(HashMap<StringName, Variant> &settings, const String &p_type, const String &p_import_id, ResourceImporterScene::InternalImportCategory p_category) {
  426. if (base_subresource_settings.has(p_type)) {
  427. Dictionary d = base_subresource_settings[p_type];
  428. if (d.has(p_import_id)) {
  429. d = d[p_import_id];
  430. List<ResourceImporterScene::ImportOption> options;
  431. if (editing_animation) {
  432. ResourceImporterScene::get_animation_singleton()->get_internal_import_options(p_category, &options);
  433. } else {
  434. ResourceImporterScene::get_scene_singleton()->get_internal_import_options(p_category, &options);
  435. }
  436. for (const ResourceImporterScene::ImportOption &E : options) {
  437. String key = E.option.name;
  438. if (d.has(key)) {
  439. settings[key] = d[key];
  440. }
  441. }
  442. }
  443. }
  444. }
  445. void SceneImportSettings::update_view() {
  446. update_view_timer->start();
  447. }
  448. void SceneImportSettings::open_settings(const String &p_path, bool p_for_animation) {
  449. if (scene) {
  450. memdelete(scene);
  451. scene = nullptr;
  452. }
  453. editing_animation = p_for_animation;
  454. scene_import_settings_data->settings = nullptr;
  455. scene_import_settings_data->path = p_path;
  456. // Visibility
  457. data_mode->set_tab_hidden(1, p_for_animation);
  458. data_mode->set_tab_hidden(2, p_for_animation);
  459. action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_EXTRACT_MATERIALS), p_for_animation);
  460. action_menu->get_popup()->set_item_disabled(action_menu->get_popup()->get_item_id(ACTION_CHOOSE_MESH_SAVE_PATHS), p_for_animation);
  461. base_path = p_path;
  462. material_set.clear();
  463. mesh_set.clear();
  464. material_map.clear();
  465. mesh_map.clear();
  466. node_map.clear();
  467. defaults.clear();
  468. mesh_preview->hide();
  469. selected_id = "";
  470. selected_type = "";
  471. cam_rot_x = -Math_PI / 4;
  472. cam_rot_y = -Math_PI / 4;
  473. cam_zoom = 1;
  474. {
  475. base_subresource_settings.clear();
  476. Ref<ConfigFile> config;
  477. config.instantiate();
  478. Error err = config->load(p_path + ".import");
  479. if (err == OK) {
  480. List<String> keys;
  481. config->get_section_keys("params", &keys);
  482. for (const String &E : keys) {
  483. Variant value = config->get_value("params", E);
  484. if (E == "_subresources") {
  485. base_subresource_settings = value;
  486. } else {
  487. defaults[E] = value;
  488. }
  489. }
  490. }
  491. }
  492. scene = ResourceImporterScene::get_scene_singleton()->pre_import(p_path, defaults); // Use the scene singleton here because we want to see the full thing.
  493. if (scene == nullptr) {
  494. EditorNode::get_singleton()->show_warning(TTR("Error opening scene"));
  495. return;
  496. }
  497. first_aabb = true;
  498. _update_scene();
  499. base_viewport->add_child(scene);
  500. inspector->edit(nullptr);
  501. if (first_aabb) {
  502. contents_aabb = AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  503. first_aabb = false;
  504. }
  505. popup_centered_ratio();
  506. _update_view_gizmos();
  507. _update_camera();
  508. // Start with the root item (Scene) selected.
  509. scene_tree->get_root()->select(0);
  510. if (p_for_animation) {
  511. set_title(vformat(TTR("Advanced Import Settings for AnimationLibrary '%s'"), base_path.get_file()));
  512. } else {
  513. set_title(vformat(TTR("Advanced Import Settings for Scene '%s'"), base_path.get_file()));
  514. }
  515. }
  516. SceneImportSettings *SceneImportSettings::singleton = nullptr;
  517. SceneImportSettings *SceneImportSettings::get_singleton() {
  518. return singleton;
  519. }
  520. Node *SceneImportSettings::get_selected_node() {
  521. if (selected_id == "") {
  522. return nullptr;
  523. }
  524. return node_map[selected_id].node;
  525. }
  526. void SceneImportSettings::_select(Tree *p_from, String p_type, String p_id) {
  527. selecting = true;
  528. scene_import_settings_data->hide_options = false;
  529. if (p_type == "Node") {
  530. node_selected->hide(); //always hide just in case
  531. mesh_preview->hide();
  532. if (Object::cast_to<Node3D>(scene)) {
  533. Object::cast_to<Node3D>(scene)->show();
  534. }
  535. //NodeData &nd=node_map[p_id];
  536. material_tree->deselect_all();
  537. mesh_tree->deselect_all();
  538. NodeData &nd = node_map[p_id];
  539. MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(nd.node);
  540. if (mi) {
  541. Ref<Mesh> base_mesh = mi->get_mesh();
  542. if (base_mesh.is_valid()) {
  543. AABB aabb = base_mesh->get_aabb();
  544. Transform3D aabb_xf;
  545. aabb_xf.basis.scale(aabb.size);
  546. aabb_xf.origin = aabb.position;
  547. aabb_xf = mi->get_global_transform() * aabb_xf;
  548. node_selected->set_transform(aabb_xf);
  549. node_selected->show();
  550. }
  551. }
  552. if (nd.node == scene) {
  553. scene_import_settings_data->settings = &defaults;
  554. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX;
  555. } else {
  556. scene_import_settings_data->settings = &nd.settings;
  557. if (mi) {
  558. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE;
  559. scene_import_settings_data->hide_options = editing_animation;
  560. } else if (Object::cast_to<AnimationPlayer>(nd.node)) {
  561. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE;
  562. } else if (Object::cast_to<Skeleton3D>(nd.node)) {
  563. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_SKELETON_3D_NODE;
  564. } else {
  565. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_NODE;
  566. scene_import_settings_data->hide_options = editing_animation;
  567. }
  568. }
  569. } else if (p_type == "Animation") {
  570. node_selected->hide(); //always hide just in case
  571. mesh_preview->hide();
  572. if (Object::cast_to<Node3D>(scene)) {
  573. Object::cast_to<Node3D>(scene)->show();
  574. }
  575. //NodeData &nd=node_map[p_id];
  576. material_tree->deselect_all();
  577. mesh_tree->deselect_all();
  578. AnimationData &ad = animation_map[p_id];
  579. scene_import_settings_data->settings = &ad.settings;
  580. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_ANIMATION;
  581. } else if (p_type == "Mesh") {
  582. node_selected->hide();
  583. if (Object::cast_to<Node3D>(scene)) {
  584. Object::cast_to<Node3D>(scene)->hide();
  585. }
  586. MeshData &md = mesh_map[p_id];
  587. if (p_from != mesh_tree) {
  588. md.mesh_node->uncollapse_tree();
  589. md.mesh_node->select(0);
  590. mesh_tree->ensure_cursor_is_visible();
  591. }
  592. if (p_from != scene_tree) {
  593. md.scene_node->uncollapse_tree();
  594. md.scene_node->select(0);
  595. scene_tree->ensure_cursor_is_visible();
  596. }
  597. mesh_preview->set_mesh(md.mesh);
  598. mesh_preview->show();
  599. material_tree->deselect_all();
  600. scene_import_settings_data->settings = &md.settings;
  601. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MESH;
  602. } else if (p_type == "Material") {
  603. node_selected->hide();
  604. if (Object::cast_to<Node3D>(scene)) {
  605. Object::cast_to<Node3D>(scene)->hide();
  606. }
  607. mesh_preview->show();
  608. MaterialData &md = material_map[p_id];
  609. material_preview->set_material(md.material);
  610. mesh_preview->set_mesh(material_preview);
  611. if (p_from != mesh_tree) {
  612. md.mesh_node->uncollapse_tree();
  613. md.mesh_node->select(0);
  614. mesh_tree->ensure_cursor_is_visible();
  615. }
  616. if (p_from != scene_tree) {
  617. md.scene_node->uncollapse_tree();
  618. md.scene_node->select(0);
  619. scene_tree->ensure_cursor_is_visible();
  620. }
  621. if (p_from != material_tree) {
  622. md.material_node->uncollapse_tree();
  623. md.material_node->select(0);
  624. material_tree->ensure_cursor_is_visible();
  625. }
  626. scene_import_settings_data->settings = &md.settings;
  627. scene_import_settings_data->category = ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MATERIAL;
  628. }
  629. selected_type = p_type;
  630. selected_id = p_id;
  631. selecting = false;
  632. _update_camera();
  633. List<ResourceImporter::ImportOption> options;
  634. if (editing_animation) {
  635. if (scene_import_settings_data->category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  636. ResourceImporterScene::get_animation_singleton()->get_import_options(base_path, &options);
  637. } else {
  638. ResourceImporterScene::get_animation_singleton()->get_internal_import_options(scene_import_settings_data->category, &options);
  639. }
  640. } else {
  641. if (scene_import_settings_data->category == ResourceImporterScene::INTERNAL_IMPORT_CATEGORY_MAX) {
  642. ResourceImporterScene::get_scene_singleton()->get_import_options(base_path, &options);
  643. } else {
  644. ResourceImporterScene::get_scene_singleton()->get_internal_import_options(scene_import_settings_data->category, &options);
  645. }
  646. }
  647. scene_import_settings_data->defaults.clear();
  648. scene_import_settings_data->current.clear();
  649. if (scene_import_settings_data->settings) {
  650. for (const ResourceImporter::ImportOption &E : options) {
  651. scene_import_settings_data->defaults[E.option.name] = E.default_value;
  652. //needed for visibility toggling (fails if something is missing)
  653. if (scene_import_settings_data->settings->has(E.option.name)) {
  654. scene_import_settings_data->current[E.option.name] = (*scene_import_settings_data->settings)[E.option.name];
  655. } else {
  656. scene_import_settings_data->current[E.option.name] = E.default_value;
  657. }
  658. }
  659. }
  660. scene_import_settings_data->options = options;
  661. inspector->edit(scene_import_settings_data);
  662. scene_import_settings_data->notify_property_list_changed();
  663. }
  664. void SceneImportSettings::_material_tree_selected() {
  665. if (selecting) {
  666. return;
  667. }
  668. TreeItem *item = material_tree->get_selected();
  669. String type = item->get_meta("type");
  670. String import_id = item->get_meta("import_id");
  671. _select(material_tree, type, import_id);
  672. }
  673. void SceneImportSettings::_mesh_tree_selected() {
  674. if (selecting) {
  675. return;
  676. }
  677. TreeItem *item = mesh_tree->get_selected();
  678. String type = item->get_meta("type");
  679. String import_id = item->get_meta("import_id");
  680. _select(mesh_tree, type, import_id);
  681. }
  682. void SceneImportSettings::_scene_tree_selected() {
  683. if (selecting) {
  684. return;
  685. }
  686. TreeItem *item = scene_tree->get_selected();
  687. String type = item->get_meta("type");
  688. String import_id = item->get_meta("import_id");
  689. _select(scene_tree, type, import_id);
  690. }
  691. void SceneImportSettings::_viewport_input(const Ref<InputEvent> &p_input) {
  692. float *rot_x = &cam_rot_x;
  693. float *rot_y = &cam_rot_y;
  694. float *zoom = &cam_zoom;
  695. if (selected_type == "Mesh" && mesh_map.has(selected_id)) {
  696. MeshData &md = mesh_map[selected_id];
  697. rot_x = &md.cam_rot_x;
  698. rot_y = &md.cam_rot_y;
  699. zoom = &md.cam_zoom;
  700. } else if (selected_type == "Material" && material_map.has(selected_id)) {
  701. MaterialData &md = material_map[selected_id];
  702. rot_x = &md.cam_rot_x;
  703. rot_y = &md.cam_rot_y;
  704. zoom = &md.cam_zoom;
  705. }
  706. Ref<InputEventMouseMotion> mm = p_input;
  707. if (mm.is_valid() && (mm->get_button_mask() & MouseButton::MASK_LEFT) != MouseButton::NONE) {
  708. (*rot_x) -= mm->get_relative().y * 0.01 * EDSCALE;
  709. (*rot_y) -= mm->get_relative().x * 0.01 * EDSCALE;
  710. (*rot_x) = CLAMP((*rot_x), -Math_PI / 2, Math_PI / 2);
  711. _update_camera();
  712. }
  713. Ref<InputEventMouseButton> mb = p_input;
  714. if (mb.is_valid() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  715. (*zoom) *= 1.1;
  716. if ((*zoom) > 10.0) {
  717. (*zoom) = 10.0;
  718. }
  719. _update_camera();
  720. }
  721. if (mb.is_valid() && mb->get_button_index() == MouseButton::WHEEL_UP) {
  722. (*zoom) /= 1.1;
  723. if ((*zoom) < 0.1) {
  724. (*zoom) = 0.1;
  725. }
  726. _update_camera();
  727. }
  728. }
  729. void SceneImportSettings::_re_import() {
  730. HashMap<StringName, Variant> main_settings;
  731. main_settings = defaults;
  732. main_settings.erase("_subresources");
  733. Dictionary nodes;
  734. Dictionary materials;
  735. Dictionary meshes;
  736. Dictionary animations;
  737. Dictionary subresources;
  738. for (KeyValue<String, NodeData> &E : node_map) {
  739. if (E.value.settings.size()) {
  740. Dictionary d;
  741. for (const KeyValue<StringName, Variant> &F : E.value.settings) {
  742. d[String(F.key)] = F.value;
  743. }
  744. nodes[E.key] = d;
  745. }
  746. }
  747. if (nodes.size()) {
  748. subresources["nodes"] = nodes;
  749. }
  750. for (KeyValue<String, MaterialData> &E : material_map) {
  751. if (E.value.settings.size()) {
  752. Dictionary d;
  753. for (const KeyValue<StringName, Variant> &F : E.value.settings) {
  754. d[String(F.key)] = F.value;
  755. }
  756. materials[E.key] = d;
  757. }
  758. }
  759. if (materials.size()) {
  760. subresources["materials"] = materials;
  761. }
  762. for (KeyValue<String, MeshData> &E : mesh_map) {
  763. if (E.value.settings.size()) {
  764. Dictionary d;
  765. for (const KeyValue<StringName, Variant> &F : E.value.settings) {
  766. d[String(F.key)] = F.value;
  767. }
  768. meshes[E.key] = d;
  769. }
  770. }
  771. if (meshes.size()) {
  772. subresources["meshes"] = meshes;
  773. }
  774. for (KeyValue<String, AnimationData> &E : animation_map) {
  775. if (E.value.settings.size()) {
  776. Dictionary d;
  777. for (const KeyValue<StringName, Variant> &F : E.value.settings) {
  778. d[String(F.key)] = F.value;
  779. }
  780. animations[E.key] = d;
  781. }
  782. }
  783. if (animations.size()) {
  784. subresources["animations"] = animations;
  785. }
  786. if (subresources.size()) {
  787. main_settings["_subresources"] = subresources;
  788. }
  789. EditorFileSystem::get_singleton()->reimport_file_with_custom_parameters(base_path, editing_animation ? "animation_library" : "scene", main_settings);
  790. }
  791. void SceneImportSettings::_notification(int p_what) {
  792. switch (p_what) {
  793. case NOTIFICATION_READY: {
  794. connect("confirmed", callable_mp(this, &SceneImportSettings::_re_import));
  795. } break;
  796. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  797. inspector->set_property_name_style(EditorPropertyNameProcessor::get_settings_style());
  798. } break;
  799. }
  800. }
  801. void SceneImportSettings::_menu_callback(int p_id) {
  802. switch (p_id) {
  803. case ACTION_EXTRACT_MATERIALS: {
  804. save_path->set_text(TTR("Select folder to extract material resources"));
  805. external_extension_type->select(0);
  806. } break;
  807. case ACTION_CHOOSE_MESH_SAVE_PATHS: {
  808. save_path->set_text(TTR("Select folder where mesh resources will save on import"));
  809. external_extension_type->select(1);
  810. } break;
  811. case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
  812. save_path->set_text(TTR("Select folder where animations will save on import"));
  813. external_extension_type->select(1);
  814. } break;
  815. }
  816. save_path->set_current_dir(base_path.get_base_dir());
  817. current_action = p_id;
  818. save_path->popup_centered_ratio();
  819. }
  820. void SceneImportSettings::_save_path_changed(const String &p_path) {
  821. save_path_item->set_text(1, p_path);
  822. if (FileAccess::exists(p_path)) {
  823. save_path_item->set_text(2, "Warning: File exists");
  824. save_path_item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced."));
  825. save_path_item->set_icon(2, get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons")));
  826. } else {
  827. save_path_item->set_text(2, "Will create new File");
  828. save_path_item->set_icon(2, get_theme_icon(SNAME("StatusSuccess"), SNAME("EditorIcons")));
  829. }
  830. }
  831. void SceneImportSettings::_browse_save_callback(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  832. if (p_button != MouseButton::LEFT) {
  833. return;
  834. }
  835. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  836. String path = item->get_text(1);
  837. item_save_path->set_current_file(path);
  838. save_path_item = item;
  839. item_save_path->popup_centered_ratio();
  840. }
  841. void SceneImportSettings::_save_dir_callback(const String &p_path) {
  842. external_path_tree->clear();
  843. TreeItem *root = external_path_tree->create_item();
  844. save_path_items.clear();
  845. switch (current_action) {
  846. case ACTION_EXTRACT_MATERIALS: {
  847. for (const KeyValue<String, MaterialData> &E : material_map) {
  848. MaterialData &md = material_map[E.key];
  849. TreeItem *item = external_path_tree->create_item(root);
  850. String name = md.material_node->get_text(0);
  851. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  852. item->set_icon(0, get_theme_icon(SNAME("StandardMaterial3D"), SNAME("EditorIcons")));
  853. item->set_text(0, name);
  854. if (md.has_import_id) {
  855. if (md.settings.has("use_external/enabled") && bool(md.settings["use_external/enabled"])) {
  856. item->set_text(2, "Already External");
  857. item->set_tooltip_text(2, TTR("This material already references an external file, no action will be taken.\nDisable the external property for it to be extracted again."));
  858. } else {
  859. item->set_metadata(0, E.key);
  860. item->set_editable(0, true);
  861. item->set_checked(0, true);
  862. String path = p_path.path_join(name);
  863. if (external_extension_type->get_selected() == 0) {
  864. path += ".tres";
  865. } else {
  866. path += ".res";
  867. }
  868. item->set_text(1, path);
  869. if (FileAccess::exists(path)) {
  870. item->set_text(2, "Warning: File exists");
  871. item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced."));
  872. item->set_icon(2, get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons")));
  873. } else {
  874. item->set_text(2, "Will create new File");
  875. item->set_icon(2, get_theme_icon(SNAME("StatusSuccess"), SNAME("EditorIcons")));
  876. }
  877. item->add_button(1, get_theme_icon(SNAME("Folder"), SNAME("EditorIcons")));
  878. }
  879. } else {
  880. item->set_text(2, "No import ID");
  881. item->set_tooltip_text(2, TTR("Material has no name nor any other way to identify on re-import.\nPlease name it or ensure it is exported with an unique ID."));
  882. item->set_icon(2, get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons")));
  883. }
  884. save_path_items.push_back(item);
  885. }
  886. external_paths->set_title(TTR("Extract Materials to Resource Files"));
  887. external_paths->set_ok_button_text(TTR("Extract"));
  888. } break;
  889. case ACTION_CHOOSE_MESH_SAVE_PATHS: {
  890. for (const KeyValue<String, MeshData> &E : mesh_map) {
  891. MeshData &md = mesh_map[E.key];
  892. TreeItem *item = external_path_tree->create_item(root);
  893. String name = md.mesh_node->get_text(0);
  894. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  895. item->set_icon(0, get_theme_icon(SNAME("Mesh"), SNAME("EditorIcons")));
  896. item->set_text(0, name);
  897. if (md.has_import_id) {
  898. if (md.settings.has("save_to_file/enabled") && bool(md.settings["save_to_file/enabled"])) {
  899. item->set_text(2, "Already Saving");
  900. item->set_tooltip_text(2, TTR("This mesh already saves to an external resource, no action will be taken."));
  901. } else {
  902. item->set_metadata(0, E.key);
  903. item->set_editable(0, true);
  904. item->set_checked(0, true);
  905. String path = p_path.path_join(name);
  906. if (external_extension_type->get_selected() == 0) {
  907. path += ".tres";
  908. } else {
  909. path += ".res";
  910. }
  911. item->set_text(1, path);
  912. if (FileAccess::exists(path)) {
  913. item->set_text(2, "Warning: File exists");
  914. item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced on import."));
  915. item->set_icon(2, get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons")));
  916. } else {
  917. item->set_text(2, "Will save to new File");
  918. item->set_icon(2, get_theme_icon(SNAME("StatusSuccess"), SNAME("EditorIcons")));
  919. }
  920. item->add_button(1, get_theme_icon(SNAME("Folder"), SNAME("EditorIcons")));
  921. }
  922. } else {
  923. item->set_text(2, "No import ID");
  924. item->set_tooltip_text(2, TTR("Mesh has no name nor any other way to identify on re-import.\nPlease name it or ensure it is exported with an unique ID."));
  925. item->set_icon(2, get_theme_icon(SNAME("StatusError"), SNAME("EditorIcons")));
  926. }
  927. save_path_items.push_back(item);
  928. }
  929. external_paths->set_title(TTR("Set paths to save meshes as resource files on Reimport"));
  930. external_paths->set_ok_button_text(TTR("Set Paths"));
  931. } break;
  932. case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
  933. for (const KeyValue<String, AnimationData> &E : animation_map) {
  934. AnimationData &ad = animation_map[E.key];
  935. TreeItem *item = external_path_tree->create_item(root);
  936. String name = ad.scene_node->get_text(0);
  937. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  938. item->set_icon(0, get_theme_icon(SNAME("Animation"), SNAME("EditorIcons")));
  939. item->set_text(0, name);
  940. if (ad.settings.has("save_to_file/enabled") && bool(ad.settings["save_to_file/enabled"])) {
  941. item->set_text(2, "Already Saving");
  942. item->set_tooltip_text(2, TTR("This animation already saves to an external resource, no action will be taken."));
  943. } else {
  944. item->set_metadata(0, E.key);
  945. item->set_editable(0, true);
  946. item->set_checked(0, true);
  947. String path = p_path.path_join(name);
  948. if (external_extension_type->get_selected() == 0) {
  949. path += ".tres";
  950. } else {
  951. path += ".res";
  952. }
  953. item->set_text(1, path);
  954. if (FileAccess::exists(path)) {
  955. item->set_text(2, "Warning: File exists");
  956. item->set_tooltip_text(2, TTR("Existing file with the same name will be replaced on import."));
  957. item->set_icon(2, get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons")));
  958. } else {
  959. item->set_text(2, "Will save to new File");
  960. item->set_icon(2, get_theme_icon(SNAME("StatusSuccess"), SNAME("EditorIcons")));
  961. }
  962. item->add_button(1, get_theme_icon(SNAME("Folder"), SNAME("EditorIcons")));
  963. }
  964. save_path_items.push_back(item);
  965. }
  966. external_paths->set_title(TTR("Set paths to save animations as resource files on Reimport"));
  967. external_paths->set_ok_button_text(TTR("Set Paths"));
  968. } break;
  969. }
  970. external_paths->popup_centered_ratio();
  971. }
  972. void SceneImportSettings::_save_dir_confirm() {
  973. for (int i = 0; i < save_path_items.size(); i++) {
  974. TreeItem *item = save_path_items[i];
  975. if (!item->is_checked(0)) {
  976. continue; //ignore
  977. }
  978. String path = item->get_text(1);
  979. if (!path.is_resource_file()) {
  980. continue;
  981. }
  982. String id = item->get_metadata(0);
  983. switch (current_action) {
  984. case ACTION_EXTRACT_MATERIALS: {
  985. ERR_CONTINUE(!material_map.has(id));
  986. MaterialData &md = material_map[id];
  987. Error err = ResourceSaver::save(md.material, path);
  988. if (err != OK) {
  989. EditorNode::get_singleton()->add_io_error(TTR("Can't make material external to file, write error:") + "\n\t" + path);
  990. continue;
  991. }
  992. md.settings["use_external/enabled"] = true;
  993. md.settings["use_external/path"] = path;
  994. } break;
  995. case ACTION_CHOOSE_MESH_SAVE_PATHS: {
  996. ERR_CONTINUE(!mesh_map.has(id));
  997. MeshData &md = mesh_map[id];
  998. md.settings["save_to_file/enabled"] = true;
  999. md.settings["save_to_file/path"] = path;
  1000. } break;
  1001. case ACTION_CHOOSE_ANIMATION_SAVE_PATHS: {
  1002. ERR_CONTINUE(!animation_map.has(id));
  1003. AnimationData &ad = animation_map[id];
  1004. ad.settings["save_to_file/enabled"] = true;
  1005. ad.settings["save_to_file/path"] = path;
  1006. } break;
  1007. }
  1008. }
  1009. if (current_action == ACTION_EXTRACT_MATERIALS) {
  1010. //as this happens right now, the scene needs to be saved and reimported.
  1011. _re_import();
  1012. open_settings(base_path);
  1013. } else {
  1014. scene_import_settings_data->notify_property_list_changed();
  1015. }
  1016. }
  1017. SceneImportSettings::SceneImportSettings() {
  1018. singleton = this;
  1019. VBoxContainer *main_vb = memnew(VBoxContainer);
  1020. add_child(main_vb);
  1021. HBoxContainer *menu_hb = memnew(HBoxContainer);
  1022. main_vb->add_child(menu_hb);
  1023. action_menu = memnew(MenuButton);
  1024. action_menu->set_text(TTR("Actions..."));
  1025. menu_hb->add_child(action_menu);
  1026. action_menu->get_popup()->add_item(TTR("Extract Materials"), ACTION_EXTRACT_MATERIALS);
  1027. action_menu->get_popup()->add_separator();
  1028. action_menu->get_popup()->add_item(TTR("Set Animation Save Paths"), ACTION_CHOOSE_ANIMATION_SAVE_PATHS);
  1029. action_menu->get_popup()->add_item(TTR("Set Mesh Save Paths"), ACTION_CHOOSE_MESH_SAVE_PATHS);
  1030. action_menu->get_popup()->connect("id_pressed", callable_mp(this, &SceneImportSettings::_menu_callback));
  1031. tree_split = memnew(HSplitContainer);
  1032. main_vb->add_child(tree_split);
  1033. tree_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1034. data_mode = memnew(TabContainer);
  1035. tree_split->add_child(data_mode);
  1036. data_mode->set_custom_minimum_size(Size2(300 * EDSCALE, 0));
  1037. data_mode->set_theme_type_variation("TabContainerOdd");
  1038. property_split = memnew(HSplitContainer);
  1039. tree_split->add_child(property_split);
  1040. property_split->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1041. scene_tree = memnew(Tree);
  1042. scene_tree->set_name(TTR("Scene"));
  1043. data_mode->add_child(scene_tree);
  1044. scene_tree->connect("cell_selected", callable_mp(this, &SceneImportSettings::_scene_tree_selected));
  1045. mesh_tree = memnew(Tree);
  1046. mesh_tree->set_name(TTR("Meshes"));
  1047. data_mode->add_child(mesh_tree);
  1048. mesh_tree->set_hide_root(true);
  1049. mesh_tree->connect("cell_selected", callable_mp(this, &SceneImportSettings::_mesh_tree_selected));
  1050. material_tree = memnew(Tree);
  1051. material_tree->set_name(TTR("Materials"));
  1052. data_mode->add_child(material_tree);
  1053. material_tree->connect("cell_selected", callable_mp(this, &SceneImportSettings::_material_tree_selected));
  1054. material_tree->set_hide_root(true);
  1055. SubViewportContainer *vp_container = memnew(SubViewportContainer);
  1056. vp_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1057. vp_container->set_custom_minimum_size(Size2(10, 10));
  1058. vp_container->set_stretch(true);
  1059. vp_container->connect("gui_input", callable_mp(this, &SceneImportSettings::_viewport_input));
  1060. property_split->add_child(vp_container);
  1061. base_viewport = memnew(SubViewport);
  1062. vp_container->add_child(base_viewport);
  1063. base_viewport->set_use_own_world_3d(true);
  1064. camera = memnew(Camera3D);
  1065. base_viewport->add_child(camera);
  1066. camera->make_current();
  1067. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  1068. camera_attributes.instantiate();
  1069. camera->set_attributes(camera_attributes);
  1070. }
  1071. light = memnew(DirectionalLight3D);
  1072. light->set_transform(Transform3D().looking_at(Vector3(-1, -2, -0.6), Vector3(0, 1, 0)));
  1073. base_viewport->add_child(light);
  1074. light->set_shadow(true);
  1075. {
  1076. Ref<StandardMaterial3D> selection_mat;
  1077. selection_mat.instantiate();
  1078. selection_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1079. selection_mat->set_albedo(Color(1, 0.8, 1.0));
  1080. Ref<SurfaceTool> st;
  1081. st.instantiate();
  1082. st->begin(Mesh::PRIMITIVE_LINES);
  1083. AABB base_aabb;
  1084. base_aabb.size = Vector3(1, 1, 1);
  1085. for (int i = 0; i < 12; i++) {
  1086. Vector3 a, b;
  1087. base_aabb.get_edge(i, a, b);
  1088. st->add_vertex(a);
  1089. st->add_vertex(a.lerp(b, 0.2));
  1090. st->add_vertex(b);
  1091. st->add_vertex(b.lerp(a, 0.2));
  1092. }
  1093. selection_mesh.instantiate();
  1094. st->commit(selection_mesh);
  1095. selection_mesh->surface_set_material(0, selection_mat);
  1096. node_selected = memnew(MeshInstance3D);
  1097. node_selected->set_mesh(selection_mesh);
  1098. base_viewport->add_child(node_selected);
  1099. node_selected->hide();
  1100. }
  1101. {
  1102. mesh_preview = memnew(MeshInstance3D);
  1103. base_viewport->add_child(mesh_preview);
  1104. mesh_preview->hide();
  1105. material_preview.instantiate();
  1106. }
  1107. {
  1108. collider_mat.instantiate();
  1109. collider_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1110. collider_mat->set_albedo(Color(0.5, 0.5, 1.0));
  1111. }
  1112. inspector = memnew(EditorInspector);
  1113. inspector->set_custom_minimum_size(Size2(300 * EDSCALE, 0));
  1114. inspector->set_property_name_style(EditorPropertyNameProcessor::get_settings_style());
  1115. property_split->add_child(inspector);
  1116. scene_import_settings_data = memnew(SceneImportSettingsData);
  1117. set_ok_button_text(TTR("Reimport"));
  1118. set_cancel_button_text(TTR("Close"));
  1119. external_paths = memnew(ConfirmationDialog);
  1120. add_child(external_paths);
  1121. external_path_tree = memnew(Tree);
  1122. external_paths->add_child(external_path_tree);
  1123. external_path_tree->connect("button_clicked", callable_mp(this, &SceneImportSettings::_browse_save_callback));
  1124. external_paths->connect("confirmed", callable_mp(this, &SceneImportSettings::_save_dir_confirm));
  1125. external_path_tree->set_columns(3);
  1126. external_path_tree->set_column_titles_visible(true);
  1127. external_path_tree->set_column_expand(0, true);
  1128. external_path_tree->set_column_custom_minimum_width(0, 100 * EDSCALE);
  1129. external_path_tree->set_column_title(0, TTR("Resource"));
  1130. external_path_tree->set_column_expand(1, true);
  1131. external_path_tree->set_column_custom_minimum_width(1, 100 * EDSCALE);
  1132. external_path_tree->set_column_title(1, TTR("Path"));
  1133. external_path_tree->set_column_expand(2, false);
  1134. external_path_tree->set_column_custom_minimum_width(2, 200 * EDSCALE);
  1135. external_path_tree->set_column_title(2, TTR("Status"));
  1136. save_path = memnew(EditorFileDialog);
  1137. save_path->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_DIR);
  1138. HBoxContainer *extension_hb = memnew(HBoxContainer);
  1139. save_path->get_vbox()->add_child(extension_hb);
  1140. extension_hb->add_spacer();
  1141. extension_hb->add_child(memnew(Label(TTR("Save Extension:"))));
  1142. external_extension_type = memnew(OptionButton);
  1143. extension_hb->add_child(external_extension_type);
  1144. external_extension_type->add_item(TTR("Text: *.tres"));
  1145. external_extension_type->add_item(TTR("Binary: *.res"));
  1146. external_path_tree->set_hide_root(true);
  1147. add_child(save_path);
  1148. item_save_path = memnew(EditorFileDialog);
  1149. item_save_path->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  1150. item_save_path->add_filter("*.tres", TTR("Text Resource"));
  1151. item_save_path->add_filter("*.res", TTR("Binary Resource"));
  1152. add_child(item_save_path);
  1153. item_save_path->connect("file_selected", callable_mp(this, &SceneImportSettings::_save_path_changed));
  1154. save_path->connect("dir_selected", callable_mp(this, &SceneImportSettings::_save_dir_callback));
  1155. update_view_timer = memnew(Timer);
  1156. update_view_timer->set_wait_time(0.2);
  1157. update_view_timer->connect("timeout", callable_mp(this, &SceneImportSettings::_update_view_gizmos));
  1158. add_child(update_view_timer);
  1159. }
  1160. SceneImportSettings::~SceneImportSettings() {
  1161. memdelete(scene_import_settings_data);
  1162. }