How to create a new object under new collection..?

Hello All,
I want to create an object(s) under the newly created collection “RT_BoolOps_Collection” by the script when the user press button.
But, the problem is, this script creates an object link in other collection as well.
I want to create an object(s) only in “RT_BoolOps_Collection” collection every time the user press a button. like, RT_BoolOps_Box, RT_BoolOps_Box.001, RT_BoolOps_Box.002, etc.

class CtreateObject(bpy.types.Operator):
bl_idname = “pt.add_box”
bl_label = “Add Box”
bl_options = {“REGISTER”, “UNDO”}
bl_description = “”“Create a box”""

def LOL(self):

    # create_box = bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0))
    # bpy.context.active_object.name = 'RT_BoolOps_Box'


    if "RT_BoolOps_Collection" not in bpy.data.collections:



        create_collection = bpy.data.collections.new(name="RT_BoolOps_Collection")
        bpy.context.scene.collection.children.link(create_collection)
        bpy.data.collections['RT_BoolOps_Collection']
        bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0))
        bpy.context.active_object.name = 'RT_BoolOps_Box'
        bpy.data.collections['RT_BoolOps_Collection'].objects.link(bpy.context.object)



    else:
        bpy.data.collections['RT_BoolOps_Collection']
        bpy.ops.mesh.primitive_cube_add(location=(0, 0, 0))
        bpy.context.active_object.name = 'RT_BoolOps_Box'

        # bpy.data.collections['RT_BoolOps_Collection'].objects.link(bpy.context.object)




def execute(self, context):
    scene = context.scene
    object = context.object

    self.LOL()

    return {'FINISHED'}
2 Likes

same problem here…

1 Like

Bumping this thread. I’m interested as well.

@Nitin_Joshi You are doing wonderful job. I think everyone who is working in BoolOps collection facing same problem. Actually i am also the one who have same error. Is somebody have the solution of this problem. :slightly_smiling_face:

Here is a solution I found on the web modified to suit my needs, but I think it has the potential to help others struggling to learn how to do this specific topic. I haven’t refined the code yet, but it will get you started.

import bpy
from bpy import context

scene = context.scene
cursor = scene.cursor.location
obj = context.active_object
view_layer = context.view_layer

#checks to see if the new coll exists in the master coll
def recurLayerCollection(layerColl, collName):
found = None
if (layerColl.name == collName):
return layerColl
for layer in layerColl.children:
found = recurLayerCollection(layer, collName)
if found:
return found

#creates dup, sees if new collection exists, if not creates it
#if it does exist it places the dup in the sub coll
#then it deselects the new obj and goes to the master coll and selects the original
def create_dup():
master_coll = view_layer.layer_collection
current_coll = recurLayerCollection(master_coll, context.collection.name)
target_coll_name = “test”
target_coll = recurLayerCollection(master_coll, target_coll_name)

if target_coll:
    view_layer.active_layer_collection = target_coll
    new_coll = False
    obj_new = obj.copy()
    view_layer.active_layer_collection = target_coll
    view_layer.active_layer_collection.collection.objects.link(obj_new)
    view_layer.active_layer_collection = current_coll
    obj_new.select_set(False)
    obj.select_set(True)
    view_layer.objects.active = obj
else:
#    create a new collection in the master scene collection
    target_coll = bpy.data.collections.new(target_coll_name)
    scene.collection.children.link(target_coll)
    target_coll = recurLayerCollection(master_coll, target_coll_name)
    new_coll = True
    obj_new = obj.copy()
    view_layer.active_layer_collection = target_coll
    view_layer.active_layer_collection.collection.objects.link(obj_new)
    view_layer.active_layer_collection = current_coll
    obj_new.select_set(False)
    obj.select_set(True)
    view_layer.objects.active = obj

create_dup()