Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved 3D Scene Importer #47166

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/io/resource_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ class ResourceImporter : public Reference {
ImportOption() {}
};

virtual bool has_advanced_options() const { return false; }
virtual void show_advanced_options(const String &p_path) {}

virtual int get_preset_count() const { return 0; }
virtual String get_preset_name(int p_idx) const { return String(); }

Expand Down
41 changes: 27 additions & 14 deletions editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector
return err;
}

void EditorFileSystem::_reimport_file(const String &p_file) {
void EditorFileSystem::_reimport_file(const String &p_file, const Map<StringName, Variant> *p_custom_options, const String &p_custom_importer) {
EditorFileSystemDirectory *fs = nullptr;
int cpos = -1;
bool found = _find_file(p_file, &fs, cpos);
Expand All @@ -1677,23 +1677,32 @@ void EditorFileSystem::_reimport_file(const String &p_file) {
//try to obtain existing params

Map<StringName, Variant> params;
String importer_name;
String importer_name; //empty by default though

if (p_custom_importer != String()) {
importer_name = p_custom_importer;
}
if (p_custom_options != nullptr) {
params = *p_custom_options;
}

if (FileAccess::exists(p_file + ".import")) {
//use existing
Ref<ConfigFile> cf;
cf.instance();
Error err = cf->load(p_file + ".import");
if (err == OK) {
if (cf->has_section("params")) {
List<String> sk;
cf->get_section_keys("params", &sk);
for (List<String>::Element *E = sk.front(); E; E = E->next()) {
params[E->get()] = cf->get_value("params", E->get());
if (p_custom_options == nullptr) {
Ref<ConfigFile> cf;
cf.instance();
Error err = cf->load(p_file + ".import");
if (err == OK) {
if (cf->has_section("params")) {
List<String> sk;
cf->get_section_keys("params", &sk);
for (List<String>::Element *E = sk.front(); E; E = E->next()) {
params[E->get()] = cf->get_value("params", E->get());
}
}
if (p_custom_importer != String() && cf->has_section("remap")) {
importer_name = cf->get_value("remap", "importer");
}
}
if (cf->has_section("remap")) {
importer_name = cf->get_value("remap", "importer");
}
}

Expand Down Expand Up @@ -1887,6 +1896,10 @@ void EditorFileSystem::_find_group_files(EditorFileSystemDirectory *efd, Map<Str
}
}

void EditorFileSystem::reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const Map<StringName, Variant> &p_custom_params) {
_reimport_file(p_file, &p_custom_params, p_importer);
}

void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
{
// Ensure that ProjectSettings::IMPORTED_FILES_PATH exists.
Expand Down
4 changes: 3 additions & 1 deletion editor/editor_file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class EditorFileSystem : public Node {

void _update_extensions();

void _reimport_file(const String &p_file);
void _reimport_file(const String &p_file, const Map<StringName, Variant> *p_custom_options = nullptr, const String &p_custom_importer = String());
Error _reimport_group(const String &p_group_file, const Vector<String> &p_files);

bool _test_for_reimport(const String &p_path, bool p_only_imported_files);
Expand Down Expand Up @@ -257,6 +257,8 @@ class EditorFileSystem : public Node {

void reimport_files(const Vector<String> &p_files);

void reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const Map<StringName, Variant> &p_custom_params);

void update_script_classes();

bool is_group_file(const String &p_path) const;
Expand Down
4 changes: 4 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
#include "editor/import/resource_importer_texture.h"
#include "editor/import/resource_importer_texture_atlas.h"
#include "editor/import/resource_importer_wav.h"
#include "editor/import/scene_import_settings.h"
#include "editor/import/scene_importer_mesh_node_3d.h"
#include "editor/import_dock.h"
#include "editor/multi_node_edit.h"
Expand Down Expand Up @@ -6179,6 +6180,9 @@ EditorNode::EditorNode() {
project_settings = memnew(ProjectSettingsEditor(&editor_data));
gui_base->add_child(project_settings);

scene_import_settings = memnew(SceneImportSettings);
gui_base->add_child(scene_import_settings);

export_template_manager = memnew(ExportTemplateManager);
gui_base->add_child(export_template_manager);

Expand Down
2 changes: 2 additions & 0 deletions editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Button;
class VSplitContainer;
class Window;
class SubViewport;
class SceneImportSettings;

class EditorNode : public Node {
GDCLASS(EditorNode, Node);
Expand Down Expand Up @@ -410,6 +411,7 @@ class EditorNode : public Node {
EditorResourcePreview *resource_preview;
EditorFolding editor_folding;

SceneImportSettings *scene_import_settings;
struct BottomPanelItem {
String name;
Control *control = nullptr;
Expand Down
20 changes: 19 additions & 1 deletion editor/filesystem_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,25 @@ void FileSystemDock::_select_file(const String &p_path, bool p_select_in_favorit
}
} else if (fpath != "Favorites") {
if (ResourceLoader::get_resource_type(fpath) == "PackedScene") {
editor->open_request(fpath);
bool is_imported = false;

{
List<String> importer_exts;
ResourceImporterScene::get_singleton()->get_recognized_extensions(&importer_exts);
String extension = fpath.get_extension();
for (List<String>::Element *E = importer_exts.front(); E; E = E->next()) {
if (extension.nocasecmp_to(E->get())) {
is_imported = true;
break;
}
}
}

if (is_imported) {
ResourceImporterScene::get_singleton()->show_advanced_options(fpath);
} else {
editor->open_request(fpath);
}
} else {
editor->load_resource(fpath);
}
Expand Down
64 changes: 45 additions & 19 deletions editor/import/editor_import_collada.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ struct ColladaImport {
Vector<int> valid_animated_properties;
Map<String, bool> bones_with_animation;

Set<String> mesh_unique_names;
Set<String> material_unique_names;

Error _populate_skeleton(Skeleton3D *p_skeleton, Collada::Node *p_node, int &r_bone, int p_parent);
Error _create_scene_skeletons(Collada::Node *p_node);
Error _create_scene(Collada::Node *p_node, Node3D *p_parent);
Expand Down Expand Up @@ -326,12 +329,25 @@ Error ColladaImport::_create_material(const String &p_target) {

Ref<StandardMaterial3D> material = memnew(StandardMaterial3D);

String base_name;
if (src_mat.name != "") {
material->set_name(src_mat.name);
base_name = src_mat.name;
} else if (effect.name != "") {
material->set_name(effect.name);
base_name = effect.name;
} else {
base_name = "Material";
}

String name = base_name;
int counter = 2;
while (material_unique_names.has(name)) {
name = base_name + itos(counter++);
}

material_unique_names.insert(name);

material->set_name(name);

// DIFFUSE

if (effect.diffuse.texture != "") {
Expand Down Expand Up @@ -1128,7 +1144,22 @@ Error ColladaImport::_create_resources(Collada::Node *p_node, bool p_use_compres
ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(meshid), ERR_INVALID_DATA);
mesh = Ref<EditorSceneImporterMesh>(memnew(EditorSceneImporterMesh));
const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid];
mesh->set_name(meshdata.name);
String name = meshdata.name;
if (name == "") {
name = "Mesh";
}
int counter = 2;
while (mesh_unique_names.has(name)) {
name = meshdata.name;
if (name == "") {
name = "Mesh";
}
name += itos(counter++);
}

mesh_unique_names.insert(name);

mesh->set_name(name);
Error err = _create_mesh_surfaces(morphs.size() == 0, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, morph, morphs, p_use_compression, use_mesh_builtin_materials);
ERR_FAIL_COND_V_MSG(err, err, "Cannot create mesh surface.");

Expand Down Expand Up @@ -1645,16 +1676,23 @@ void EditorSceneImporterCollada::get_extensions(List<String> *r_extensions) cons
}

Node *EditorSceneImporterCollada::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err) {
if (r_err) {
*r_err = OK;
}
ColladaImport state;
uint32_t flags = Collada::IMPORT_FLAG_SCENE;
if (p_flags & IMPORT_ANIMATION) {
flags |= Collada::IMPORT_FLAG_ANIMATION;
}

state.use_mesh_builtin_materials = !(p_flags & IMPORT_MATERIALS_IN_INSTANCES);
state.use_mesh_builtin_materials = true;
state.bake_fps = p_bake_fps;

Error err = state.load(p_path, flags, p_flags & EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS, p_flags & EditorSceneImporter::IMPORT_USE_COMPRESSION);
Error err = state.load(p_path, flags, p_flags & EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS, 0);

if (r_err) {
*r_err = err;
}

ERR_FAIL_COND_V_MSG(err != OK, nullptr, "Cannot load scene from file '" + p_path + "'.");

Expand All @@ -1674,7 +1712,7 @@ Node *EditorSceneImporterCollada::import_scene(const String &p_path, uint32_t p_
}

if (p_flags & IMPORT_ANIMATION) {
state.create_animations(p_flags & IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS, p_flags & EditorSceneImporter::IMPORT_ANIMATION_KEEP_VALUE_TRACKS);
state.create_animations(true, true);
AnimationPlayer *ap = memnew(AnimationPlayer);
for (int i = 0; i < state.animations.size(); i++) {
String name;
Expand All @@ -1684,12 +1722,6 @@ Node *EditorSceneImporterCollada::import_scene(const String &p_path, uint32_t p_
name = state.animations[i]->get_name();
}

if (p_flags & IMPORT_ANIMATION_DETECT_LOOP) {
if (name.begins_with("loop") || name.ends_with("loop") || name.begins_with("cycle") || name.ends_with("cycle")) {
state.animations.write[i]->set_loop(true);
}
}

ap->add_animation(name, state.animations[i]);
}
state.scene->add_child(ap);
Expand All @@ -1707,7 +1739,7 @@ Ref<Animation> EditorSceneImporterCollada::import_animation(const String &p_path
Error err = state.load(p_path, Collada::IMPORT_FLAG_ANIMATION, p_flags & EditorSceneImporter::IMPORT_GENERATE_TANGENT_ARRAYS);
ERR_FAIL_COND_V_MSG(err != OK, RES(), "Cannot load animation from file '" + p_path + "'.");

state.create_animations(p_flags & EditorSceneImporter::IMPORT_ANIMATION_FORCE_ALL_TRACKS_IN_ALL_CLIPS, p_flags & EditorSceneImporter::IMPORT_ANIMATION_KEEP_VALUE_TRACKS);
state.create_animations(true, true);
if (state.scene) {
memdelete(state.scene);
}
Expand All @@ -1716,12 +1748,6 @@ Ref<Animation> EditorSceneImporterCollada::import_animation(const String &p_path
return Ref<Animation>();
}
Ref<Animation> anim = state.animations[0];
String base = p_path.get_basename().to_lower();
if (p_flags & IMPORT_ANIMATION_DETECT_LOOP) {
if (base.begins_with("loop") || base.ends_with("loop") || base.begins_with("cycle") || base.ends_with("cycle")) {
anim->set_loop(true);
}
}

return anim;
}
Expand Down
2 changes: 1 addition & 1 deletion editor/import/resource_importer_obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ static Error _parse_obj(const String &p_path, List<Ref<Mesh>> &r_meshes, bool p_
Node *EditorOBJImporter::import_scene(const String &p_path, uint32_t p_flags, int p_bake_fps, List<String> *r_missing_deps, Error *r_err) {
List<Ref<Mesh>> meshes;

Error err = _parse_obj(p_path, meshes, false, p_flags & IMPORT_GENERATE_TANGENT_ARRAYS, p_flags & IMPORT_USE_COMPRESSION, Vector3(1, 1, 1), Vector3(0, 0, 0), r_missing_deps);
Error err = _parse_obj(p_path, meshes, false, p_flags & IMPORT_GENERATE_TANGENT_ARRAYS, 0, Vector3(1, 1, 1), Vector3(0, 0, 0), r_missing_deps);

if (err != OK) {
if (r_err) {
Expand Down
Loading