What are the Python codes related to collection actions for blender 2.8?

hello

there is some changes in a lot of things in 2.8…
i cant find how to:

#create a new collection ?

#add selected object to collection "name here" ? 

#add selected object to collection created in the same code ? 

#more useful code about collections?

could someone show me?

also where to find this type of information appart from digging for hours with ctrl space in the console?

thanks

See the API docs:
https://docs.blender.org/api/blender2.8/bpy.types.BlendDataCollections.html
https://docs.blender.org/api/blender2.8/bpy.types.Collection.html

1 Like

thanks for the link brecht

so i tried

bpy.types.BlendDataCollectionsnew(name)

bpy.types.BlendDataCollectionsnew("name")

bpy.types.BlendDataCollectionsnew(name="name")

bpy.types.BlendDataCollections.new(name)

bpy.types.BlendDataCollections.new("name")

nothing is happening

i also tried the code that blender is telling me that it just create a collection, but nothing is new inside of the outliner (the code of “ctrl G”)

bpy.ops.collection.create(name="Collection")

im really sorry but i think im doing something wrong and i dont know what it is

Here’s a somewhat practical example to kick start you a bit. Things to remember along the way: collections have a hierarchy, it’s not a flat list, and collections can contain other collections; they need to be placed under a parent in the scene for it to show up and work correctly*

Scenario:

  1. We want to create a new Collection called “Test” as a Sibling of the default “Cube” object
  2. We want to move Cube into this new “Test” collection

Make some helpers

def find_collection(context, item):
    collections = item.users_collection
    if len(collections) > 0:
        return collections[0]
    return context.scene.collection

def make_collection(collection_name, parent_collection):
    if collection_name in bpy.data.collections: # Does the collection already exist?
        return bpy.data.collections[collection_name]
    else:
        new_collection = bpy.data.collections.new(collection_name)
        parent_collection.children.link(new_collection) # Add the new collection under a parent
        return new_collection

Do the scenario:

# Step 1
cube = bpy.data.objects["Cube"]
cube_collection = find_collection(bpy.context, cube)
new_collection = make_collection("Test", cube_collection)

# Step 2
new_collection.objects.link(cube)  # put the cube in the new collection
cube_collection.objects.unlink(cube)  # remove it from the old collection

*Note: Objects can be linked into multiple collections, be mindful of this. Above I’ve just grabbed the first one for Cube during the find operation
*Note: There’s a special “scene.collection” at the upper root of the hierarchy. It’s kinda separate right now and needs to be handled separately

3 Likes

Thanks a lot for your time,

so it worked, i tried to do it to the whole selection instead of just an object and this is what i coded

import bpy 

def find_collection(context, item):
    collections = item.users_collection
    if len(collections) > 0:
        return collections[0]
    return context.scene.collection

def make_collection(collection_name, parent_collection):
    if collection_name in bpy.data.collections:
        return bpy.data.collections[collection_name]
    else:
        new_collection = bpy.data.collections.new(collection_name)
        parent_collection.children.link(new_collection)
        return new_collection

for o in bpy.context.selected_objects:
    P = bpy.context.selected_objects[2]
    P_collection = find_collection(bpy.context, P)
    new_collection = make_collection("Quick Particle 01", P_collection)
    new_collection.objects.link(P)
    P_collection.objects.unlink(P)

but the number just after

    P = bpy.context.selected_objects[2]

as i just discover, is the number of selected object -1 , and my next step is to adapt it to the correct number every time … hmm

for o in bpy.context.selected_objects:
    P = bpy.context.selected_objects[2]
    P += 1
    P_collection = find_collection(bpy.context, P)
    new_collection = make_collection("Quick Particle 01", P_collection)
    new_collection.objects.link(P)
    P_collection.objects.unlink(P)

If I understood correctly the solution would be simple?

and how to select a collection by his name in outliner to use other operators like duplicate (active is not enough)

oh the cringe.

if i had a time machine…

sorry didn’t understand the question, could you precisely describe what you intend to do so i can help you ?

you may watch this video first, thanks to sybren, it was released not so long ago

1 Like

there is an operator in the outliner which is bpy.ops.outliner.collection_duplicate()
you can override it. the problem is your collection is not active making it active is not enough.
when you select a collection with the mouse you can visually see the difference from just making the collection active.
but apparently this no way to have the same state without using the manual selection.

import bpy

class OUTLINER_OT_collection_duplicate(bpy.types.Operator):
    bl_idname = "outliner.collection_duplicate1"
    bl_label = "Toggle Hide"


    def execute(self, context):

        layer_collection = context.view_layer.layer_collection.children['Collection']
        context.view_layer.active_layer_collection = layer_collection
        ar = context.screen.areas
        area = next(
            (a for a in ar if a.type == 'OUTLINER'), None)
        
        bpy.ops.outliner.collection_duplicate(
            {'area': area})

        return {'FINISHED'}
    
def register():
    bpy.utils.register_class(OUTLINER_OT_collection_duplicate)

def unregister():
    bpy.utils.unregister_class(OUTLINER_OT_collection_duplicate)


if __name__ == "__main__":
    register()

    # test call
    bpy.ops.outliner.collection_duplicate1()

did you try to do a data-block copy ?

c = bpy.data.collections['coll_name'].copy()
then append c in master

1 Like

ah ok with a link behind. it was so simple as that.

in fact I forgot it was my first answer what you proposed. but this is creating a link copy not a full copy as in the outliner duplicate linked vs dupplicate collection. but no matter I give it up

Hello there!
I’d love to understand any python script that could let me to insert objects under a batch series of created collection, following object names, it could be useful to batch export to FBX for a fast workflow to Unreal Engine.
My case example:

Object01
Object02

Need to Create Collection automatically and set the relative object inside:

Collection01\Object01
Collection02\Object02

Hope it could be possible…