1
$\begingroup$

I used this python code to set the parent (link) the specific objects to newly created collection. The code is working well, but I can see the same objects instance outside(Scene Collection) the newly created collection. I would like to know what should be the reason still objects instances are visible outside(Scene Collection) the newly created collection and what should the solution to show the objects under the newly created collection?

import bpy

new_collection_name = "NewCollection"
new_collection = bpy.data.collections.new(new_collection_name)
bpy.context.scene.collection.children.link(new_collection)

object_to_parent = bpy.data.objects.get("rig")
new_collection.objects.link(object_to_parent)

This is my output

enter image description here

But my exception should be like below image, This I did by manually drag and drop the objects to the newly created collection. How I can get below result via python code?

enter image description here

If any one can help, that should be appreciable,

Thanks, Subin

$\endgroup$

2 Answers 2

0
$\begingroup$

Alternatively without operators.

import bpy


collection = bpy.data.collections["NewCollection"]
for obj_name in ("rig", "batman_geo"):
    obj = bpy.data.objects[obj_name]
    while obj.users_collection:
        obj.users_collection[0].objects.unlink(obj)
    collection.objects.link(obj)

The reason you see a grayed out child in the new collection is because its parent is linked to the collection, but the child is not and you have "Object Children" filter enabled in the outliner settings.

enter image description here

Note an object can be linked to an arbitrary number of collections, so I first unlink from all linked collections, then link to the new one. In your case rig is linked both in the scene collection and in the new collection.

You don't parent an object to a collection, because that would imply an exclusive relationship, you link an object to a collection.

If you want to recursively link all children of a root object to a specific collection you can use

import bpy


collection = bpy.data.collections["NewCollection"]
objects_to_link = [bpy.data.objects["rig"]]
while objects_to_link:
    obj = objects_to_link.pop(0)
    while obj.users_collection:
        obj.users_collection[0].objects.unlink(obj)
    collection.objects.link(obj)
    objects_to_link.extend(obj.children)
$\endgroup$
1
  • $\begingroup$ Thank you very much Gorgious $\endgroup$
    – Subin Gopi
    Commented Dec 28, 2023 at 23:57
1
$\begingroup$

Use this instead :

import bpy


COLLECTION_NAME = "NewCollection"

OBJECT_TO_PARENT = bpy.data.objects.get("rig")


bpy.ops.object.select_all(action='DESELECT')

OBJECT_TO_PARENT.select_set(True)


bpy.ops.object.move_to_collection(new_collection_name=COLLECTION_NAME, collection_index=0, is_new=True)
$\endgroup$
1
  • $\begingroup$ Thank you very much Yaroslav $\endgroup$
    – Subin Gopi
    Commented Dec 28, 2023 at 23:57

You must log in to answer this question.

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