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:
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)