I want to automate the objects sorting in my scene, the idea is to select the parent object and create a collection for each child (this is done) and then moving one of each of those to their own named collection (here is where I'm stuck). I know how to move selected objects to one collection, but in this case were the names are being retrieved automatically I don't know how to proceed.

Here is the code that creates one collection per children when I select only the parent: enter image description here

After the collections are created by this script I need to move each of those children to their own recently created collection.

import bpy 
def getChildren(myObject): 
    children = [] 
    for ob in bpy.data.objects: 
        if ob.parent == myObject: 
    return children 
myObject = bpy.data.objects['OBJECTS'] 
children = getChildren(myObject) 
for c in children: 
    CollList = [c.name]

    CollLen = len(CollList)

    for x in range(0,CollLen):
        y = bpy.data.collections.new(CollList[x])

    ListObjects = [ x for x in bpy.context.selected_objects if x.type == 'MESH' ]

    for z in ListObjects:

Use the objects children property.

A blender object already has a children collection, there is no need to reinvent the wheel.

Test script,

loop over the children of the context object, (optionally) if they are linked to another collection, unlink, create a new collection with object name, link child object to it, link collection to context collection.

import bpy

context = bpy.context
coll = context.collection
ob = context.object

def unlink(ob):
    for c in bpy.data.collections:
        if ob.name in c.objects:

for child in ob.children:
    if child.type != 'MESH':
    # unlink from any other collection?
    new_col = bpy.data.collections.new(child.name)

# to remove if the context object is only a temporary place holder
  • $\begingroup$ I understand, just I need to create my own collection and then from that new created collection attach the collections created by the script. $\endgroup$ – Juan Carlos May 5 at 12:46
  • $\begingroup$ I hope I don't need to open another question for it, but after the creation is done, how I could deleted that "old" parent empty since I don't have use for it anymore, to keep the scene tidy? $\endgroup$ – Juan Carlos May 5 at 12:59
  • 1
    $\begingroup$ just add an bpy.ops.object.delete() under the for loop - but not indented. $\endgroup$ – Chris May 5 at 13:49
  • $\begingroup$ Why the moved objects to its own collection are still linked to the main one? they shouldn't be unliked already by the script and being only visible under their own collection? $\endgroup$ – Juan Carlos May 6 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.