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”""
@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.
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.
#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