2
$\begingroup$

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: 
            children.append(ob) 
    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])
        bpy.context.scene.collection.children.link(y)

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

    for z in ListObjects:
        bpy.data.collections[NewCollectionName].objects.link(z)
$\endgroup$

1 Answer 1

7
$\begingroup$

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:
            c.objects.unlink(ob)

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

# to remove if the context object is only a temporary place holder
bpy.data.objects.remove(ob)
$\endgroup$
4
  • $\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$ May 5, 2021 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$ May 5, 2021 at 12:59
  • 1
    $\begingroup$ just add an bpy.ops.object.delete() under the for loop - but not indented. $\endgroup$
    – Chris
    May 5, 2021 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$ May 6, 2021 at 5:07

You must log in to answer this question.

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