From 0db02f26b66ba4e3ac671607ba85fc794ae698fa Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Mon, 20 Jun 2016 00:19:04 -0300 Subject: -Added options for scene import plugin to keep materials and user-added tracks to animations, please test! closes #4425 --- tools/editor/io_plugins/editor_import_collada.cpp | 6 + .../io_plugins/editor_scene_import_plugin.cpp | 160 ++++++++++++++++++++- .../editor/io_plugins/editor_scene_import_plugin.h | 8 ++ 3 files changed, 172 insertions(+), 2 deletions(-) (limited to 'tools/editor/io_plugins') diff --git a/tools/editor/io_plugins/editor_import_collada.cpp b/tools/editor/io_plugins/editor_import_collada.cpp index 80cd54756e..363cba3678 100644 --- a/tools/editor/io_plugins/editor_import_collada.cpp +++ b/tools/editor/io_plugins/editor_import_collada.cpp @@ -2077,6 +2077,7 @@ void ColladaImport::create_animation(int p_clip, bool p_make_tracks_in_all_bones animation->add_track(Animation::TYPE_TRANSFORM); int track = animation->get_track_count() -1; animation->track_set_path( track , path ); + animation->track_set_imported( track , true ); //helps merging later Vector snapshots = base_snapshots; @@ -2229,6 +2230,7 @@ void ColladaImport::create_animation(int p_clip, bool p_make_tracks_in_all_bones animation->add_track(Animation::TYPE_TRANSFORM); int track = animation->get_track_count() -1; animation->track_set_path( track , path ); + animation->track_set_imported( track , true ); //helps merging later Transform xform = cn->compute_transform(collada); @@ -2284,8 +2286,11 @@ void ColladaImport::create_animation(int p_clip, bool p_make_tracks_in_all_bones animation->add_track(Animation::TYPE_VALUE); int track = animation->get_track_count() -1; + path = path +":"+at.param; animation->track_set_path( track , path ); + animation->track_set_imported( track , true ); //helps merging later + for(int i=0;iset_name("animations"); for(int i=0;iget_name()=="") diff --git a/tools/editor/io_plugins/editor_scene_import_plugin.cpp b/tools/editor/io_plugins/editor_scene_import_plugin.cpp index ed766c6598..c7d92a9658 100644 --- a/tools/editor/io_plugins/editor_scene_import_plugin.cpp +++ b/tools/editor/io_plugins/editor_scene_import_plugin.cpp @@ -1069,11 +1069,13 @@ const EditorSceneImportDialog::FlagInfo EditorSceneImportDialog::scene_flag_name {EditorSceneImportPlugin::SCENE_FLAG_IMPORT_ANIMATIONS,("Actions"),"Import Animations",true}, {EditorSceneImportPlugin::SCENE_FLAG_COMPRESS_GEOMETRY,("Actions"),"Compress Geometry",false}, {EditorSceneImportPlugin::SCENE_FLAG_GENERATE_TANGENT_ARRAYS,("Actions"),"Force Generation of Tangent Arrays",false}, - {EditorSceneImportPlugin::SCENE_FLAG_DETECT_ALPHA,("Materials"),"Set Alpha in Materials (-alpha)",true}, - {EditorSceneImportPlugin::SCENE_FLAG_DETECT_VCOLOR,("Materials"),"Set Vert. Color in Materials (-vcol)",true}, {EditorSceneImportPlugin::SCENE_FLAG_LINEARIZE_DIFFUSE_TEXTURES,("Actions"),"SRGB->Linear Of Diffuse Textures",false}, {EditorSceneImportPlugin::SCENE_FLAG_CONVERT_NORMALMAPS_TO_XY,("Actions"),"Convert Normal Maps to XY",true}, {EditorSceneImportPlugin::SCENE_FLAG_SET_LIGHTMAP_TO_UV2_IF_EXISTS,("Actions"),"Set Material Lightmap to UV2 if Tex2Array Exists",true}, + {EditorSceneImportPlugin::SCENE_FLAG_MERGE_KEEP_MATERIALS,("Merge"),"Keep Materials after first import (delete them for re-import).",true}, + {EditorSceneImportPlugin::SCENE_FLAG_MERGE_KEEP_EXTRA_ANIM_TRACKS,("Merge"),"Keep user-added Animation tracks.",true}, + {EditorSceneImportPlugin::SCENE_FLAG_DETECT_ALPHA,("Materials"),"Set Alpha in Materials (-alpha)",true}, + {EditorSceneImportPlugin::SCENE_FLAG_DETECT_VCOLOR,("Materials"),"Set Vert. Color in Materials (-vcol)",true}, {EditorSceneImportPlugin::SCENE_FLAG_CREATE_COLLISIONS,("Create"),"Create Collisions and/or Rigid Bodies (-col,-colonly,-rigid)",true}, {EditorSceneImportPlugin::SCENE_FLAG_CREATE_PORTALS,("Create"),"Create Portals (-portal)",true}, {EditorSceneImportPlugin::SCENE_FLAG_CREATE_ROOMS,("Create"),"Create Rooms (-room)",true}, @@ -2455,6 +2457,138 @@ void EditorSceneImportPlugin::_optimize_animations(Node *scene, float p_max_lin_ } +void EditorSceneImportPlugin::_find_resources_to_merge(Node *scene, Node *node, bool p_merge_material, Map > &materials, bool p_merge_anims, Map >& merged_anims,Set > &tested_meshes) { + + if (node->get_owner()!=scene) + return; + + String path = scene->get_path_to(node); + + if (p_merge_anims && node->cast_to()) { + + AnimationPlayer *ap = node->cast_to(); + List anims; + ap->get_animation_list(&anims); + for (List::Element *E=anims.front();E;E=E->next()) { + Ref anim = ap->get_animation(E->get()); + Ref clone; + + bool has_user_tracks=false; + + for(int i=0;iget_track_count();i++) { + + if (!anim->track_is_imported(i)) { + has_user_tracks=true; + break; + } + } + + if (has_user_tracks) { + + clone = anim->duplicate(); + for(int i=0;iget_track_count();i++) { + if (clone->track_is_imported(i)) { + clone->remove_track(i); + i--; + } + } + + merged_anims[path+"::"+String(E->get())]=clone; + } + } + } + + + + if (p_merge_material && node->cast_to()) { + MeshInstance *mi=node->cast_to(); + Ref mesh = mi->get_mesh(); + if (mesh.is_valid() && mesh->get_name()!=String() && !tested_meshes.has(mesh)) { + + for(int i=0;iget_surface_count();i++) { + Ref material = mesh->surface_get_material(i); + materials[mesh->get_name()+":surf:"+mesh->surface_get_name(i)]=material; + } + + tested_meshes.insert(mesh); + } + } + + + + for(int i=0;iget_child_count();i++) { + _find_resources_to_merge(scene,node->get_child(i),p_merge_material,materials,p_merge_anims,merged_anims,tested_meshes); + } + +} + + +void EditorSceneImportPlugin::_merge_found_resources(Node *scene, Node *node, bool p_merge_material, const Map > &materials, bool p_merge_anims, const Map >& merged_anims, Set > &tested_meshes) { + + if (node->get_owner()!=scene) + return; + + String path = scene->get_path_to(node); + + if (node->cast_to()) { + + AnimationPlayer *ap = node->cast_to(); + List anims; + ap->get_animation_list(&anims); + for (List::Element *E=anims.front();E;E=E->next()) { + Ref anim = ap->get_animation(E->get()); + + String anim_path = path+"::"+String(E->get()); + + if (merged_anims.has(anim_path)) { + + Ref user_tracks = merged_anims[anim_path]; + for(int i=0;iget_track_count();i++) { + + int idx = anim->get_track_count(); + anim->add_track(user_tracks->track_get_type(i)); + anim->track_set_path(idx,user_tracks->track_get_path(i)); + anim->track_set_interpolation_type(idx,user_tracks->track_get_interpolation_type(i)); + for(int j=0;jtrack_get_key_count(i);j++) { + + float ofs = user_tracks->track_get_key_time(i,j); + float trans = user_tracks->track_get_key_transition(i,j); + Variant value = user_tracks->track_get_key_value(i,j); + + anim->track_insert_key(idx,ofs,value,trans); + } + } + } + } + } + + + + if (node->cast_to()) { + MeshInstance *mi=node->cast_to(); + Ref mesh = mi->get_mesh(); + if (mesh.is_valid() && mesh->get_name()!=String() && !tested_meshes.has(mesh)) { + + for(int i=0;iget_surface_count();i++) { + String sname = mesh->get_name()+":surf:"+mesh->surface_get_name(i); + + if (materials.has(sname)) { + mesh->surface_set_material(i,materials[sname]); + } + } + + tested_meshes.insert(mesh); + } + } + + + + for(int i=0;iget_child_count();i++) { + _merge_found_resources(scene,node->get_child(i),p_merge_material,materials,p_merge_anims,merged_anims,tested_meshes); + } + +} + Error EditorSceneImportPlugin::import2(Node *scene, const String& p_dest_path, const Ref& p_from) { Error err=OK; @@ -2506,6 +2640,28 @@ Error EditorSceneImportPlugin::import2(Node *scene, const String& p_dest_path, c _filter_tracks(scene,animation_filter); + if (scene_flags&(SCENE_FLAG_MERGE_KEEP_MATERIALS|SCENE_FLAG_MERGE_KEEP_EXTRA_ANIM_TRACKS) && FileAccess::exists(p_dest_path)) { + //must merge! + + Ref pscene = ResourceLoader::load(p_dest_path,"PackedScene",true); + if (pscene.is_valid()) { + + Node *instance = pscene->instance(); + if (instance) { + Map > merged_anims; + Map > merged_materials; + Set > tested_meshes; + + _find_resources_to_merge(instance,instance,scene_flags&SCENE_FLAG_MERGE_KEEP_MATERIALS,merged_materials,scene_flags&SCENE_FLAG_MERGE_KEEP_EXTRA_ANIM_TRACKS,merged_anims,tested_meshes); + tested_meshes.clear(); + _merge_found_resources(instance,instance,scene_flags&SCENE_FLAG_MERGE_KEEP_MATERIALS,merged_materials,scene_flags&SCENE_FLAG_MERGE_KEEP_EXTRA_ANIM_TRACKS,merged_anims,tested_meshes); + + memdelete(instance); + } + + } + + } /// BEFORE ANYTHING, RUN SCRIPT diff --git a/tools/editor/io_plugins/editor_scene_import_plugin.h b/tools/editor/io_plugins/editor_scene_import_plugin.h index 8a2d30f1f6..c31d3a33d3 100644 --- a/tools/editor/io_plugins/editor_scene_import_plugin.h +++ b/tools/editor/io_plugins/editor_scene_import_plugin.h @@ -116,6 +116,10 @@ class EditorSceneImportPlugin : public EditorImportPlugin { void _tag_import_paths(Node *p_scene,Node *p_node); + void _find_resources_to_merge(Node *scene, Node *node, bool p_merge_material, Map >&materials, bool p_merge_anims, Map >& merged_anims, Set > &tested_meshes); + void _merge_found_resources(Node *scene, Node *node, bool p_merge_material, const Map > &materials, bool p_merge_anims, const Map >& merged_anims, Set > &tested_meshes); + + public: enum SceneFlags { @@ -134,6 +138,9 @@ public: SCENE_FLAG_CREATE_NAVMESH=1<<17, SCENE_FLAG_DETECT_LIGHTMAP_LAYER=1<<18, + SCENE_FLAG_MERGE_KEEP_MATERIALS=1<<20, + SCENE_FLAG_MERGE_KEEP_EXTRA_ANIM_TRACKS=1<<21, + SCENE_FLAG_REMOVE_NOIMP=1<<24, SCENE_FLAG_IMPORT_ANIMATIONS=1<<25, SCENE_FLAG_COMPRESS_GEOMETRY=1<<26, @@ -144,6 +151,7 @@ public: }; + virtual String get_name() const; virtual String get_visible_name() const; virtual void import_dialog(const String& p_from=""); -- cgit v1.2.3