Skip to content

A note on bpy.data, bpy.data.objects, ...

We have been using bpy.data.objects in most examples above to access objects in the scene. This is actually not completely clean, as bpy.data.objects holds all objects in the Blender file. Usually, the distinction doesn't matter as you only have one scene, but a Blender file can hold multiple scenes, each with their own set of objects:

# A file with two scenes, each with their own set of objects
>>> bpy.data.scenes.values()
[bpy.data.scenes['Scene'], bpy.data.scenes['Scene.001']]

# Current scene
>>> bpy.context.scene
bpy.data.scenes['Scene']

# And its objects
>>> bpy.context.scene.objects.values()
[bpy.data.objects['Bottom cube'], bpy.data.objects['Top Cube']]

# <Select different scene>

# Different current scene
>>> bpy.context.scene
bpy.data.scenes['Scene.001']

# And its objects
>>> bpy.context.scene.objects.values()
[bpy.data.objects['Bottom cube.001'], bpy.data.objects['Top Cube.001']]

# All objects in the *file*
>>> bpy.data.objects.values()
[bpy.data.objects['Bottom cube'], bpy.data.objects['Bottom cube.001'], 
bpy.data.objects['Top Cube'], bpy.data.objects['Top Cube.001']]

Although objects can also be shared between scenes:

# Two scenes
>>> bpy.data.scenes.values()
[bpy.data.scenes['Scene'], bpy.data.scenes['Scene.001']]

# First scene, cubes are local to scene, torus is shared between scenes
>>> bpy.context.scene
bpy.data.scenes['Scene']

>>> bpy.context.scene.objects.values()
[bpy.data.objects['Torus'], bpy.data.objects['Bottom cube'], 
bpy.data.objects['Top Cube']]

# Second scene, different cubes, torus is shared
>>> bpy.context.scene
bpy.data.scenes['Scene.001']

>>> bpy.context.scene.objects.values()
[bpy.data.objects['Bottom cube.001'], bpy.data.objects['Top Cube.001'], 
bpy.data.objects['Torus']]

The point here is that bpy.data.objects, and every other attribute under bpy.data, holds values of the complete Blender file. Per-scene values are available through attributes of a Scene object, e.g. bpy.context.scene.objects. For certain use cases this distinction matters.


Last update: 27 November 2023 14:39:38