From a8c891566ece8c34a3fe224633b5b6c92605de4d Mon Sep 17 00:00:00 2001
From: Theo Hallenius <redsymbzone@hotmail.com>
Date: Sat, 2 Aug 2014 23:07:38 +0200
Subject: [PATCH 1/3] Merge animations during reimport

---
 .../io_plugins/editor_scene_import_plugin.cpp | 46 +++++++++++++++++--
 1 file changed, 42 insertions(+), 4 deletions(-)

diff --git a/tools/editor/io_plugins/editor_scene_import_plugin.cpp b/tools/editor/io_plugins/editor_scene_import_plugin.cpp
index 0b7ffd55f04..8b0d51093fd 100644
--- a/tools/editor/io_plugins/editor_scene_import_plugin.cpp
+++ b/tools/editor/io_plugins/editor_scene_import_plugin.cpp
@@ -1519,17 +1519,55 @@ void EditorSceneImportPlugin::_merge_existing_node(Node *p_node,Node *p_imported
 			} else if (p_node->get_type()=="AnimationPlayer") {
 				//for paths, overwrite path
 
-				AnimationPlayer *aplayer_imported =imported_node->cast_to<AnimationPlayer>();
-				AnimationPlayer *aplayer_node =p_node->cast_to<AnimationPlayer>();
+				AnimationPlayer *aplayer_imported = imported_node->cast_to<AnimationPlayer>();
+				AnimationPlayer *aplayer_node = p_node->cast_to<AnimationPlayer>();
 
 				//use imported bones, obviously
 				List<StringName> anims;
+				List<StringName> existing_anims;
 				aplayer_imported->get_animation_list(&anims);
+				aplayer_node->get_animation_list(&existing_anims);
 				//use imported animations, could merge some stuff though
-				for (List<StringName>::Element *E=anims.front();E;E=E->next()) {
+				for (List<StringName>::Element *N = anims.front(); N; N = N->next()) {
 
+					Ref<Animation> candidate = aplayer_imported->get_animation(N->get());
 
-					aplayer_node->add_animation(E->get(),aplayer_imported->get_animation(E->get()));
+					if (aplayer_node->has_animation(N->get())) {
+
+						Ref<Animation> found = aplayer_node->get_animation(N->get());
+
+						candidate->set_loop(found->has_loop());
+						candidate->set_step(found->get_step());
+
+						//For each track candidate
+						for (int i = 0; i < candidate->get_track_count(); i++) {
+
+							NodePath track_path = candidate->track_get_path(i);
+							// For each track existing
+							for (int x = 0; x < found->get_track_count(); x++) {
+
+								NodePath path_to_compare = found->track_get_path(x);
+								if (track_path == path_to_compare && candidate->track_get_type(x) == found->track_get_type(i)) {
+
+									//Tracks matches
+									candidate->track_set_interpolation_type(i, found->track_get_interpolation_type(x));
+									candidate->value_track_set_continuous(i, found->value_track_is_continuous(x));
+
+									//Key transitions might have changed, but the animation remained unchanged
+									if (candidate->track_get_key_count(x) == found->track_get_key_count(i)) {
+										for (int k = 0; k < candidate->track_get_key_count(x); k++) {
+											candidate->track_set_key_transition(x, k, found->track_get_key_transition(i, k));
+										}
+									}
+
+								}
+
+							}
+						}
+
+					}
+
+					aplayer_node->add_animation(N->get(), candidate);
 				}
 
 			} else if (p_node->get_type()=="CollisionShape") {

From 85bccf487cbc3d40b16d4cb23efa43fd75d6499f Mon Sep 17 00:00:00 2001
From: Theo Hallenius <redsymbzone@hotmail.com>
Date: Sun, 3 Aug 2014 01:10:22 +0200
Subject: [PATCH 2/3] Keep function callbacks, no throwing errors

---
 .../io_plugins/editor_scene_import_plugin.cpp | 35 +++++++++++++------
 1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/tools/editor/io_plugins/editor_scene_import_plugin.cpp b/tools/editor/io_plugins/editor_scene_import_plugin.cpp
index 8b0d51093fd..a3c24363284 100644
--- a/tools/editor/io_plugins/editor_scene_import_plugin.cpp
+++ b/tools/editor/io_plugins/editor_scene_import_plugin.cpp
@@ -1514,11 +1514,11 @@ void EditorSceneImportPlugin::_merge_existing_node(Node *p_node,Node *p_imported
 					skeleton_node->add_bone(skeleton_imported->get_bone_name(i));
 					skeleton_node->set_bone_parent(i,skeleton_imported->get_bone_parent(i));
 					skeleton_node->set_bone_rest(i,skeleton_imported->get_bone_rest(i));
-					skeleton_node->set_bone_pose(i,skeleton_imported->get_bone_pose(i));
+					//skeleton_node->set_bone_pose(i,skeleton_imported->get_bone_pose(i)); // not in a scene, will throw errors
 				}
-			} else if (p_node->get_type()=="AnimationPlayer") {
+			}
+			else if (p_node->get_type() == "AnimationPlayer") {
 				//for paths, overwrite path
-
 				AnimationPlayer *aplayer_imported = imported_node->cast_to<AnimationPlayer>();
 				AnimationPlayer *aplayer_node = p_node->cast_to<AnimationPlayer>();
 
@@ -1527,7 +1527,8 @@ void EditorSceneImportPlugin::_merge_existing_node(Node *p_node,Node *p_imported
 				List<StringName> existing_anims;
 				aplayer_imported->get_animation_list(&anims);
 				aplayer_node->get_animation_list(&existing_anims);
-				//use imported animations, could merge some stuff though
+
+				//use imported animations
 				for (List<StringName>::Element *N = anims.front(); N; N = N->next()) {
 
 					Ref<Animation> candidate = aplayer_imported->get_animation(N->get());
@@ -1547,16 +1548,21 @@ void EditorSceneImportPlugin::_merge_existing_node(Node *p_node,Node *p_imported
 							for (int x = 0; x < found->get_track_count(); x++) {
 
 								NodePath path_to_compare = found->track_get_path(x);
-								if (track_path == path_to_compare && candidate->track_get_type(x) == found->track_get_type(i)) {
+
+								if (track_path.hash() == path_to_compare.hash() && candidate->track_get_type(x) == found->track_get_type(i)) {
 
 									//Tracks matches
-									candidate->track_set_interpolation_type(i, found->track_get_interpolation_type(x));
-									candidate->value_track_set_continuous(i, found->value_track_is_continuous(x));
+									if (candidate->track_get_interpolation_type(i) != found->track_get_interpolation_type(x))
+										candidate->track_set_interpolation_type(i, found->track_get_interpolation_type(x));
+									if (candidate->track_get_type(i) == Animation::TYPE_VALUE && candidate->value_track_is_continuous(i) != found->value_track_is_continuous(x))
+										candidate->value_track_set_continuous(i, found->value_track_is_continuous(x));
 
 									//Key transitions might have changed, but the animation remained unchanged
-									if (candidate->track_get_key_count(x) == found->track_get_key_count(i)) {
-										for (int k = 0; k < candidate->track_get_key_count(x); k++) {
-											candidate->track_set_key_transition(x, k, found->track_get_key_transition(i, k));
+									if (candidate->track_get_key_count(i) == found->track_get_key_count(x)) {
+										for (int k = 0; k < candidate->track_get_key_count(i); k++) {
+
+											if (candidate->track_get_key_transition(i, k) != found->track_get_key_transition(x, k))
+												candidate->track_set_key_transition(i, k, found->track_get_key_transition(x, k));
 										}
 									}
 
@@ -1565,6 +1571,15 @@ void EditorSceneImportPlugin::_merge_existing_node(Node *p_node,Node *p_imported
 							}
 						}
 
+						// Append function callbacks
+						for (int x = 0; x < found->get_track_count(); x++) {
+							if (found->track_get_type(x) == Animation::TYPE_METHOD)
+								candidate->add_track(Animation::TYPE_METHOD, candidate->get_track_count());
+
+							for (int k = 0; k < found->track_get_key_count(x); k++) {
+								candidate->track_insert_key(x, found->track_get_key_time(x, k), found->track_get_key_value(x, k), found->track_get_key_transition(x, k));
+							}
+						}
 					}
 
 					aplayer_node->add_animation(N->get(), candidate);

From 0dd29d0512a4e28f83f7b4529fccf9f126982a5d Mon Sep 17 00:00:00 2001
From: Theo Hallenius <redsymbzone@hotmail.com>
Date: Fri, 15 Aug 2014 03:35:31 +0200
Subject: [PATCH 3/3] Forgot about keeping value tracks.

---
 tools/editor/io_plugins/editor_scene_import_plugin.cpp | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/tools/editor/io_plugins/editor_scene_import_plugin.cpp b/tools/editor/io_plugins/editor_scene_import_plugin.cpp
index a3c24363284..99a2326b4b5 100644
--- a/tools/editor/io_plugins/editor_scene_import_plugin.cpp
+++ b/tools/editor/io_plugins/editor_scene_import_plugin.cpp
@@ -1571,14 +1571,13 @@ void EditorSceneImportPlugin::_merge_existing_node(Node *p_node,Node *p_imported
 							}
 						}
 
-						// Append function callbacks
+						// Append function callbacks and values
 						for (int x = 0; x < found->get_track_count(); x++) {
-							if (found->track_get_type(x) == Animation::TYPE_METHOD)
-								candidate->add_track(Animation::TYPE_METHOD, candidate->get_track_count());
+							if (found->track_get_type(x) == Animation::TYPE_METHOD || found->track_get_type(x) == Animation::TYPE_VALUE)
+								candidate->add_track(found->track_get_type(x), candidate->get_track_count());
 
-							for (int k = 0; k < found->track_get_key_count(x); k++) {
+							for (int k = 0; k < found->track_get_key_count(x); k++)
 								candidate->track_insert_key(x, found->track_get_key_time(x, k), found->track_get_key_value(x, k), found->track_get_key_transition(x, k));
-							}
 						}
 					}