I am creating a bone, and then a collection with a specific name if it does not exist, with the following function:
def make_collection(collection_name):
if collection_name in bpy.data.collections:
return bpy.data.collections[collection_name]
else:
new_collection = bpy.data.collections.new(collection_name)
bpy.context.scene.collection.children.link(new_collection)
return new_collection
The collection is linked to the master collection so it's on the top of the hierarchy, and now i try link it to the collection.
I create the collection:
root = make_collection("bat")
and later on in the code, i create a bone (although is an empty since it's a standalone bone), and i try to link it like this:
bpy.ops.object.empty_add(type='CUBE')
bone = bpy.context.active_object
bone.name = data[i].name
#bpy.ops.collection.objects_add_active(collection=root)
root.objects.link(bone)
Even though it's called root, root is not the master collection, just to clarify. The result is the following:
It's the same empty i created, that appears twice in both collections. Although an idea would be to just delete other collections, 2 will appear still, one in the master collection, and one in the collection i created.
Also it's the same item since, if i click one, both get selected at the same time, so there's only one empty in the scene, i just can choose where to click it from.
Now the problem is i'm doing this with several objects in a loop, so i would be basically duplicating the object list twice, and that's pretty redundant.
If i don't use the root.objects.link code at all, the objects would just be on the master collection, but i want them to be on the collection i specifically created.
Is there any way to fix this? I'm not sure why it appears on both collections and i'm very confused. I'm fairly new with the blender API, so i'm not sure what i'm doing wrong.
Thanks for reading!