[Bf-extensions-cvs] [862b0bbd] master: glTF importer: fix bug when glTF has no scene

Julien Duroure noreply at git.blender.org
Tue Jan 22 21:50:52 CET 2019


Commit: 862b0bbd7d319104e7e5f8ab9576daa97be3298d
Author: Julien Duroure
Date:   Tue Jan 22 21:49:55 2019 +0100
Branches: master
https://developer.blender.org/rBA862b0bbd7d319104e7e5f8ab9576daa97be3298d

glTF importer: fix bug when glTF has no scene

In that case, create all nodes in current scene

===================================================================

M	io_scene_gltf2/blender/imp/gltf2_blender_gltf.py
M	io_scene_gltf2/blender/imp/gltf2_blender_scene.py

===================================================================

diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_gltf.py b/io_scene_gltf2/blender/imp/gltf2_blender_gltf.py
index e4cb65f1..bdf8e7f6 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_gltf.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_gltf.py
@@ -28,8 +28,13 @@ class BlenderGlTF():
         bpy.context.scene.render.engine = 'BLENDER_EEVEE'
         BlenderGlTF.pre_compute(gltf)
 
-        for scene_idx, scene in enumerate(gltf.data.scenes):
-            BlenderScene.create(gltf, scene_idx)
+        if gltf.data.scenes is not None:
+            for scene_idx, scene in enumerate(gltf.data.scenes):
+                BlenderScene.create(gltf, scene_idx)
+        else:
+            # special case where there is no scene in glTF file
+            # generate all objects in current scene
+            BlenderScene.create(gltf, None)
 
         # Armature correction
         # Try to detect bone chains, and set bone lengths
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
index 6c2f14cb..902cc290 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
@@ -28,22 +28,31 @@ class BlenderScene():
     @staticmethod
     def create(gltf, scene_idx):
         """Scene creation."""
-        pyscene = gltf.data.scenes[scene_idx]
+        if scene_idx is not None:
+            pyscene = gltf.data.scenes[scene_idx]
+            list_nodes = pyscene.nodes
 
-    # Create a new scene only if not already exists in .blend file
-    # TODO : put in current scene instead ?
-        if pyscene.name not in [scene.name for scene in bpy.data.scenes]:
-            # TODO: There is a bug in 2.8 alpha that break CLEAR_KEEP_TRANSFORM
-            # if we are creating a new scene
+            # Create a new scene only if not already exists in .blend file
+            # TODO : put in current scene instead ?
+            if pyscene.name not in [scene.name for scene in bpy.data.scenes]:
+                # TODO: There is a bug in 2.8 alpha that break CLEAR_KEEP_TRANSFORM
+                # if we are creating a new scene
+                scene = bpy.context.scene
+                scene.render.engine = "BLENDER_EEVEE"
+
+                gltf.blender_scene = scene.name
+            else:
+                gltf.blender_scene = pyscene.name
+
+            # Switch to newly created main scene
+            bpy.context.window.scene = bpy.data.scenes[gltf.blender_scene]
+
+        else:
+            # No scene in glTF file, create all objects in current scene
             scene = bpy.context.scene
             scene.render.engine = "BLENDER_EEVEE"
-
             gltf.blender_scene = scene.name
-        else:
-            gltf.blender_scene = pyscene.name
-
-        # Switch to newly created main scene
-        bpy.context.window.scene = bpy.data.scenes[gltf.blender_scene]
+            list_nodes = BlenderScene.get_root_nodes(gltf)
 
         # Create Yup2Zup empty
         obj_rotation = bpy.data.objects.new("Yup2Zup", None)
@@ -52,8 +61,8 @@ class BlenderScene():
 
         bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj_rotation)
 
-        if pyscene.nodes is not None:
-            for node_idx in pyscene.nodes:
+        if list_nodes is not None:
+            for node_idx in list_nodes:
                 BlenderNode.create(gltf, node_idx, None)  # None => No parent
 
         # Now that all mesh / bones are created, create vertex groups on mesh
@@ -72,19 +81,19 @@ class BlenderScene():
 
         if gltf.data.animations:
             for anim_idx, anim in enumerate(gltf.data.animations):
-                if pyscene.nodes is not None:
-                    for node_idx in pyscene.nodes:
+                if list_nodes is not None:
+                    for node_idx in list_nodes:
                         BlenderAnimation.anim(gltf, anim_idx, node_idx)
 
         # Parent root node to rotation object
-        if pyscene.nodes is not None:
-            for node_idx in pyscene.nodes:
+        if list_nodes is not None:
+            for node_idx in list_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 node_idx in list_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)
@@ -96,3 +105,22 @@ class BlenderScene():
                 bpy.context.scene.collection.objects.unlink(obj_rotation)
                 bpy.data.objects.remove(obj_rotation)
 
+    @staticmethod
+    def get_root_nodes(gltf):
+        if gltf.data.nodes is None:
+            return None
+
+        parents = {}
+        for idx, node  in enumerate(gltf.data.nodes):
+            pynode = gltf.data.nodes[idx]
+            if pynode.children:
+                for child_idx in pynode.children:
+                    parents[child_idx] = idx
+
+        roots = []
+        for idx, node in enumerate(gltf.data.nodes):
+            if idx not in parents.keys():
+                roots.append(idx)
+
+        return roots
+



More information about the Bf-extensions-cvs mailing list