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?
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:
We want to create a new Collection called “Test” as a Sibling of the default “Cube” object
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
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?
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()
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: