This question already has an answer here:
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?
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', '') bpy.ops.import_scene.obj(filepath=f) myCol = bpy.data.collections.new(collection_name) bpy.context.scene.collection.children.link(myCol) for ob in bpy.context.selected_objects: myCol.objects.link(ob)
And this is how it looks like after the import: