2.8 is new and I can't find how to:

  • Create a new collection.

  • Add selected object to collection "name here" .

  • Add selected object to collection created in the same code.

Could someone show me how?

i found this and this in the API

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

---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----


so i got an explanation there: https://devtalk.blender.org/t/what-are-the-python-codes-related-to-collection-actions-for-blender-2-8/4479/3


I found this question (and related links) useful, so thought I would respond here even though the original query is now quite old.

I found the code you have adapted quite hard to follow. Mainly because of my lack of Python experience but partly because of the additional checks (number of selected objects > 0 and create new collection if one doesn't already exist).

This version is less sophisticated, but is perhaps easier to follow and it resolves the [2] related error.

import bpy

#create list of collection names
CollList = ['Structure', 'Architecture', 'Biscuits']

#Find length of collection list (could just use '3' in this case, but I prefer to have a general case)
CollLen = len(CollList)

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

#create list of all selected mesh objects
ListObjects = [ x for x in bpy.context.selected_objects if x.type == 'MESH' ]

#run through list and move objects
for z in ListObjects:

Here an example i made, it will place the active object to a new collection. it's a really simple example i hope it can help the beginner (like me) out there.

import bpy

obj = bpy.context.object
obj_old_coll = obj.users_collection #list of all collection the obj is in

new_coll = bpy.data.collections.new(name="COLLECTION TEST") #create new coll in data
bpy.context.scene.collection.children.link(new_coll) #add new coll to the scene
new_coll.objects.link(obj) #link obj to scene

for ob in obj_old_coll: #unlink from all  precedent obj collections

here another examples:

import bpy

A = bpy.context.object
A_old_coll = A.users_collection #list of all collection the obj is in

terrain_coll_name = "Scatter: ["+A.name+"]"+" Terrain"
if terrain_coll_name not in bpy.data.collections: #if this collection name is not in all collection already:
    terrain_coll = bpy.data.collections.new(name=terrain_coll_name) #create new coll in data
    bpy.context.scene.collection.children.link(terrain_coll) #add new coll to the scene
    terrain_coll = bpy.data.collections[terrain_coll_name] #else let re use the already existing one

terrain_coll.objects.link(A) #link obj to terrain coll

for ob in A_old_coll: #unlink from all  precedent obj collections

Here's a one liner to hide collections:

bpy.data.collections['characters'].hide_viewport = True

It doesn't exactly turn the eye off. Instead, it greys it out (and all its children). But it hides the collection nonetheless.

It does take longer to unhide things with this line, though. It's not as fast as toggling the eye. If someone could clarify this behavior, that would be great.

However, hope this is helpful!

  • 1
    $\begingroup$ Did you mean to post this answer to a different question, because this question didn't ask about hiding collections? $\endgroup$ – rjg Aug 25 at 22:18

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.