Collections! Blender 2.80 replaced groups and scene layers with collections. In this chapter of Scripting for Artists, Sybren explains how to handle them from your scripts.
Thanks, Sybren. These are very instructive. But I didn't fully understand how these works. I like the way you start trying out a few things in Blender console before you introducing new things. I'm trying to figure out how do I get the info I need to know from Blender console. One thing confuse me is, I don't know when should I use bpy.data(D) and othertimes bpy.context(C). What is the difference between bpy.data(D) and bpy.context(C) that I should understand? Bacause they seems have lots of common stuffs(eg. collection('s)). but C.collection and D.collections return different things. And why D.collections and C.collection(without 's')? D.collecion returns shomething with angle bracket (which we never used), what's that about?
@原苗苗 Good questions!
bpy.data
contains all the data loaded by Blender at the moment. All the collections (so also from different scenes, or ones that aren't even used in any scene), all the objects, etc. For any specific moment in time, it has the same contents regardless of where you access it from (independent of which editor your operator runs in, for example).
The context
has varying info in there. bpy.context
is the global one, but many functions also have a context
parameter, where they get a specific context passed. context.scene
is the current scene, context.object
is the current "object of interest" -- this defaults to the active object in the scene, but when drawing property panels, those draw functions can also get the pinned object as context.object
. context.region
points to the UI region ("header", "footer", "main 3D view", etc.) of where the operator was invoked. As a rule of thumb, always use the context
parameter if you get one from Blender, and only use bpy.context
as a fallback.
bpy.data.objects
is a collection of objects. This means that you can use bpy.data.objects[0]
to get the first one, or bpy.data.objects["Cube"]
to get the one named "Cube". Same for the other bpy.data.xxx
attributes.
If a collection isn't linked, does it disappear after closing Blender?
@Leslie Solorzano Why not find out yourself? Just create a collection via Python, so that it's not linked to anything, and save & reload the file.
Okay, how about this... How do I make a specific collection the active collection?
*@Ron Tarrant* Never mind. I found a solution here: https://blender.stackexchange.com/questions/141581/how-to-set-active-scene-collection
Can anyone tell me where the example Python scripts are, the ones that ship with Blender?
*@Ron Tarrant* Never mind. I found them in C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\templates_py\
When I was following along, adding a new collection, the new collection was linked to whichever collection was currently selected in the Outliner. And if the selected item in the Outliner was an object (rather than a collection) the new collection was added as a child of the object. This raises several questions, but first of which is: How do I select a specific collection using Python? I took a look through the API docs, but nothing jumped out.
*@Ron Tarrant* I've been researching this now for the better part of a day and finding nothing. Surely there's a way to do this.
*@Ron Tarrant* Please pop over to https://blender.chat/ and join the Python channel. It's easier to go over your code there and see what exactly you're doing.
*@Sybren A. Stüvel* I can't find a way to switch Blender Chat to a light theme. Because of an eye condition, it's painful to read light text on a dark background. But thanks anyway.
Thanks Sybren This tutorial is really good to understand 2.8 collection too.. thanks. hope you will teach about "scene layer" management too.. (with use bpy) ,Then I have one question about un-link.
Each scene have one "Master Collection"(Scene Collection), and we can link "Collection 1" to Master Collection. then we can link "obj1" to "Collection 1" and Collection1 is child collection of Master Collection.
I expect, when I un-link "obj1" from "Collection1", obj1 is removed from Collection1, but still in "Master Collection",, But Blender remove the "obj1" from Master Collection too. then it becom orphan data.
Though I can link "obj1" to "Master Collection" later, but it seems un-necessary step. Is there any reason,, un-link from "Collection1" (child correction of Master) un-link from "Master correction" too?
This was very nice. Thank you!
The part about: Handling errors when they occur was cool, thanks for that.
Suggestion for a next series / addition: Moving a collection called "apples", from collection "fruit", to a collection "lunch"
*@Henk Kok* Moving a collection is the same as moving an object, except you use coll_from.children.unlink()
and coll_to.children.link()
.
The link to catching an exception is covered by "Handling errors when they occur".
*@Sybren A. Stüvel* DOH! So obvious. Thank you.
Join to comment publicly.