From f89d1c9581c03160485a7b4b09fa5d538331fdeb Mon Sep 17 00:00:00 2001
From: Julien Duroure <julien.duroure@gmail.com>
Date: Sun, 25 Nov 2018 16:26:12 +0100
Subject: [PATCH] Fix orientation issue in object animation

---
 .../imp/gltf2_blender_animation_bone.py       |  2 +-
 .../imp/gltf2_blender_animation_node.py       |  6 +++++-
 .../blender/imp/gltf2_blender_gltf.py         |  4 ++++
 .../blender/imp/gltf2_blender_scene.py        | 20 ++++++++++---------
 4 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_animation_bone.py b/io_scene_gltf2/blender/imp/gltf2_blender_animation_bone.py
index ec95692bc..c08849661 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_animation_bone.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation_bone.py
@@ -172,7 +172,7 @@ class BlenderBoneAnim():
             action = bpy.data.actions[name]
             # Check if this action has some users.
             # If no user (only 1 indeed), that means that this action must be deleted
-            # (is an action from a deleted action)
+            # (is an action from a deleted object)
             if action.users == 1:
                 bpy.data.actions.remove(action)
                 action = bpy.data.actions.new(name)
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py b/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
index 2a04f4690..9302afd57 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_animation_node.py
@@ -55,7 +55,7 @@ class BlenderNodeAnim():
         action = bpy.data.actions.new(name)
         # Check if this action has some users.
         # If no user (only 1 indeed), that means that this action must be deleted
-        # (is an action from a deleted action)
+        # (is an action from a deleted object)
         if action.users == 1:
             bpy.data.actions.remove(action)
             action = bpy.data.actions.new(name)
@@ -63,6 +63,10 @@ class BlenderNodeAnim():
             obj.animation_data_create()
         obj.animation_data.action = bpy.data.actions[action.name]
 
+        # There is an animation on object
+        # We can't remove Yup2Zup oject
+        gltf.animation_object = True
+
         for channel_idx in node.animations[anim_idx]:
             channel = animation.channels[channel_idx]
 
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_gltf.py b/io_scene_gltf2/blender/imp/gltf2_blender_gltf.py
index 0c819f9c9..9afc12ded 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_gltf.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_gltf.py
@@ -77,6 +77,10 @@ class BlenderGlTF():
         # default scene used
         gltf.blender_scene = None
 
+        # Check if there is animation on object
+        # Init is to False, and will be set to True during creation
+        gltf.animation_object = False
+
         # Blender material
         if gltf.data.materials:
             for material in gltf.data.materials:
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
index 14310fb8f..c155b1155 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
@@ -80,16 +80,18 @@ class BlenderScene():
             for node_idx in pyscene.nodes:
                 bpy.data.objects[gltf.data.nodes[node_idx].blender_object].parent = obj_rotation
 
+            if gltf.animation_object is False:
 
-            for node_idx in pyscene.nodes:
-                for obj_ in bpy.context.scene.objects:
-                    obj_.select_set(False)
-                bpy.data.objects[gltf.data.nodes[node_idx].blender_object].select_set(True)
-                bpy.context.view_layer.objects.active = bpy.data.objects[gltf.data.nodes[node_idx].blender_object]
 
-                bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')
+                for node_idx in pyscene.nodes:
+                    for obj_ in bpy.context.scene.objects:
+                        obj_.select_set(False)
+                    bpy.data.objects[gltf.data.nodes[node_idx].blender_object].select_set(True)
+                    bpy.context.view_layer.objects.active = bpy.data.objects[gltf.data.nodes[node_idx].blender_object]
+
+                    bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')
 
-            # remove object
-            bpy.context.scene.collection.objects.unlink(obj_rotation)
-            bpy.data.objects.remove(obj_rotation)
+                # remove object
+                bpy.context.scene.collection.objects.unlink(obj_rotation)
+                bpy.data.objects.remove(obj_rotation)
 
-- 
GitLab