Blender Studio
  • Films
  • Projects
  • Training
  • Characters
  • Tools
  • Blog
  • Join
  • BLENDER.ORG

    • Download

      Get the latest Blender, older versions, or experimental builds.

    • What's New

      Stay up-to-date with the new features in the latest Blender releases.

    LEARNING & RESOURCES

    • Blender Studio

      Access production assets and knowledge from the open movies.

    • Manual

      Documentation on the usage and features in Blender.

    DEVELOPMENT

    • Developers Blog

      Latest development updates, by Blender developers.

    • Documentation

      Guidelines, release notes and development docs.

    • Benchmark

      A platform to collect and share results of the Blender Benchmark.

    • Blender Conference

      The yearly event that brings the community together.

    DONATE

    • Development Fund

      Support core development with a monthly contribution.

    • One-time Donations

      Perform a single donation with more payment options available.

Training Highlights
Stylized Rendering with Brushstrokes
Geometry Nodes from Scratch
Procedural Shading Fundamentals
Stylized Character Workflow

Training types
Course Documentation Production Lesson Workshop

Training categories
Animation Geometry Nodes Lighting Rendering Rigging Shading
Film Highlights
Wing It!
2023
Charge
2022
Sprite Fright
2021
Spring
2019
Project Highlights
Project DogWalk
Interactive
Gold
Showcase
BCON24 Identity
Showcase
Fighting with Grease Pencil
Article
Course
Scripting for Artists
Blender Versions
Videos
  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
  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

Sybren Stüvel
Sybren Stüvel Author
  • Free
Download English Subtitles
Download .mp4   50.1 MB
License CC-BY
Report Problem
Videos

Blender Collections

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

Join to leave a comment.

16 comments
原苗苗
原苗苗
Sept. 19th, 2022

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 Stüvel
Sybren Stüvel
Sept. 21st, 2022

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

Leslie Solorzano
Leslie Solorzano
Nov. 29th, 2020

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

Sybren Stüvel
Sybren Stüvel
Dec. 21st, 2020

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

Ron Tarrant
Ron Tarrant
Aug. 20th, 2020

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

Ron Tarrant
Ron Tarrant
Aug. 20th, 2020

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

Ron Tarrant
Ron Tarrant
Aug. 19th, 2020

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

Ron Tarrant
Ron Tarrant
Aug. 20th, 2020

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

Ron Tarrant
Ron Tarrant
Aug. 19th, 2020

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
Ron Tarrant
Aug. 19th, 2020

*@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 Stüvel
Sybren Stüvel
Sept. 1st, 2020

*@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
Ron Tarrant
Sept. 14th, 2020

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

Takeshi Funahashi
Takeshi Funahashi
April 26th, 2020

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?

Henk Kok
Henk Kok
March 27th, 2020

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 Stüvel
Sybren Stüvel
March 27th, 2020

*@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
Henk Kok
March 27th, 2020

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

Films Projects Training Blog Blender Studio for Teams
Pipeline and Tools
  • CloudRig
  • Blender Kitsu
  • Brushstroke Tools Add-on
  • Blender Studio Extensions
Characters
  • Mikassa
  • Whale
  • Ballan Wrasse
  • Snow
Studio
  • Terms & Conditions
  • Privacy Policy
  • Contact
  • Remixing Music
Blender Studio

The creators who share.

Artistic freedom starts with Blender The Free and Open Source 3D Creation Suite