I currently have a method that does:

self.myCol = bpy.data.collections.new(self.collection_name)

This method creates and adds a new collection to the outliner. Next, I want to create a bunch of cubes and add those cubes to the collection. How can I tell blender to create objects only within that particular collection?

Something like:

bpy.ops.mesh.primitive_cube_add(collection='name', size=2, location=(0, 0, 0))

Obvious cube_add doesn't have the param for it, so what's the proper way to do this?


4 Answers 4


Your answer is actually wrong.

If your active collection is another as the main collection, when running your script the object will not be removed from this.

import bpy

# our created cube is the active one
obj = bpy.context.active_object
# Remove object from all collections not used in a scene
# add it to our specific collection

About remove all: https://docs.blender.org/api/blender2.8/bpy.ops.collection.html#bpy.ops.collection.objects_remove_all


I figured it out. C is bpy.context btw.

self.master_collection = bpy.context.scene.collection

self.layer_collection = bpy.data.collections[ collection_name ]

for i in range(...):

    bpy.ops.mesh.primitive_cube_add(size=2, view_align=False, enter_editmode=False, location=(0, 0, 0))
    # Create the cubes

    self.layer_collection.objects.link(C.object) #link it with collection

    self.master_collection.objects.unlink(C.object) #unlink it from master collection

Set the active collection.

IMO the key here if adding a new object with an operator is setting the context collection. See Change active collection

python console test.

Set the context collection to scene "master collection" scene.collection and add cube to it.

>>> lc = C.view_layer.layer_collection
>>> C.view_layer.active_layer_collection = lc
>>> C.collection

>>> bpy.ops.mesh.primitive_cube_add()

Set to another collection in the scene, eg "Collection" in default startup file ie scene.collection.children['Collection']

>>> lc = C.view_layer.layer_collection.children['Collection']
>>> C.view_layer.active_layer_collection = lc
>>> C.collection

>>> bpy.ops.mesh.primitive_cube_add()

OBJ Importing

The script imports .obj files from a specified folder and pastes them to the scene by collections. Import.py creates the empty collections and temporary moves objects to data-block. Paste.py moves objects from data-block to the scene and distributes them by collections. Also added the scale and rotation corrections.

The important moment that can help with the question happens here: at first the objects creates from the meshes data-block using bpy.data.objects.new(mesh.name, mesh) and continue store in the objects data-block (not linked with the scene). After they are distributed across the collections using the name matching.


import bpy
import os.path as path
import glob

root = path.abspath(path.join('' ,".."))
files = glob.glob(root + '\*.obj')

for f in files:
    name = path.split(f)[1].replace('.obj', '')
    col = bpy.data.collections.new(name)
    indexes = len([ob for ob in bpy.context.selected_objects if ob.tag == False])
    for i in range(indexes):
         bpy.context.selected_objects[i].name = '{}-{}'.format(name, i)
         bpy.context.selected_objects[i].data.name = '{}-{}'.format(name, i)
    for ob in bpy.data.objects:
    for m in bpy.data.materials:

for mesh in bpy.data.meshes:
    ob = bpy.data.objects.new(mesh.name, mesh)
    ob.active_material_index = 0
    for i in range(len(ob.material_slots)):
        bpy.ops.object.material_slot_remove({'object': ob})


import bpy
from mathutils import Vector, Euler
from math import radians

for ob in bpy.data.objects:
    for col in bpy.data.collections:
       if col.name == ob.name.split('-')[0]:

for ob in bpy.context.scene.objects:
    ob.scale = Vector=((0.5, 0.5, 0.5))
    ob.rotation_euler = Euler((radians(180), 0.0, 0.0), 'XYZ')

Note: DON’T use a symbol - in names


You must log in to answer this question.

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