diff options
author | Juan Linietsky <juan@godotengine.org> | 2021-03-19 09:57:52 -0300 |
---|---|---|
committer | Juan Linietsky <juan@godotengine.org> | 2021-03-22 12:16:40 -0300 |
commit | 97a3a662205d598dad195fa72d2dcb2f19c21088 (patch) | |
tree | 7d35031e64f59f1a6d4a66aaac9492cdffbefe47 /editor/import/resource_importer_scene.cpp | |
parent | 07f076fa4f2896415993bb8e3fb42128423de0d2 (diff) |
Improved 3D Scene Importer
* Added option for importers to show an Advanced settings dialog
* Created advanced settings dialog for Scene Importer
* Cleaned up importers (remove many old/unused options)
* Added the ability to customize every node, material, mesh and animation individually
* Saving to animations and meshes to files is now a manual process, making it more predictable
* Added the ability for materials to be replaced by external files (or to be made external, up to you).
* When doubleclicking an impoted scene in the filesystem dock, it automatically shows the import settings instead of asking to open it.
WARNING: Lightmap UV unwrap is not working, it needs to be re-made.
Diffstat (limited to 'editor/import/resource_importer_scene.cpp')
-rw-r--r-- | editor/import/resource_importer_scene.cpp | 1239 |
1 files changed, 641 insertions, 598 deletions
diff --git a/editor/import/resource_importer_scene.cpp b/editor/import/resource_importer_scene.cpp index 14ecccc13e..dba6ee3fac 100644 --- a/editor/import/resource_importer_scene.cpp +++ b/editor/import/resource_importer_scene.cpp @@ -32,7 +32,9 @@ #include "core/io/resource_saver.h" #include "editor/editor_node.h" +#include "editor/import/scene_import_settings.h" #include "editor/import/scene_importer_mesh_node_3d.h" +#include "scene/3d/area_3d.h" #include "scene/3d/collision_shape_3d.h" #include "scene/3d/mesh_instance_3d.h" #include "scene/3d/navigation_region_3d.h" @@ -111,20 +113,14 @@ void EditorSceneImporter::_bind_methods() { BIND_CONSTANT(IMPORT_SCENE); BIND_CONSTANT(IMPORT_ANIMATION); - BIND_CONSTANT(IMPORT_ANIMATION_DETECT_LOOP); - BIND_CONSTANT(IMPORT_ANIMATION_OPTIMIZE); - BIND_CONSTANT(IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS); - BIND_CONSTANT(IMPORT_ANIMATION_KEEP_VALUE_TRACKS); - BIND_CONSTANT(IMPORT_GENERATE_TANGENT_ARRAYS); BIND_CONSTANT(IMPORT_FAIL_ON_MISSING_DEPENDENCIES); - BIND_CONSTANT(IMPORT_MATERIALS_IN_INSTANCES); - BIND_CONSTANT(IMPORT_USE_COMPRESSION); + BIND_CONSTANT(IMPORT_GENERATE_TANGENT_ARRAYS); + BIND_CONSTANT(IMPORT_USE_NAMED_SKIN_BINDS); } ///////////////////////////////// void EditorScenePostImport::_bind_methods() { BIND_VMETHOD(MethodInfo(Variant::OBJECT, "post_import", PropertyInfo(Variant::OBJECT, "scene"))); - ClassDB::bind_method(D_METHOD("get_source_folder"), &EditorScenePostImport::get_source_folder); ClassDB::bind_method(D_METHOD("get_source_file"), &EditorScenePostImport::get_source_file); } @@ -136,16 +132,11 @@ Node *EditorScenePostImport::post_import(Node *p_scene) { return p_scene; } -String EditorScenePostImport::get_source_folder() const { - return source_folder; -} - String EditorScenePostImport::get_source_file() const { return source_file; } -void EditorScenePostImport::init(const String &p_source_folder, const String &p_source_file) { - source_folder = p_source_folder; +void EditorScenePostImport::init(const String &p_source_file) { source_file = p_source_file; } @@ -183,29 +174,9 @@ bool ResourceImporterScene::get_option_visibility(const String &p_option, const if (p_option != "animation/import" && !bool(p_options["animation/import"])) { return false; } - - if (p_option == "animation/keep_custom_tracks" && int(p_options["animation/storage"]) == 0) { - return false; - } - - if (p_option.begins_with("animation/optimizer/") && p_option != "animation/optimizer/enabled" && !bool(p_options["animation/optimizer/enabled"])) { - return false; - } - - if (p_option.begins_with("animation/clip_")) { - int max_clip = p_options["animation/clips/amount"]; - int clip = p_option.get_slice("/", 1).get_slice("_", 1).to_int() - 1; - if (clip >= max_clip) { - return false; - } - } - } - - if (p_option == "materials/keep_on_reimport" && int(p_options["materials/storage"]) == 0) { - return false; } - if (p_option == "meshes/lightmap_texel_size" && int(p_options["meshes/light_baking"]) < 2) { + if (p_option == "meshes/lightmap_texel_size" && int(p_options["meshes/light_baking"]) < 3) { return false; } @@ -213,34 +184,11 @@ bool ResourceImporterScene::get_option_visibility(const String &p_option, const } int ResourceImporterScene::get_preset_count() const { - return PRESET_MAX; + return 0; } String ResourceImporterScene::get_preset_name(int p_idx) const { - switch (p_idx) { - case PRESET_SINGLE_SCENE: - return TTR("Import as Single Scene"); - case PRESET_SEPARATE_ANIMATIONS: - return TTR("Import with Separate Animations"); - case PRESET_SEPARATE_MATERIALS: - return TTR("Import with Separate Materials"); - case PRESET_SEPARATE_MESHES: - return TTR("Import with Separate Objects"); - case PRESET_SEPARATE_MESHES_AND_MATERIALS: - return TTR("Import with Separate Objects+Materials"); - case PRESET_SEPARATE_MESHES_AND_ANIMATIONS: - return TTR("Import with Separate Objects+Animations"); - case PRESET_SEPARATE_MATERIALS_AND_ANIMATIONS: - return TTR("Import with Separate Materials+Animations"); - case PRESET_SEPARATE_MESHES_MATERIALS_AND_ANIMATIONS: - return TTR("Import with Separate Objects+Materials+Animations"); - case PRESET_MULTIPLE_SCENES: - return TTR("Import as Multiple Scenes"); - case PRESET_MULTIPLE_SCENES_AND_MATERIALS: - return TTR("Import as Multiple Scenes+Materials"); - } - - return ""; + return String(); } static bool _teststr(const String &p_what, const String &p_str) { @@ -299,10 +247,24 @@ static void _gen_shape_list(const Ref<Mesh> &mesh, List<Ref<Shape3D>> &r_shape_l } } -Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh>, List<Ref<Shape3D>>> &collision_map, LightBakeMode p_light_bake_mode) { +static void _pre_gen_shape_list(const Ref<EditorSceneImporterMesh> &mesh, List<Ref<Shape3D>> &r_shape_list, bool p_convex) { + if (!p_convex) { + Ref<Shape3D> shape = mesh->create_trimesh_shape(); + r_shape_list.push_back(shape); + } else { + Vector<Ref<Shape3D>> cd = mesh->convex_decompose(); + if (cd.size()) { + for (int i = 0; i < cd.size(); i++) { + r_shape_list.push_back(cd[i]); + } + } + } +} + +Node *ResourceImporterScene::_pre_fix_node(Node *p_node, Node *p_root, Map<Ref<EditorSceneImporterMesh>, List<Ref<Shape3D>>> &collision_map) { // children first for (int i = 0; i < p_node->get_child_count(); i++) { - Node *r = _fix_node(p_node->get_child(i), p_root, collision_map, p_light_bake_mode); + Node *r = _pre_fix_node(p_node->get_child(i), p_root, collision_map); if (!r) { i--; //was erased } @@ -317,33 +279,29 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> return nullptr; } - if (Object::cast_to<MeshInstance3D>(p_node)) { - MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_node); + if (Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { + EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); - Ref<ArrayMesh> m = mi->get_mesh(); + Ref<EditorSceneImporterMesh> m = mi->get_mesh(); if (m.is_valid()) { for (int i = 0; i < m->get_surface_count(); i++) { - Ref<StandardMaterial3D> mat = m->surface_get_material(i); + Ref<BaseMaterial3D> mat = m->get_surface_material(i); if (!mat.is_valid()) { continue; } if (_teststr(mat->get_name(), "alpha")) { - mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); + mat->set_transparency(BaseMaterial3D::TRANSPARENCY_ALPHA); mat->set_name(_fixstr(mat->get_name(), "alpha")); } if (_teststr(mat->get_name(), "vcol")) { - mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); - mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); + mat->set_flag(BaseMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true); + mat->set_flag(BaseMaterial3D::FLAG_SRGB_VERTEX_COLOR, true); mat->set_name(_fixstr(mat->get_name(), "vcol")); } } } - - if (p_light_bake_mode != LIGHT_BAKE_DISABLED) { - mi->set_gi_mode(GeometryInstance3D::GI_MODE_BAKED); - } } if (Object::cast_to<AnimationPlayer>(p_node)) { @@ -367,6 +325,17 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> } } } + + String animname = E->get(); + const int loop_string_count = 3; + static const char *loop_strings[loop_string_count] = { "loops", "loop", "cycle" }; + for (int i = 0; i < loop_string_count; i++) { + if (_teststr(animname, loop_strings[i])) { + anim->set_loop(true); + animname = _fixstr(animname, loop_strings[i]); + ap->rename_animation(E->get(), animname); + } + } } } @@ -374,9 +343,9 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> if (isroot) { return p_node; } - MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_node); + EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); if (mi) { - Ref<Mesh> mesh = mi->get_mesh(); + Ref<EditorSceneImporterMesh> mesh = mi->get_mesh(); if (mesh.is_valid()) { List<Ref<Shape3D>> shapes; @@ -384,10 +353,10 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> if (collision_map.has(mesh)) { shapes = collision_map[mesh]; } else if (_teststr(name, "colonly")) { - _gen_shape_list(mesh, shapes, false); + _pre_gen_shape_list(mesh, shapes, false); collision_map[mesh] = shapes; } else if (_teststr(name, "convcolonly")) { - _gen_shape_list(mesh, shapes, true); + _pre_gen_shape_list(mesh, shapes, true); collision_map[mesh] = shapes; } @@ -454,13 +423,13 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> colshape->set_owner(sb->get_owner()); } - } else if (_teststr(name, "rigid") && Object::cast_to<MeshInstance3D>(p_node)) { + } else if (_teststr(name, "rigid") && Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { if (isroot) { return p_node; } - MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_node); - Ref<Mesh> mesh = mi->get_mesh(); + EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + Ref<EditorSceneImporterMesh> mesh = mi->get_mesh(); if (mesh.is_valid()) { List<Ref<Shape3D>> shapes; @@ -492,10 +461,10 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> } } - } else if ((_teststr(name, "col") || (_teststr(name, "convcol"))) && Object::cast_to<MeshInstance3D>(p_node)) { - MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_node); + } else if ((_teststr(name, "col") || (_teststr(name, "convcol"))) && Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { + EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); - Ref<Mesh> mesh = mi->get_mesh(); + Ref<EditorSceneImporterMesh> mesh = mi->get_mesh(); if (mesh.is_valid()) { List<Ref<Shape3D>> shapes; @@ -542,71 +511,31 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> } } - } else if (_teststr(name, "navmesh") && Object::cast_to<MeshInstance3D>(p_node)) { + } else if (_teststr(name, "navmesh") && Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { if (isroot) { return p_node; } - MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_node); + EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); - Ref<ArrayMesh> mesh = mi->get_mesh(); + Ref<EditorSceneImporterMesh> mesh = mi->get_mesh(); ERR_FAIL_COND_V(mesh.is_null(), nullptr); NavigationRegion3D *nmi = memnew(NavigationRegion3D); nmi->set_name(_fixstr(name, "navmesh")); - Ref<NavigationMesh> nmesh = memnew(NavigationMesh); - nmesh->create_from_mesh(mesh); + Ref<NavigationMesh> nmesh = mesh->create_navigation_mesh(); nmi->set_navigation_mesh(nmesh); Object::cast_to<Node3D>(nmi)->set_transform(mi->get_transform()); p_node->replace_by(nmi); memdelete(p_node); p_node = nmi; - } else if (_teststr(name, "vehicle")) { - if (isroot) { - return p_node; - } - - Node *owner = p_node->get_owner(); - Node3D *s = Object::cast_to<Node3D>(p_node); - VehicleBody3D *bv = memnew(VehicleBody3D); - String n = _fixstr(p_node->get_name(), "vehicle"); - bv->set_name(n); - p_node->replace_by(bv); - p_node->set_name(n); - bv->add_child(p_node); - bv->set_owner(owner); - p_node->set_owner(owner); - bv->set_transform(s->get_transform()); - s->set_transform(Transform()); - - p_node = bv; - - } else if (_teststr(name, "wheel")) { - if (isroot) { - return p_node; - } - Node *owner = p_node->get_owner(); - Node3D *s = Object::cast_to<Node3D>(p_node); - VehicleWheel3D *bv = memnew(VehicleWheel3D); - String n = _fixstr(p_node->get_name(), "wheel"); - bv->set_name(n); - p_node->replace_by(bv); - p_node->set_name(n); - bv->add_child(p_node); - bv->set_owner(owner); - p_node->set_owner(owner); - bv->set_transform(s->get_transform()); - s->set_transform(Transform()); - - p_node = bv; - - } else if (Object::cast_to<MeshInstance3D>(p_node)) { + } else if (Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { //last attempt, maybe collision inside the mesh data - MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_node); + EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); - Ref<ArrayMesh> mesh = mi->get_mesh(); + Ref<EditorSceneImporterMesh> mesh = mi->get_mesh(); if (!mesh.is_null()) { List<Ref<Shape3D>> shapes; if (collision_map.has(mesh)) { @@ -644,27 +573,314 @@ Node *ResourceImporterScene::_fix_node(Node *p_node, Node *p_root, Map<Ref<Mesh> return p_node; } -void ResourceImporterScene::_create_clips(Node *scene, const Array &p_clips, bool p_bake_all) { - if (!scene->has_node(String("AnimationPlayer"))) { - return; +Node *ResourceImporterScene::_post_fix_node(Node *p_node, Node *p_root, Map<Ref<EditorSceneImporterMesh>, List<Ref<Shape3D>>> &collision_map, Set<Ref<EditorSceneImporterMesh>> &r_scanned_meshes, const Dictionary &p_node_data, const Dictionary &p_material_data, const Dictionary &p_animation_data, float p_animation_fps) { + // children first + for (int i = 0; i < p_node->get_child_count(); i++) { + Node *r = _post_fix_node(p_node->get_child(i), p_root, collision_map, r_scanned_meshes, p_node_data, p_material_data, p_animation_data, p_animation_fps); + if (!r) { + i--; //was erased + } + } + + bool isroot = p_node == p_root; + + String import_id; + + if (p_node->has_meta("import_id")) { + import_id = p_node->get_meta("import_id"); + } else { + import_id = "PATH:" + p_root->get_path_to(p_node); + } + + Dictionary node_settings; + if (p_node_data.has(import_id)) { + node_settings = p_node_data[import_id]; + } + + if (!isroot && (node_settings.has("import/skip_import") && bool(node_settings["import/skip_import"]))) { + memdelete(p_node); + return nullptr; + } + + if (Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { + EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + + Ref<EditorSceneImporterMesh> m = mi->get_mesh(); + + if (m.is_valid()) { + if (!r_scanned_meshes.has(m)) { + for (int i = 0; i < m->get_surface_count(); i++) { + Ref<Material> mat = m->get_surface_material(i); + if (mat.is_valid()) { + String mat_id; + if (mat->has_meta("import_id")) { + mat_id = mat->get_meta("import_id"); + } else { + mat_id = mat->get_name(); + } + + if (mat_id != String() && p_material_data.has(mat_id)) { + Dictionary matdata = p_material_data[mat_id]; + if (matdata.has("use_external/enabled") && bool(matdata["use_external/enabled"]) && matdata.has("use_external/path")) { + String path = matdata["use_external/path"]; + Ref<Material> external_mat = ResourceLoader::load(path); + if (external_mat.is_valid()) { + m->set_surface_material(i, external_mat); + } + } + } + } + } + + r_scanned_meshes.insert(m); + } + + if (node_settings.has("generate/physics")) { + int mesh_physics_mode = node_settings["generate/physics"]; + + if (mesh_physics_mode != MESH_PHYSICS_DISABLED) { + List<Ref<Shape3D>> shapes; + + if (collision_map.has(m)) { + shapes = collision_map[m]; + } else { + switch (mesh_physics_mode) { + case MESH_PHYSICS_MESH_AND_STATIC_COLLIDER: { + _pre_gen_shape_list(m, shapes, false); + } break; + case MESH_PHYSICS_RIGID_BODY_AND_MESH: { + _pre_gen_shape_list(m, shapes, true); + } break; + case MESH_PHYSICS_STATIC_COLLIDER_ONLY: { + _pre_gen_shape_list(m, shapes, false); + } break; + case MESH_PHYSICS_AREA_ONLY: { + _pre_gen_shape_list(m, shapes, true); + } break; + } + } + + if (shapes.size()) { + CollisionObject3D *base = nullptr; + switch (mesh_physics_mode) { + case MESH_PHYSICS_MESH_AND_STATIC_COLLIDER: { + StaticBody3D *col = memnew(StaticBody3D); + col->set_name("Collider"); + p_node->add_child(col); + base = col; + } break; + case MESH_PHYSICS_RIGID_BODY_AND_MESH: { + RigidBody3D *rigid_body = memnew(RigidBody3D); + rigid_body->set_name(p_node->get_name()); + p_node->replace_by(rigid_body); + rigid_body->set_transform(mi->get_transform()); + p_node = rigid_body; + mi->set_name("mesh"); + mi->set_transform(Transform()); + rigid_body->add_child(mi); + mi->set_owner(rigid_body->get_owner()); + base = rigid_body; + } break; + case MESH_PHYSICS_STATIC_COLLIDER_ONLY: { + StaticBody3D *col = memnew(StaticBody3D); + col->set_transform(mi->get_transform()); + col->set_name(p_node->get_name()); + p_node->replace_by(col); + memdelete(p_node); + p_node = col; + base = col; + } break; + case MESH_PHYSICS_AREA_ONLY: { + Area3D *area = memnew(Area3D); + area->set_transform(mi->get_transform()); + area->set_name(p_node->get_name()); + p_node->replace_by(area); + memdelete(p_node); + p_node = area; + base = area; + + } break; + } + + int idx = 0; + for (List<Ref<Shape3D>>::Element *E = shapes.front(); E; E = E->next()) { + CollisionShape3D *cshape = memnew(CollisionShape3D); + cshape->set_shape(E->get()); + base->add_child(cshape); + + cshape->set_name("shape" + itos(idx)); + cshape->set_owner(base->get_owner()); + idx++; + } + } + } + } + } + } + + //navmesh (node may have changed type above) + if (Object::cast_to<EditorSceneImporterMeshNode3D>(p_node)) { + EditorSceneImporterMeshNode3D *mi = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); + + Ref<EditorSceneImporterMesh> m = mi->get_mesh(); + + if (m.is_valid()) { + if (node_settings.has("generate/navmesh")) { + int navmesh_mode = node_settings["generate/navmesh"]; + + if (navmesh_mode != NAVMESH_DISABLED) { + NavigationRegion3D *nmi = memnew(NavigationRegion3D); + + Ref<NavigationMesh> nmesh = m->create_navigation_mesh(); + nmi->set_navigation_mesh(nmesh); + + if (navmesh_mode == NAVMESH_NAVMESH_ONLY) { + nmi->set_transform(mi->get_transform()); + p_node->replace_by(nmi); + memdelete(p_node); + p_node = nmi; + } else { + mi->add_child(nmi); + nmi->set_owner(mi->get_owner()); + } + } + } + } + } + + if (Object::cast_to<AnimationPlayer>(p_node)) { + AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node); + + { + //make sure this is unique + node_settings = node_settings.duplicate(true); + //fill node settings for this node with default values + List<ImportOption> iopts; + get_internal_import_options(INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE, &iopts); + for (List<ImportOption>::Element *E = iopts.front(); E; E = E->next()) { + if (!node_settings.has(E->get().option.name)) { + node_settings[E->get().option.name] = E->get().default_value; + } + } + } + + bool use_optimizer = node_settings["optimizer/enabled"]; + float anim_optimizer_linerr = node_settings["optimizer/max_linear_error"]; + float anim_optimizer_angerr = node_settings["optimizer/max_angular_error"]; + float anim_optimizer_maxang = node_settings["optimizer/max_angle"]; + + if (use_optimizer) { + _optimize_animations(ap, anim_optimizer_linerr, anim_optimizer_angerr, anim_optimizer_maxang); + } + + Array animation_clips; + { + int clip_count = node_settings["clips/amount"]; + + for (int i = 0; i < clip_count; i++) { + String name = node_settings["clip_" + itos(i + 1) + "/name"]; + int from_frame = node_settings["clip_" + itos(i + 1) + "/start_frame"]; + int end_frame = node_settings["clip_" + itos(i + 1) + "/end_frame"]; + bool loop = node_settings["clip_" + itos(i + 1) + "/loops"]; + bool save_to_file = node_settings["clip_" + itos(i + 1) + "/save_to_file/enabled"]; + bool save_to_path = node_settings["clip_" + itos(i + 1) + "/save_to_file/path"]; + bool save_to_file_keep_custom = node_settings["clip_" + itos(i + 1) + "/save_to_file/keep_custom_tracks"]; + + animation_clips.push_back(name); + animation_clips.push_back(from_frame / p_animation_fps); + animation_clips.push_back(end_frame / p_animation_fps); + animation_clips.push_back(loop); + animation_clips.push_back(save_to_file); + animation_clips.push_back(save_to_path); + animation_clips.push_back(save_to_file_keep_custom); + } + } + + if (animation_clips.size()) { + _create_clips(ap, animation_clips, true); + } else { + List<StringName> anims; + ap->get_animation_list(&anims); + for (List<StringName>::Element *E = anims.front(); E; E = E->next()) { + String name = E->get(); + Ref<Animation> anim = ap->get_animation(name); + if (p_animation_data.has(name)) { + Dictionary anim_settings = p_animation_data[name]; + { + //fill with default values + List<ImportOption> iopts; + get_internal_import_options(INTERNAL_IMPORT_CATEGORY_ANIMATION, &iopts); + for (List<ImportOption>::Element *F = iopts.front(); F; F = F->next()) { + if (!anim_settings.has(F->get().option.name)) { + anim_settings[F->get().option.name] = F->get().default_value; + } + } + } + + anim->set_loop(anim_settings["settings/loops"]); + bool save = anim_settings["save_to_file/enabled"]; + String path = anim_settings["save_to_file/path"]; + bool keep_custom = anim_settings["save_to_file/keep_custom_tracks"]; + + Ref<Animation> saved_anim = _save_animation_to_file(anim, save, path, keep_custom); + + if (saved_anim != anim) { + ap->add_animation(name, saved_anim); //replace + } + } + } + } } - Node *n = scene->get_node(String("AnimationPlayer")); - ERR_FAIL_COND(!n); - AnimationPlayer *anim = Object::cast_to<AnimationPlayer>(n); - ERR_FAIL_COND(!anim); + return p_node; +} + +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) { + if (!p_save_to_file || !p_save_to_path.is_resource_file()) { + return anim; + } + + if (FileAccess::exists(p_save_to_path) && p_keep_custom_tracks) { + // Copy custom animation tracks from previously imported files. + Ref<Animation> old_anim = ResourceLoader::load(p_save_to_path, "Animation", ResourceFormatLoader::CACHE_MODE_IGNORE); + if (old_anim.is_valid()) { + for (int i = 0; i < old_anim->get_track_count(); i++) { + if (!old_anim->track_is_imported(i)) { + old_anim->copy_track(i, anim); + } + } + anim->set_loop(old_anim->has_loop()); + } + } + if (ResourceCache::has(p_save_to_path)) { + Ref<Animation> old_anim = Ref<Resource>(ResourceCache::get(p_save_to_path)); + if (old_anim.is_valid()) { + old_anim->copy_from(anim); + anim = old_anim; + } + } + anim->set_path(p_save_to_path, true); // Set path to save externally. + Error err = ResourceSaver::save(p_save_to_path, anim, ResourceSaver::FLAG_CHANGE_PATH); + ERR_FAIL_COND_V_MSG(err != OK, anim, "Saving of animation failed: " + p_save_to_path); + return anim; +} + +void ResourceImporterScene::_create_clips(AnimationPlayer *anim, const Array &p_clips, bool p_bake_all) { if (!anim->has_animation("default")) { return; } Ref<Animation> default_anim = anim->get_animation("default"); - for (int i = 0; i < p_clips.size(); i += 4) { + for (int i = 0; i < p_clips.size(); i += 7) { String name = p_clips[i]; float from = p_clips[i + 1]; float to = p_clips[i + 2]; bool loop = p_clips[i + 3]; + bool save_to_file = p_clips[i + 4]; + String save_to_path = p_clips[i + 5]; + bool keep_current = p_clips[i + 6]; if (from >= to) { continue; } @@ -752,141 +968,17 @@ void ResourceImporterScene::_create_clips(Node *scene, const Array &p_clips, boo new_anim->set_loop(loop); new_anim->set_length(to - from); anim->add_animation(name, new_anim); - } - - anim->remove_animation("default"); //remove default (no longer needed) -} - -void ResourceImporterScene::_filter_anim_tracks(Ref<Animation> anim, Set<String> &keep) { - Ref<Animation> a = anim; - ERR_FAIL_COND(!a.is_valid()); - for (int j = 0; j < a->get_track_count(); j++) { - String path = a->track_get_path(j); - - if (!keep.has(path)) { - a->remove_track(j); - j--; + Ref<Animation> saved_anim = _save_animation_to_file(new_anim, save_to_file, save_to_path, keep_current); + if (saved_anim != new_anim) { + anim->add_animation(name, saved_anim); } } -} - -void ResourceImporterScene::_filter_tracks(Node *scene, const String &p_text) { - if (!scene->has_node(String("AnimationPlayer"))) { - return; - } - Node *n = scene->get_node(String("AnimationPlayer")); - ERR_FAIL_COND(!n); - AnimationPlayer *anim = Object::cast_to<AnimationPlayer>(n); - ERR_FAIL_COND(!anim); - - Vector<String> strings = p_text.split("\n"); - for (int i = 0; i < strings.size(); i++) { - strings.write[i] = strings[i].strip_edges(); - } - - List<StringName> anim_names; - anim->get_animation_list(&anim_names); - for (List<StringName>::Element *E = anim_names.front(); E; E = E->next()) { - String name = E->get(); - bool valid_for_this = false; - bool valid = false; - - Set<String> keep; - Set<String> keep_local; - - for (int i = 0; i < strings.size(); i++) { - if (strings[i].begins_with("@")) { - valid_for_this = false; - for (Set<String>::Element *F = keep_local.front(); F; F = F->next()) { - keep.insert(F->get()); - } - keep_local.clear(); - Vector<String> filters = strings[i].substr(1, strings[i].length()).split(","); - for (int j = 0; j < filters.size(); j++) { - String fname = filters[j].strip_edges(); - if (fname == "") { - continue; - } - int fc = fname[0]; - bool plus; - if (fc == '+') { - plus = true; - } else if (fc == '-') { - plus = false; - } else { - continue; - } - - String filter = fname.substr(1, fname.length()).strip_edges(); - - if (!name.matchn(filter)) { - continue; - } - valid_for_this = plus; - } - - if (valid_for_this) { - valid = true; - } - - } else if (valid_for_this) { - Ref<Animation> a = anim->get_animation(name); - if (!a.is_valid()) { - continue; - } - - for (int j = 0; j < a->get_track_count(); j++) { - String path = a->track_get_path(j); - - String tname = strings[i]; - if (tname == "") { - continue; - } - int fc = tname[0]; - bool plus; - if (fc == '+') { - plus = true; - } else if (fc == '-') { - plus = false; - } else { - continue; - } - - String filter = tname.substr(1, tname.length()).strip_edges(); - - if (!path.matchn(filter)) { - continue; - } - - if (plus) { - keep_local.insert(path); - } else if (!keep.has(path)) { - keep_local.erase(path); - } - } - } - } - - if (valid) { - for (Set<String>::Element *F = keep_local.front(); F; F = F->next()) { - keep.insert(F->get()); - } - _filter_anim_tracks(anim->get_animation(name), keep); - } - } + anim->remove_animation("default"); //remove default (no longer needed) } -void ResourceImporterScene::_optimize_animations(Node *scene, float p_max_lin_error, float p_max_ang_error, float p_max_angle) { - if (!scene->has_node(String("AnimationPlayer"))) { - return; - } - Node *n = scene->get_node(String("AnimationPlayer")); - ERR_FAIL_COND(!n); - AnimationPlayer *anim = Object::cast_to<AnimationPlayer>(n); - ERR_FAIL_COND(!anim); - +void ResourceImporterScene::_optimize_animations(AnimationPlayer *anim, float p_max_lin_error, float p_max_ang_error, float p_max_angle) { List<StringName> anim_names; anim->get_animation_list(&anim_names); for (List<StringName>::Element *E = anim_names.front(); E; E = E->next()) { @@ -895,208 +987,99 @@ void ResourceImporterScene::_optimize_animations(Node *scene, float p_max_lin_er } } -static String _make_extname(const String &p_str) { - String ext_name = p_str.replace(".", "_"); - ext_name = ext_name.replace(":", "_"); - ext_name = ext_name.replace("\"", "_"); - ext_name = ext_name.replace("<", "_"); - ext_name = ext_name.replace(">", "_"); - ext_name = ext_name.replace("/", "_"); - ext_name = ext_name.replace("|", "_"); - ext_name = ext_name.replace("\\", "_"); - ext_name = ext_name.replace("?", "_"); - ext_name = ext_name.replace("*", "_"); - - return ext_name; -} - -void ResourceImporterScene::_find_meshes(Node *p_node, Map<Ref<ArrayMesh>, Transform> &meshes) { - List<PropertyInfo> pi; - p_node->get_property_list(&pi); - - MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_node); - - if (mi) { - Ref<ArrayMesh> mesh = mi->get_mesh(); - - if (mesh.is_valid() && !meshes.has(mesh)) { - Node3D *s = mi; - Transform transform; - while (s) { - transform = transform * s->get_transform(); - s = Object::cast_to<Node3D>(s->get_parent()); +void ResourceImporterScene::get_internal_import_options(InternalImportCategory p_category, List<ImportOption> *r_options) const { + switch (p_category) { + case INTERNAL_IMPORT_CATEGORY_NODE: { + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "import/skip_import", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false)); + } break; + case INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE: { + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "import/skip_import", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false)); + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/physics", PROPERTY_HINT_ENUM, "Disabled,Mesh + Static Collider,Rigid Body + Mesh,Static Collider Only,Area Only"), 0)); + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/navmesh", PROPERTY_HINT_ENUM, "Disabled,Mesh + NavMesh,NavMesh Only"), 0)); + } break; + case INTERNAL_IMPORT_CATEGORY_MESH: { + 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)); + r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "save_to_file/path", PROPERTY_HINT_SAVE_FILE, "*.res,*.tres"), "")); + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "save_to_file/make_streamable"), "")); + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/shadow_meshes", PROPERTY_HINT_ENUM, "Default,Enable,Disable"), 0)); + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/lightmap_uv", PROPERTY_HINT_ENUM, "Default,Enable,Disable"), 0)); + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "generate/lods", PROPERTY_HINT_ENUM, "Default,Enable,Disable"), 0)); + } break; + case INTERNAL_IMPORT_CATEGORY_MATERIAL: { + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "use_external/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false)); + r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "use_external/path", PROPERTY_HINT_FILE, "*.material,*.res,*.tres"), "")); + } break; + case INTERNAL_IMPORT_CATEGORY_ANIMATION: { + r_options->push_back(ResourceImporter::ImportOption(PropertyInfo(Variant::BOOL, "settings/loops"), false)); + 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)); + r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "save_to_file/path", PROPERTY_HINT_SAVE_FILE, "*.res,*.tres"), "")); + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "save_to_file/keep_custom_tracks"), "")); + } break; + case INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE: { + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "import/skip_import", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), false)); + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "optimizer/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), true)); + r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "optimizer/max_linear_error"), 0.05)); + r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "optimizer/max_angular_error"), 0.01)); + r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "optimizer/max_angle"), 22)); + 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)); + + for (int i = 0; i < 256; i++) { + r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "slice_" + itos(i + 1) + "/name"), "")); + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slice_" + itos(i + 1) + "/start_frame"), 0)); + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "slice_" + itos(i + 1) + "/end_frame"), 0)); + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "slice_" + itos(i + 1) + "/loops"), false)); + 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)); + r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "slice_" + itos(i + 1) + "/save_to_file/path", PROPERTY_HINT_SAVE_FILE, ".res,*.tres"), "")); + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "slice_" + itos(i + 1) + "/save_to_file/keep_custom_tracks"), false)); } - - meshes[mesh] = transform; + } break; + default: { } } - for (int i = 0; i < p_node->get_child_count(); i++) { - _find_meshes(p_node->get_child(i), meshes); - } } -void ResourceImporterScene::_make_external_resources(Node *p_node, const String &p_base_path, bool p_make_animations, bool p_animations_as_text, bool p_keep_animations, bool p_make_materials, bool p_materials_as_text, bool p_keep_materials, bool p_make_meshes, bool p_meshes_as_text, Map<Ref<Animation>, Ref<Animation>> &p_animations, Map<Ref<Material>, Ref<Material>> &p_materials, Map<Ref<ArrayMesh>, Ref<ArrayMesh>> &p_meshes) { - List<PropertyInfo> pi; - - if (p_make_animations) { - if (Object::cast_to<AnimationPlayer>(p_node)) { - AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(p_node); - - List<StringName> anims; - ap->get_animation_list(&anims); - for (List<StringName>::Element *E = anims.front(); E; E = E->next()) { - Ref<Animation> anim = ap->get_animation(E->get()); - ERR_CONTINUE(anim.is_null()); - - if (!p_animations.has(anim)) { - // Tracks from source file should be set as imported, anything else is a custom track. - for (int i = 0; i < anim->get_track_count(); i++) { - anim->track_set_imported(i, true); - } - - String ext_name; - - if (p_animations_as_text) { - ext_name = p_base_path.plus_file(_make_extname(E->get()) + ".tres"); - } else { - ext_name = p_base_path.plus_file(_make_extname(E->get()) + ".anim"); - } - - if (FileAccess::exists(ext_name) && p_keep_animations) { - // Copy custom animation tracks from previously imported files. - Ref<Animation> old_anim = ResourceLoader::load(ext_name, "Animation", ResourceFormatLoader::CACHE_MODE_IGNORE); - if (old_anim.is_valid()) { - for (int i = 0; i < old_anim->get_track_count(); i++) { - if (!old_anim->track_is_imported(i)) { - old_anim->copy_track(i, anim); - } - } - anim->set_loop(old_anim->has_loop()); - } - } - - anim->set_path(ext_name, true); // Set path to save externally. - ResourceSaver::save(ext_name, anim, ResourceSaver::FLAG_CHANGE_PATH); - p_animations[anim] = anim; - } +bool ResourceImporterScene::get_internal_option_visibility(InternalImportCategory p_category, const String &p_option, const Map<StringName, Variant> &p_options) const { + if (p_options.has("import/skip_import") && p_option != "import/skip_import" && bool(p_options["import/skip_import"])) { + return false; //if skip import + } + switch (p_category) { + case INTERNAL_IMPORT_CATEGORY_NODE: { + } break; + case INTERNAL_IMPORT_CATEGORY_MESH_3D_NODE: { + } break; + case INTERNAL_IMPORT_CATEGORY_MESH: { + if (p_option == "save_to_file/path" || p_option == "save_to_file/make_streamable") { + return p_options["save_to_file/enabled"]; + } + } break; + case INTERNAL_IMPORT_CATEGORY_MATERIAL: { + if (p_option == "use_external/path") { + return p_options["use_external/enabled"]; + } + } break; + case INTERNAL_IMPORT_CATEGORY_ANIMATION: { + if (p_option == "save_to_file/path" || p_option == "save_to_file/keep_custom_tracks") { + return p_options["save_to_file/enabled"]; + } + } break; + case INTERNAL_IMPORT_CATEGORY_ANIMATION_NODE: { + if (p_option.begins_with("animation/optimizer/") && p_option != "animation/optimizer/enabled" && !bool(p_options["animation/optimizer/enabled"])) { + return false; } - } - } - - p_node->get_property_list(&pi); - - for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) { - if (E->get().type == Variant::OBJECT) { - Ref<Material> mat = p_node->get(E->get().name); - - if (p_make_materials && mat.is_valid() && mat->get_name() != "") { - if (!p_materials.has(mat)) { - String ext_name; - - if (p_materials_as_text) { - ext_name = p_base_path.plus_file(_make_extname(mat->get_name()) + ".tres"); - } else { - ext_name = p_base_path.plus_file(_make_extname(mat->get_name()) + ".material"); - } - - if (p_keep_materials && FileAccess::exists(ext_name)) { - //if exists, use it - p_materials[mat] = ResourceLoader::load(ext_name); - } else { - ResourceSaver::save(ext_name, mat, ResourceSaver::FLAG_CHANGE_PATH); - p_materials[mat] = ResourceLoader::load(ext_name, "", ResourceFormatLoader::CACHE_MODE_IGNORE); // disable loading from the cache. - } - } - - if (p_materials[mat] != mat) { - p_node->set(E->get().name, p_materials[mat]); - } - } else { - Ref<ArrayMesh> mesh = p_node->get(E->get().name); - - if (mesh.is_valid()) { - bool mesh_just_added = false; - - if (p_make_meshes) { - if (!p_meshes.has(mesh)) { - //meshes are always overwritten, keeping them is not practical - String ext_name; - - if (p_meshes_as_text) { - ext_name = p_base_path.plus_file(_make_extname(mesh->get_name()) + ".tres"); - } else { - ext_name = p_base_path.plus_file(_make_extname(mesh->get_name()) + ".mesh"); - } - - ResourceSaver::save(ext_name, mesh, ResourceSaver::FLAG_CHANGE_PATH); - p_meshes[mesh] = ResourceLoader::load(ext_name); - p_node->set(E->get().name, p_meshes[mesh]); - mesh_just_added = true; - } - } - - if (p_make_materials) { - if (mesh_just_added || !p_meshes.has(mesh)) { - for (int i = 0; i < mesh->get_surface_count(); i++) { - mat = mesh->surface_get_material(i); - - if (!mat.is_valid()) { - continue; - } - if (mat->get_name() == "") { - continue; - } - - if (!p_materials.has(mat)) { - String ext_name; - - if (p_materials_as_text) { - ext_name = p_base_path.plus_file(_make_extname(mat->get_name()) + ".tres"); - } else { - ext_name = p_base_path.plus_file(_make_extname(mat->get_name()) + ".material"); - } - - if (p_keep_materials && FileAccess::exists(ext_name)) { - //if exists, use it - p_materials[mat] = ResourceLoader::load(ext_name); - } else { - ResourceSaver::save(ext_name, mat, ResourceSaver::FLAG_CHANGE_PATH); - p_materials[mat] = ResourceLoader::load(ext_name, "", ResourceFormatLoader::CACHE_MODE_IGNORE); // disable loading from the cache. - } - } - - if (p_materials[mat] != mat) { - mesh->surface_set_material(i, p_materials[mat]); - - //re-save the mesh since a material is now assigned - if (p_make_meshes) { - String ext_name; - - if (p_meshes_as_text) { - ext_name = p_base_path.plus_file(_make_extname(mesh->get_name()) + ".tres"); - } else { - ext_name = p_base_path.plus_file(_make_extname(mesh->get_name()) + ".mesh"); - } - - ResourceSaver::save(ext_name, mesh, ResourceSaver::FLAG_CHANGE_PATH); - p_meshes[mesh] = ResourceLoader::load(ext_name); - } - } - } - if (!p_make_meshes) { - p_meshes[mesh] = Ref<ArrayMesh>(); //save it anyway, so it won't be checked again - } - } - } + if (p_option.begins_with("animation/slice_")) { + int max_slice = p_options["animation/slices/amount"]; + int slice = p_option.get_slice("/", 1).get_slice("_", 1).to_int() - 1; + if (slice >= max_slice) { + return false; } } + } break; + default: { } } - for (int i = 0; i < p_node->get_child_count(); i++) { - _make_external_resources(p_node->get_child(i), p_base_path, p_make_animations, p_animations_as_text, p_keep_animations, p_make_materials, p_materials_as_text, p_keep_materials, p_make_meshes, p_meshes_as_text, p_animations, p_materials, p_meshes); - } + return true; } void ResourceImporterScene::get_import_options(List<ImportOption> *r_options, int p_preset) const { @@ -1115,42 +1098,18 @@ void ResourceImporterScene::get_import_options(List<ImportOption> *r_options, in script_ext_hint += "*." + E->get(); } - bool materials_out = p_preset == PRESET_SEPARATE_MATERIALS || p_preset == PRESET_SEPARATE_MESHES_AND_MATERIALS || p_preset == PRESET_MULTIPLE_SCENES_AND_MATERIALS || p_preset == PRESET_SEPARATE_MATERIALS_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_MATERIALS_AND_ANIMATIONS; - bool meshes_out = p_preset == PRESET_SEPARATE_MESHES || p_preset == PRESET_SEPARATE_MESHES_AND_MATERIALS || p_preset == PRESET_SEPARATE_MESHES_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_MATERIALS_AND_ANIMATIONS; - bool scenes_out = p_preset == PRESET_MULTIPLE_SCENES || p_preset == PRESET_MULTIPLE_SCENES_AND_MATERIALS; - bool animations_out = p_preset == PRESET_SEPARATE_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MATERIALS_AND_ANIMATIONS || p_preset == PRESET_SEPARATE_MESHES_MATERIALS_AND_ANIMATIONS; - r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "nodes/root_scale", PROPERTY_HINT_RANGE, "0.001,1000,0.001"), 1.0)); - r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "nodes/custom_script", PROPERTY_HINT_FILE, script_ext_hint), "")); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "nodes/storage", PROPERTY_HINT_ENUM, "Single Scene,Instanced Sub-Scenes"), scenes_out ? 1 : 0)); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "materials/location", PROPERTY_HINT_ENUM, "Node,Mesh"), (meshes_out || materials_out) ? 1 : 0)); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "materials/storage", PROPERTY_HINT_ENUM, "Built-In,Files (.material),Files (.tres)", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), materials_out ? 1 : 0)); - r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "materials/keep_on_reimport"), materials_out)); r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "meshes/ensure_tangents"), true)); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "meshes/storage", PROPERTY_HINT_ENUM, "Built-In,Files (.mesh),Files (.tres)"), meshes_out ? 1 : 0)); r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "meshes/generate_lods"), true)); r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "meshes/create_shadow_meshes"), true)); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "meshes/light_baking", PROPERTY_HINT_ENUM, "Disabled,Enable,Gen Lightmaps", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0)); + r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "meshes/light_baking", PROPERTY_HINT_ENUM, "Disabled,Dynamic,Static,Static Lightmaps", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 2)); r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "meshes/lightmap_texel_size", PROPERTY_HINT_RANGE, "0.001,100,0.001"), 0.1)); r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "skins/use_named_skins"), true)); - r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "external_files/store_in_subdir"), false)); - r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/import", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), true)); + r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/import"), true)); r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "animation/fps", PROPERTY_HINT_RANGE, "1,120,1"), 15)); - r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "animation/filter_script", PROPERTY_HINT_MULTILINE_TEXT), "")); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "animation/storage", PROPERTY_HINT_ENUM, "Built-In,Files (.anim),Files (.tres)", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), animations_out)); - r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/keep_custom_tracks"), animations_out)); - r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/optimizer/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), true)); - r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "animation/optimizer/max_linear_error"), 0.05)); - r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "animation/optimizer/max_angular_error"), 0.01)); - r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "animation/optimizer/max_angle"), 22)); - r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/optimizer/remove_unused_tracks"), true)); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "animation/clips/amount", PROPERTY_HINT_RANGE, "0,256,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), 0)); - for (int i = 0; i < 256; i++) { - r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "animation/clip_" + itos(i + 1) + "/name"), "")); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "animation/clip_" + itos(i + 1) + "/start_frame"), 0)); - r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "animation/clip_" + itos(i + 1) + "/end_frame"), 0)); - r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "animation/clip_" + itos(i + 1) + "/loops"), false)); - } + r_options->push_back(ImportOption(PropertyInfo(Variant::STRING, "import_script/path", PROPERTY_HINT_FILE, script_ext_hint), "")); + + r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "_subresources", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), Dictionary())); } void ResourceImporterScene::_replace_owner(Node *p_node, Node *p_scene, Node *p_new_owner) { @@ -1222,7 +1181,7 @@ Ref<Animation> ResourceImporterScene::import_animation_from_other_importer(Edito return importer->import_animation(p_path, p_flags, p_bake_fps); } -void ResourceImporterScene::_generate_meshes(Node *p_node, bool p_generate_lods, bool p_create_shadow_meshes) { +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<uint8_t> &r_dst_lightmap_cache) { EditorSceneImporterMeshNode3D *src_mesh_node = Object::cast_to<EditorSceneImporterMeshNode3D>(p_node); if (src_mesh_node) { //is mesh @@ -1235,14 +1194,94 @@ void ResourceImporterScene::_generate_meshes(Node *p_node, bool p_generate_lods, Ref<ArrayMesh> mesh; if (!src_mesh_node->get_mesh()->has_mesh()) { //do mesh processing - if (p_generate_lods) { + + bool generate_lods = p_generate_lods; + bool create_shadow_meshes = p_create_shadow_meshes; + bool bake_lightmaps = p_light_bake_mode == LIGHT_BAKE_STATIC_LIGHTMAPS; + String save_to_file; + + String mesh_id; + + if (src_mesh_node->get_mesh()->has_meta("import_id")) { + mesh_id = src_mesh_node->get_mesh()->get_meta("import_id"); + } else { + mesh_id = src_mesh_node->get_mesh()->get_name(); + } + + if (mesh_id != String() && p_mesh_data.has(mesh_id)) { + Dictionary mesh_settings = p_mesh_data[mesh_id]; + + if (mesh_settings.has("generate/shadow_meshes")) { + int shadow_meshes = mesh_settings["generate/shadow_meshes"]; + if (shadow_meshes == MESH_OVERRIDE_ENABLE) { + create_shadow_meshes = true; + } else if (shadow_meshes == MESH_OVERRIDE_DISABLE) { + create_shadow_meshes = false; + } + } + + if (mesh_settings.has("generate/lightmap_uv")) { + int lightmap_uv = mesh_settings["generate/lightmap_uv"]; + if (lightmap_uv == MESH_OVERRIDE_ENABLE) { + bake_lightmaps = true; + } else if (lightmap_uv == MESH_OVERRIDE_DISABLE) { + bake_lightmaps = false; + } + } + + if (mesh_settings.has("generate/lods")) { + int lods = mesh_settings["generate/lods"]; + if (lods == MESH_OVERRIDE_ENABLE) { + generate_lods = true; + } else if (lods == MESH_OVERRIDE_DISABLE) { + generate_lods = false; + } + } + + if (mesh_settings.has("save_to_file/enabled") && bool(mesh_settings["save_to_file/enabled"]) && mesh_settings.has("save_to_file/path")) { + save_to_file = mesh_settings["save_to_file/path"]; + if (!save_to_file.is_resource_file()) { + save_to_file = ""; + } + } + } + + if (generate_lods) { src_mesh_node->get_mesh()->generate_lods(); } - if (p_create_shadow_meshes) { + if (create_shadow_meshes) { src_mesh_node->get_mesh()->create_shadow_mesh(); } + + if (bake_lightmaps) { + Transform xf; + Node3D *n = src_mesh_node; + while (n) { + xf = n->get_transform() * xf; + n = n->get_parent_spatial(); + } + + //use xf as transform for mesh, and bake it + } + + if (save_to_file != String()) { + Ref<Mesh> existing = Ref<Resource>(ResourceCache::get(save_to_file)); + if (existing.is_valid()) { + //if somehow an existing one is useful, create + existing->reset_state(); + } + mesh = src_mesh_node->get_mesh()->get_mesh(existing); + + ResourceSaver::save(save_to_file, mesh); //override + + mesh->set_path(save_to_file, true); //takeover existing, if needed + + } else { + mesh = src_mesh_node->get_mesh()->get_mesh(); + } + } else { + mesh = src_mesh_node->get_mesh()->get_mesh(); } - mesh = src_mesh_node->get_mesh()->get_mesh(); if (mesh.is_valid()) { mesh_node->set_mesh(mesh); @@ -1251,15 +1290,68 @@ void ResourceImporterScene::_generate_meshes(Node *p_node, bool p_generate_lods, } } } + + switch (p_light_bake_mode) { + case LIGHT_BAKE_DISABLED: { + mesh_node->set_gi_mode(GeometryInstance3D::GI_MODE_DISABLED); + } break; + case LIGHT_BAKE_DYNAMIC: { + mesh_node->set_gi_mode(GeometryInstance3D::GI_MODE_DYNAMIC); + } break; + case LIGHT_BAKE_STATIC: + case LIGHT_BAKE_STATIC_LIGHTMAPS: { + mesh_node->set_gi_mode(GeometryInstance3D::GI_MODE_BAKED); + } break; + } + p_node->replace_by(mesh_node); memdelete(p_node); p_node = mesh_node; } for (int i = 0; i < p_node->get_child_count(); i++) { - _generate_meshes(p_node->get_child(i), p_generate_lods, p_create_shadow_meshes); + _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_dst_lightmap_cache); } } + +Node *ResourceImporterScene::pre_import(const String &p_source_file) { + Ref<EditorSceneImporter> importer; + String ext = p_source_file.get_extension().to_lower(); + + EditorProgress progress("pre-import", TTR("Pre-Import Scene"), 0); + progress.step(TTR("Importing Scene..."), 0); + + for (Set<Ref<EditorSceneImporter>>::Element *E = importers.front(); E; E = E->next()) { + List<String> extensions; + E->get()->get_extensions(&extensions); + + for (List<String>::Element *F = extensions.front(); F; F = F->next()) { + if (F->get().to_lower() == ext) { + importer = E->get(); + break; + } + } + + if (importer.is_valid()) { + break; + } + } + + ERR_FAIL_COND_V(!importer.is_valid(), nullptr); + + Error err; + Node *scene = importer->import_scene(p_source_file, EditorSceneImporter::IMPORT_ANIMATION | EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS, 15, nullptr, &err); + if (!scene || err != OK) { + return nullptr; + } + + Map<Ref<EditorSceneImporterMesh>, List<Ref<Shape3D>>> collision_map; + + _pre_fix_node(scene, scene, collision_map); + + return scene; +} + Error ResourceImporterScene::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) { const String &src_path = p_source_file; @@ -1289,27 +1381,21 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p float fps = p_options["animation/fps"]; - int import_flags = EditorSceneImporter::IMPORT_ANIMATION_DETECT_LOOP; - if (!bool(p_options["animation/optimizer/remove_unused_tracks"])) { - import_flags |= EditorSceneImporter::IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS; - } + int import_flags = 0; if (bool(p_options["animation/import"])) { import_flags |= EditorSceneImporter::IMPORT_ANIMATION; } - if (bool(p_options["meshes/ensure_tangents"])) { - import_flags |= EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS; - } - - if (int(p_options["materials/location"]) == 0) { - import_flags |= EditorSceneImporter::IMPORT_MATERIALS_IN_INSTANCES; - } - if (bool(p_options["skins/use_named_skins"])) { import_flags |= EditorSceneImporter::IMPORT_USE_NAMED_SKIN_BINDS; } + bool ensure_tangents = p_options["meshes/ensure_tangents"]; + if (ensure_tangents) { + import_flags |= EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS; + } + Error err = OK; List<String> missing_deps; // for now, not much will be done with this Node *scene = importer->import_scene(src_path, import_flags, fps, &missing_deps, &err); @@ -1317,6 +1403,29 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p return err; } + Dictionary subresources = p_options["_subresources"]; + + Dictionary node_data; + if (subresources.has("nodes")) { + node_data = subresources["nodes"]; + } + + Dictionary material_data; + if (subresources.has("materials")) { + material_data = subresources["materials"]; + } + + Dictionary animation_data; + if (subresources.has("animations")) { + animation_data = subresources["animations"]; + } + + Set<Ref<EditorSceneImporterMesh>> scanned_meshes; + Map<Ref<EditorSceneImporterMesh>, List<Ref<Shape3D>>> collision_map; + + _pre_fix_node(scene, scene, collision_map); + _post_fix_node(scene, scene, collision_map, scanned_meshes, node_data, material_data, animation_data, fps); + String root_type = p_options["nodes/root_type"]; root_type = root_type.split(" ")[0]; // full root_type is "ClassName (filename.gd)" for a script global class. @@ -1354,73 +1463,35 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p bool gen_lods = bool(p_options["meshes/generate_lods"]); bool create_shadow_meshes = bool(p_options["meshes/create_shadow_meshes"]); - - _generate_meshes(scene, gen_lods, create_shadow_meshes); - - err = OK; - - String animation_filter = String(p_options["animation/filter_script"]).strip_edges(); - - bool use_optimizer = p_options["animation/optimizer/enabled"]; - float anim_optimizer_linerr = p_options["animation/optimizer/max_linear_error"]; - float anim_optimizer_angerr = p_options["animation/optimizer/max_angular_error"]; - float anim_optimizer_maxang = p_options["animation/optimizer/max_angle"]; int light_bake_mode = p_options["meshes/light_baking"]; + float texel_size = p_options["meshes/lightmap_texel_size"]; + float lightmap_texel_size = MAX(0.001, texel_size); - Map<Ref<Mesh>, List<Ref<Shape3D>>> collision_map; + Vector<uint8_t> src_lightmap_cache; + Vector<uint8_t> dst_lightmap_cache; - scene = _fix_node(scene, scene, collision_map, LightBakeMode(light_bake_mode)); - - if (use_optimizer) { - _optimize_animations(scene, anim_optimizer_linerr, anim_optimizer_angerr, anim_optimizer_maxang); - } - - Array animation_clips; { - int clip_count = p_options["animation/clips/amount"]; - - for (int i = 0; i < clip_count; i++) { - String name = p_options["animation/clip_" + itos(i + 1) + "/name"]; - int from_frame = p_options["animation/clip_" + itos(i + 1) + "/start_frame"]; - int end_frame = p_options["animation/clip_" + itos(i + 1) + "/end_frame"]; - bool loop = p_options["animation/clip_" + itos(i + 1) + "/loops"]; - - animation_clips.push_back(name); - animation_clips.push_back(from_frame / fps); - animation_clips.push_back(end_frame / fps); - animation_clips.push_back(loop); + src_lightmap_cache = FileAccess::get_file_as_array(p_source_file + ".unwrap_cache", &err); + if (err != OK) { + src_lightmap_cache.clear(); } } - if (animation_clips.size()) { - _create_clips(scene, animation_clips, !bool(p_options["animation/optimizer/remove_unused_tracks"])); - } - if (animation_filter != "") { - _filter_tracks(scene, animation_filter); + Dictionary mesh_data; + if (subresources.has("meshes")) { + mesh_data = subresources["meshes"]; } + _generate_meshes(scene, mesh_data, gen_lods, create_shadow_meshes, LightBakeMode(light_bake_mode), lightmap_texel_size, src_lightmap_cache, dst_lightmap_cache); - bool external_animations = int(p_options["animation/storage"]) == 1 || int(p_options["animation/storage"]) == 2; - bool external_animations_as_text = int(p_options["animation/storage"]) == 2; - bool keep_custom_tracks = p_options["animation/keep_custom_tracks"]; - bool external_materials = int(p_options["materials/storage"]) == 1 || int(p_options["materials/storage"]) == 2; - bool external_materials_as_text = int(p_options["materials/storage"]) == 2; - bool external_meshes = int(p_options["meshes/storage"]) == 1 || int(p_options["meshes/storage"]) == 2; - bool external_meshes_as_text = int(p_options["meshes/storage"]) == 2; - bool external_scenes = int(p_options["nodes/storage"]) == 1; - - String base_path = p_source_file.get_base_dir(); - - if (external_animations || external_materials || external_meshes || external_scenes) { - if (bool(p_options["external_files/store_in_subdir"])) { - String subdir_name = p_source_file.get_file().get_basename(); - DirAccess *da = DirAccess::open(base_path); - Error err2 = da->make_dir(subdir_name); - memdelete(da); - ERR_FAIL_COND_V_MSG(err2 != OK && err2 != ERR_ALREADY_EXISTS, err2, "Cannot make directory '" + subdir_name + "'."); - base_path = base_path.plus_file(subdir_name); + if (dst_lightmap_cache.size()) { + FileAccessRef f = FileAccess::open(p_source_file + ".unwrap_cache", FileAccess::WRITE); + if (f) { + f->store_buffer(dst_lightmap_cache.ptr(), dst_lightmap_cache.size()); } } + err = OK; +#if 0 if (light_bake_mode == 2 /* || generate LOD */) { Map<Ref<ArrayMesh>, Transform> meshes; _find_meshes(scene, meshes); @@ -1445,9 +1516,6 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p } } - float texel_size = p_options["meshes/lightmap_texel_size"]; - texel_size = MAX(0.001, texel_size); - Map<String, unsigned int> used_unwraps; EditorProgress progress2("gen_lightmaps", TTR("Generating Lightmaps"), meshes.size()); @@ -1469,7 +1537,7 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p if (err2 != OK) { EditorNode::add_io_error("Mesh '" + name + "' failed lightmap generation. Please fix geometry."); } else { - String hash = String::md5((unsigned char *)ret_cache_data); +` String hash = String::md5((unsigned char *)ret_cache_data); used_unwraps.insert(hash, ret_cache_size); if (!ret_used_cache) { @@ -1482,7 +1550,7 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p } else { int current_size = cache_data.size(); cache_data.resize(cache_data.size() + ret_cache_size); - unsigned char *ptrw = cache_data.ptrw(); + unsigned char *ptrw = cache_data.ptrw(); memcpy(&ptrw[current_size], ret_cache_data, ret_cache_size); int *data = (int *)ptrw; data[0] += 1; @@ -1530,20 +1598,11 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p file->close(); } } - - if (external_animations || external_materials || external_meshes) { - Map<Ref<Animation>, Ref<Animation>> anim_map; - Map<Ref<Material>, Ref<Material>> mat_map; - Map<Ref<ArrayMesh>, Ref<ArrayMesh>> mesh_map; - - bool keep_materials = bool(p_options["materials/keep_on_reimport"]); - - _make_external_resources(scene, base_path, external_animations, external_animations_as_text, keep_custom_tracks, external_materials, external_materials_as_text, keep_materials, external_meshes, external_meshes_as_text, anim_map, mat_map, mesh_map); - } +#endif progress.step(TTR("Running Custom Script..."), 2); - String post_import_script_path = p_options["nodes/custom_script"]; + String post_import_script_path = p_options["import_script/path"]; Ref<EditorScenePostImport> post_import_script; if (post_import_script_path != "") { @@ -1562,7 +1621,7 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p } if (post_import_script.is_valid()) { - post_import_script->init(base_path, p_source_file); + post_import_script->init(p_source_file); scene = post_import_script->post_import(scene); if (!scene) { EditorNode::add_io_error( @@ -1574,29 +1633,6 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p progress.step(TTR("Saving..."), 104); - if (external_scenes) { - //save sub-scenes as instances! - for (int i = 0; i < scene->get_child_count(); i++) { - Node *child = scene->get_child(i); - if (child->get_owner() != scene) { - continue; //not a real child probably created by scene type (ig, a scrollbar) - } - _replace_owner(child, scene, child); - - String cn = String(child->get_name()).strip_edges().replace(".", "_").replace(":", "_"); - if (cn == String()) { - cn = "ChildNode" + itos(i); - } - String path = base_path.plus_file(cn + ".scn"); - child->set_filename(path); - - Ref<PackedScene> packer = memnew(PackedScene); - packer->pack(child); - err = ResourceSaver::save(path, packer); //do not take over, let the changed files reload themselves - ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save scene to file '" + path + "'."); - } - } - Ref<PackedScene> packer = memnew(PackedScene); packer->pack(scene); print_verbose("Saving scene to: " + p_save_path + ".scn"); @@ -1613,6 +1649,13 @@ Error ResourceImporterScene::import(const String &p_source_file, const String &p ResourceImporterScene *ResourceImporterScene::singleton = nullptr; +bool ResourceImporterScene::ResourceImporterScene::has_advanced_options() const { + return true; +} +void ResourceImporterScene::ResourceImporterScene::show_advanced_options(const String &p_path) { + SceneImportSettings::get_singleton()->open_settings(p_path); +} + ResourceImporterScene::ResourceImporterScene() { singleton = this; } |