Films Training Blog
Pipeline and Tools Characters
Pipeline and Tools Characters
search Login Join
favorite

Development Fund

Support Blender Core Development with a monthly contribution.

theaters

Blender Studio

The creators who share. Access production content and knowledge from the Open Movies.

code

Blender Developer Blog

Latest news and updates on Blender development.

people

Get Involved

Join the community and help with design, development, docs and more.

bar_chart

Open Data

A platform to collect and display the Blender Benchmark results.

menu_book

Blender Docs

Documentation on Blender's features, tools and API.

Blender Conference

The yearly event that brings together the Blender community in one place.

download

Get Blender

Download the latest Blender version, or try the beta!

Films
Training
Blog
Pipeline and Tools
Characters
Search
Login
Join

Course

Scripting for Artists
feed Course Overview
feed Blender Versions
Videos keyboard_arrow_down
  1. 01

    Introduction & copy-pasting

    Free
  2. 02

    Names & Objects

  3. 03

    Stuff on Lists

  4. 04

    Data Types

  5. 05

    Collections: Mass-Rename of Objects

  6. 06

    Blender Collections

    Free
  7. 07

    For vs. While

    Free
  8. 08

    Your Own Operator

    Free
  9. 09

    From Script to Add-on

    Free
  10. 10

    User Interfaces

    Free
  11. 11

    Custom Properties

    Free
  12. 12

    Asset Linking

    Free
  13. 13

    Roast my Add-on

    Free
  14. 14

    The Roast of Nature Clicker

    Free
  15. 15

    Modal Operators

    Free
  16. 16

    Readability & Understandability

Tech keyboard_arrow_down
  1. 01

    Updating F-Curves

  2. 02

    Render 10,000 OBJ files

  3. 03

    Mass-Rename Bones & Vertex Groups

  4. 04

    Rendering from 'all' angles

Course

Scripting for Artists
feed Blender Versions
Videos keyboard_arrow_down
  1. 01

    Introduction & copy-pasting

    Free
  2. 02

    Names & Objects

  3. 03

    Stuff on Lists

  4. 04

    Data Types

  5. 05

    Collections: Mass-Rename of Objects

  6. 06

    Blender Collections

    Free
  7. 07

    For vs. While

    Free
  8. 08

    Your Own Operator

    Free
  9. 09

    From Script to Add-on

    Free
  10. 10

    User Interfaces

    Free
  11. 11

    Custom Properties

    Free
  12. 12

    Asset Linking

    Free
  13. 13

    Roast my Add-on

    Free
  14. 14

    The Roast of Nature Clicker

    Free
  15. 15

    Modal Operators

    Free
  16. 16

    Readability & Understandability

Tech keyboard_arrow_down
  1. 01

    Updating F-Curves

  2. 02

    Render 10,000 OBJ files

  3. 03

    Mass-Rename Bones & Vertex Groups

  4. 04

    Rendering from 'all' angles

Videos

Blender Collections

26th March 2020

info License: CC-BY Free
Download (50.1 MB)
Download English Subtitles
flag Report Problem

Published by

Sybren A. Stüvel

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.

  • Accessing collections from Python 00:48
  • Creating collections 04:44
  • Linking objects to collections 8:09
  • Moving objects between collections 8:59
  • Handling errors when they occur 11:08
  • Setting visibility flags 14:47

16 Comments

Join to comment publicly.

Henk Kok

27th March 2020 - 10:19

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"

Sybren A. Stüvel

27th March 2020 - 12:46

*@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".

Henk Kok

27th March 2020 - 12:48

*@Sybren A. Stüvel* DOH! So obvious. Thank you.

Takeshi Funahashi

26th April 2020 - 06:40

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?

Ron Tarrant

19th August 2020 - 16:09

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

19th August 2020 - 21:27

*@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.

Show more replies

Sybren A. Stüvel

1st September 2020 - 23:46

*@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.

Ron Tarrant

14th September 2020 - 09:30

*@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.

Ron Tarrant

19th August 2020 - 16:55

Can anyone tell me where the example Python scripts are, the ones that ship with Blender?

Ron Tarrant

20th August 2020 - 14:37

*@Ron Tarrant* Never mind. I found them in C:\Program Files\Blender Foundation\Blender 2.83\2.83\scripts\templates_py\

Ron Tarrant

20th August 2020 - 11:14

Okay, how about this... How do I make a specific collection the active collection?

Ron Tarrant

20th August 2020 - 14:38

*@Ron Tarrant* Never mind. I found a solution here: https://blender.stackexchange.com/questions/141581/how-to-set-active-scene-collection

Leslie Solorzano

29th November 2020 - 21:36

If a collection isn't linked, does it disappear after closing Blender?

Sybren A. Stüvel

21st December 2020 - 15:04

@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.

原苗苗

19th September 2022 - 11:50

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?

Sybren A. Stüvel

21st September 2022 - 09:28

@原苗苗 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.

Blender Studio

The Creators Who Share.

Facebook Logo Twitter Logo YouTube Logo
  • Films
  • Blog
  • Training
Pipeline and Tools
  • Cloud Rig
  • Blender Kitsu
  • Contact Sheet Add-on
  • Blender Purge
  • Blender Cloud Services
Characters
  • Einar
  • Security Bot
  • Huginn
  • Pack Bot
Studio
  • Terms & Conditions
  • Privacy Policy
  • Contact
  • Remixing Music
  • Blender.org
Loading...