Repository: khronosgroup/gltf-blender-io
Issue: Sample animation issue when using NLA

Issue about animation that occurs when we have to sample animation (forced by user in option, or when needs_sample return true)

Because in gltf2_blender_gather_animation_sampler_keyframes.py file, we check value of  pose_bone_if_armature.matrix_basis.decompose()

BUT:
- this value is always the same if we stash animations, and there is no active animation. See animation_case_1 file
- this value is false, and is taken from active animation. That leads to export always the same animation, even if there are different animations stashed. See animation_case_2_file

[bugs_animations.zip](https://github.com/KhronosGroup/glTF-Blender-IO/files/3173283/bugs_animations.zip)


Changed files:
addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animations.py
addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py
addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channels.py

diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channels.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channels.py
index 57f45afe..1bfc3226 100644
--- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channels.py
+++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_channels.py
@@ -59,11 +59,12 @@ def gather_animation_channels(blender_action: bpy.types.Action,
                     bone.name,
                     p,
                     bake_range_start,
-                    bake_range_end)
+                    bake_range_end,
+                    blender_action.name)
                 channels.append(channel)
     else:
         for channel_group in __get_channel_groups(blender_action, blender_object, export_settings):
-            channel = __gather_animation_channel(channel_group, blender_object, export_settings, None, None, None, None)
+            channel = __gather_animation_channel(channel_group, blender_object, export_settings, None, None, None, None, blender_action.name)
             if channel is not None:
                 channels.append(channel)
 
@@ -76,7 +77,8 @@ def __gather_animation_channel(channels: typing.Tuple[bpy.types.FCurve],
                                bake_bone: typing.Union[str, None],
                                bake_channel: typing.Union[str, None],
                                bake_range_start,
-                               bake_range_end
+                               bake_range_end,
+                               action_name: str
                                ) -> typing.Union[gltf2_io.AnimationChannel, None]:
     if not __filter_animation_channel(channels, blender_object, export_settings):
         return None
@@ -84,7 +86,7 @@ def __gather_animation_channel(channels: typing.Tuple[bpy.types.FCurve],
     return gltf2_io.AnimationChannel(
         extensions=__gather_extensions(channels, blender_object, export_settings, bake_bone),
         extras=__gather_extras(channels, blender_object, export_settings, bake_bone),
-        sampler=__gather_sampler(channels, blender_object, export_settings, bake_bone, bake_channel, bake_range_start, bake_range_end),
+        sampler=__gather_sampler(channels, blender_object, export_settings, bake_bone, bake_channel, bake_range_start, bake_range_end, action_name),
         target=__gather_target(channels, blender_object, export_settings, bake_bone, bake_channel)
     )
 
@@ -118,7 +120,8 @@ def __gather_sampler(channels: typing.Tuple[bpy.types.FCurve],
                      bake_bone: typing.Union[str, None],
                      bake_channel: typing.Union[str, None],
                      bake_range_start,
-                     bake_range_end
+                     bake_range_end,
+                     action_name
                      ) -> gltf2_io.AnimationSampler:
     return gltf2_blender_gather_animation_samplers.gather_animation_sampler(
         channels,
@@ -127,6 +130,7 @@ def __gather_sampler(channels: typing.Tuple[bpy.types.FCurve],
         bake_channel,
         bake_range_start,
         bake_range_end,
+        action_name,
         export_settings
     )
 
diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py
index dbdd7816..1e8dc734 100644
--- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py
+++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_sampler_keyframes.py
@@ -114,6 +114,7 @@ def gather_keyframes(blender_object_if_armature: typing.Optional[bpy.types.Objec
                      bake_channel: typing.Union[str, None],
                      bake_range_start,
                      bake_range_end,
+                     action_name: str,
                      export_settings
                      ) -> typing.List[Keyframe]:
     """Convert the blender action groups' fcurves to keyframes for use in glTF."""
diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
index c30fa912..7d8ab6de 100644
--- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
+++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animation_samplers.py
@@ -36,6 +36,7 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve],
                              bake_channel: typing.Union[str, None],
                              bake_range_start,
                              bake_range_end,
+                             action_name: str,
                              export_settings
                              ) -> gltf2_io.AnimationSampler:
 
@@ -57,7 +58,8 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve],
     return gltf2_io.AnimationSampler(
         extensions=__gather_extensions(channels, blender_object_if_armature, export_settings, bake_bone, bake_channel),
         extras=__gather_extras(channels, blender_object_if_armature, export_settings, bake_bone, bake_channel),
-        input=__gather_input(channels, blender_object_if_armature, non_keyed_values, bake_bone, bake_channel, bake_range_start, bake_range_end, export_settings),
+        input=__gather_input(channels, blender_object_if_armature, non_keyed_values,
+                             bake_bone, bake_channel, bake_range_start, bake_range_end, action_name, export_settings),
         interpolation=__gather_interpolation(channels, blender_object_if_armature, export_settings, bake_bone, bake_channel),
         output=__gather_output(channels, blender_object.matrix_parent_inverse.copy().freeze(),
                                blender_object_if_armature,
@@ -66,6 +68,7 @@ def gather_animation_sampler(channels: typing.Tuple[bpy.types.FCurve],
                                bake_channel,
                                bake_range_start,
                                bake_range_end,
+                               action_name,
                                export_settings)
     )
 
@@ -166,6 +169,7 @@ def __gather_input(channels: typing.Tuple[bpy.types.FCurve],
                    bake_channel: typing.Union[str, None],
                    bake_range_start,
                    bake_range_end,
+                   action_name,
                    export_settings
                    ) -> gltf2_io.Accessor:
     """Gather the key time codes."""
@@ -176,6 +180,7 @@ def __gather_input(channels: typing.Tuple[bpy.types.FCurve],
                                                                                   bake_channel,
                                                                                   bake_range_start,
                                                                                   bake_range_end,
+                                                                                  action_name,
                                                                                   export_settings)
     times = [k.seconds for k in keyframes]
 
@@ -225,6 +230,7 @@ def __gather_output(channels: typing.Tuple[bpy.types.FCurve],
                     bake_channel: typing.Union[str, None],
                     bake_range_start,
                     bake_range_end,
+                    action_name,
                     export_settings
                     ) -> gltf2_io.Accessor:
     """Gather the data of the keyframes."""
@@ -235,6 +241,7 @@ def __gather_output(channels: typing.Tuple[bpy.types.FCurve],
                                                                                   bake_channel,
                                                                                   bake_range_start,
                                                                                   bake_range_end,
+                                                                                  action_name,
                                                                                   export_settings)
     if bake_bone is not None:
         target_datapath = "pose.bones['" + bake_bone + "']." + bake_channel
diff --git a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animations.py b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animations.py
index f172f174..d46c6ed4 100644
--- a/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animations.py
+++ b/addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_animations.py
@@ -33,12 +33,27 @@ def gather_animations(blender_object: bpy.types.Object, export_settings) -> typi
     # Collect all 'actions' affecting this object. There is a direct mapping between blender actions and glTF animations
     blender_actions = __get_blender_actions(blender_object)
 
+    # save the current active action of the object, if any
+    # We will restore it after export
+    current_action = None
+    if blender_object.animation_data and blender_object.animation_data.action:
+        current_action = blender_object.animation_data.action
+
     # Export all collected actions.
     for blender_action in blender_actions:
+
+        # Set action as active, to be able to bake if needed
+        if blender_object.animation_data: # Not for shapekeys!
+            blender_object.animation_data.action = blender_action
+
         animation = __gather_animation(blender_action, blender_object, export_settings)
         if animation is not None:
             animations.append(animation)
 
+    # Restore current action
+    if blender_object.animation_data:
+        blender_object.animation_data.action = current_action
+
     return animations