Skip to content

Selections

In a lot of cases you want to operate on a set of selected objects. You can access (read only) the current selection with bpy.context.selected_objects:

>>> bpy.context.selected_objects
[bpy.data.objects['Cube'], bpy.data.objects['Plane']]

Changing the current selection can be done in several ways. Selection state per object can be controlled with the select_get() and select_set() methods:

>>> bpy.context.selected_objects
[]

>>> bpy.data.objects['Camera'].select_get()
False

>>> bpy.data.objects['Camera'].select_set(True)
>>> bpy.context.selected_objects
[bpy.data.objects['Camera']]

The full selection set can also be changed:

# Select all visible objects
>>> bpy.ops.object.select_all(action='SELECT')

# Deselect all objects
>>> bpy.ops.object.select_all(action='DESELECT')

# Toggle the selection state for each object
>>> bpy.ops.object.select_all(action='TOGGLE')

Note that the default mode for bpy.ops.object.select_all() when not specified is TOGGLE.

Also note that the selection methods above operate only on objects that are currently visible objects in the scene (in terms of the outliner eye icon), just like for the selection hotkeys (such as A) in the 3D viewport.


Last update: 06 March 2024 14:59:42