I’m having trouble updating a script that worked in Blender 2.7 over to 2.8. Since Scene Layers have been changed over to Collections. The script takes all the OBJ files from a location and brings them into Blender, which is working great.

The issue is after the objects are added to their collections there is also another "linked" copy in the Outliner.

Why is this a problem? I want to hide all the collections apart from the first one. Having these linked objects means when I try and hide the collection the object(s) stay visible.

Does anyone know how I can import objects into a collection without it linking a new copy?

I tried bpy.ops.object.move_to_collection function instead of linking objects. But I could not make it work in my script.

Here is the script:

import bpy
import os
import glob

directory_im = 'C:/Users/mila/OneDrive/Desktop/OBJ for test/'
files = glob.glob(directory_im + "*.obj")
for f in files:
    head, tail = os.path.split(f)
    collection_name = tail.replace('.obj', '')
    myCol = bpy.data.collections.new(collection_name)
    for ob in bpy.context.selected_objects:

And this is how it looks like after the import:

enter image description here


1 Answer 1


I guess what happens here is that the command bpy.ops.import_scene.obj imports the obj files into blender as children of the Scene Collection (i.e. bpy.context.scene.collection). This just seems to be the behavior of that import command and there seems to be no parameter of that import command allowing you to change the behavior.

Now, your code creates new collections and links the imported obj's to those new collections. Linking means that these obj's are referenced by those new collections as well as the Scene Collection.

The point here is that objects can be referenced by as many collections as you want. This effectively allows you to build a graph, not just a tree. That behavior resembles the behavior of layers in Blender version < 2.8.

What you need to do is unlink those obj's from the Scene Collection which you can do by adding an unlink command in your for loop:

for ob in bpy.context.selected_objects:
  • $\begingroup$ Yes. Thank you for your answer. It is exactly what I need! $\endgroup$
    – Mila
    Jul 31, 2019 at 7:29

Not the answer you're looking for? Browse other questions tagged .