[Bf-extensions-cvs] [c715490c] master: glTF import: add in active collection, if any

Julien Duroure noreply at git.blender.org
Thu Feb 28 22:04:18 CET 2019


Commit: c715490c5ee1d221ba114c219337f1df8a0cb653
Author: Julien Duroure
Date:   Thu Feb 28 21:17:22 2019 +0100
Branches: master
https://developer.blender.org/rBAc715490c5ee1d221ba114c219337f1df8a0cb653

glTF import: add in active collection, if any

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

M	io_scene_gltf2/blender/imp/gltf2_blender_camera.py
M	io_scene_gltf2/blender/imp/gltf2_blender_light.py
M	io_scene_gltf2/blender/imp/gltf2_blender_node.py
M	io_scene_gltf2/blender/imp/gltf2_blender_scene.py
M	io_scene_gltf2/blender/imp/gltf2_blender_skin.py

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

diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_camera.py b/io_scene_gltf2/blender/imp/gltf2_blender_camera.py
index b5a10ac5..2c67ba95 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_camera.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_camera.py
@@ -42,6 +42,9 @@ class BlenderCamera():
             cam.clip_end = pycamera.zfar
 
         obj = bpy.data.objects.new(pycamera.name, cam)
-        bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj)
+        if gltf.blender_active_collection is not None:
+            bpy.data.collections[gltf.blender_active_collection].objects.link(obj)
+        else:
+            bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj)
         return obj
 
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_light.py b/io_scene_gltf2/blender/imp/gltf2_blender_light.py
index 5d7cb355..38bf7777 100644
--- a/io_scene_gltf2/blender/imp/gltf2_blender_light.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_light.py
@@ -40,7 +40,10 @@ class BlenderLight():
 
         # TODO range
 
-        bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj)
+        if gltf.blender_active_collection is not None:
+            bpy.data.collections[gltf.blender_active_collection].objects.link(obj)
+        else:
+            bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj)
 
         return obj
 
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_node.py b/io_scene_gltf2/blender/imp/gltf2_blender_node.py
index b707878c..e7f68dca 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_node.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_node.py
@@ -73,7 +73,10 @@ class BlenderNode():
 
             obj = bpy.data.objects.new(name, mesh)
             obj.rotation_mode = 'QUATERNION'
-            bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj)
+            if gltf.blender_active_collection is not None:
+                bpy.data.collections[gltf.blender_active_collection].objects.link(obj)
+            else:
+                bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj)
 
             # Transforms apply only if this mesh is not skinned
             # See implementation node of gltf2 specification
@@ -148,7 +151,11 @@ class BlenderNode():
             gltf.log.info("Blender create Empty node")
             obj = bpy.data.objects.new("Node", None)
         obj.rotation_mode = 'QUATERNION'
-        bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj)
+        if gltf.blender_active_collection is not None:
+            bpy.data.collections[gltf.blender_active_collection].objects.link(obj)
+        else:
+            bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj)
+
         BlenderNode.set_transforms(gltf, node_idx, pynode, obj, parent)
         pynode.blender_object = obj.name
         BlenderNode.set_parent(gltf, obj, parent)
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
index cf8f58a1..d21cdf33 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_scene.py
@@ -28,6 +28,7 @@ class BlenderScene():
     @staticmethod
     def create(gltf, scene_idx):
         """Scene creation."""
+        gltf.blender_active_collection = None
         if scene_idx is not None:
             pyscene = gltf.data.scenes[scene_idx]
             list_nodes = pyscene.nodes
@@ -38,6 +39,8 @@ class BlenderScene():
                 # 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
+                if bpy.context.collection.name in bpy.data.collections: # avoid master collection
+                    gltf.blender_active_collection = bpy.context.collection.name
                 scene.render.engine = "BLENDER_EEVEE"
 
                 gltf.blender_scene = scene.name
@@ -46,11 +49,15 @@ class BlenderScene():
 
             # Switch to newly created main scene
             bpy.context.window.scene = bpy.data.scenes[gltf.blender_scene]
+            if bpy.context.collection.name in bpy.data.collections: # avoid master collection
+                gltf.blender_active_collection = bpy.context.collection.name
 
         else:
             # No scene in glTF file, create all objects in current scene
             scene = bpy.context.scene
             scene.render.engine = "BLENDER_EEVEE"
+            if bpy.context.collection.name in bpy.data.collections: # avoid master collection
+                gltf.blender_active_collection = bpy.context.collection.name
             gltf.blender_scene = scene.name
             list_nodes = BlenderScene.get_root_nodes(gltf)
 
@@ -59,7 +66,10 @@ class BlenderScene():
         obj_rotation.rotation_mode = 'QUATERNION'
         obj_rotation.rotation_quaternion = Quaternion((sqrt(2) / 2, sqrt(2) / 2, 0.0, 0.0))
 
-        bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj_rotation)
+        if gltf.blender_active_collection is not None:
+            bpy.data.collections[gltf.blender_active_collection].objects.link(obj_rotation)
+        else:
+            bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj_rotation)
 
         if list_nodes is not None:
             for node_idx in list_nodes:
@@ -130,7 +140,7 @@ class BlenderScene():
                     bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')
 
                 # remove object
-                bpy.context.scene.collection.objects.unlink(obj_rotation)
+                #bpy.context.scene.collection.objects.unlink(obj_rotation)
                 bpy.data.objects.remove(obj_rotation)
 
     @staticmethod
diff --git a/io_scene_gltf2/blender/imp/gltf2_blender_skin.py b/io_scene_gltf2/blender/imp/gltf2_blender_skin.py
index b2b39a2b..9dbdb7a8 100755
--- a/io_scene_gltf2/blender/imp/gltf2_blender_skin.py
+++ b/io_scene_gltf2/blender/imp/gltf2_blender_skin.py
@@ -36,7 +36,11 @@ class BlenderSkin():
 
         armature = bpy.data.armatures.new(name)
         obj = bpy.data.objects.new(name, armature)
-        bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj)
+        if gltf.blender_active_collection is not None:
+            bpy.data.collections[gltf.blender_active_collection].objects.link(obj)
+        else:
+            bpy.data.scenes[gltf.blender_scene].collection.objects.link(obj)
+            
         pyskin.blender_armature_name = obj.name
         if parent is not None:
             obj.parent = bpy.data.objects[gltf.data.nodes[parent].blender_object]



More information about the Bf-extensions-cvs mailing list