31

The following:

bpy.data.objects['Cube'].select = True

bpy.context.scene.objects.active = bpy.data.objects['Sphere.017']

don't work in Blender 2.8, they did in 2.79 though.
How should I select (multiple) objects with Python in Blender 2.8?

Lukasz-40sth
  • 3,062
  • 7
  • 19
  • 30
Michael Teiniker
  • 1,094
  • 1
  • 12
  • 26
  • Please see https://blender.stackexchange.com/questions/38618/selecting-an-object-via-scripting/38626#38626 – atomicbezierslinger Dec 08 '18 at 15:26
  • thanks for the help, i already check this topic and it didnt help me at all at resolving my problem – Fox Dec 08 '18 at 15:28
  • 2
    Regarding why "thanks" was removed from your question: https://blender.meta.stackexchange.com/questions/2463/why-is-saying-thanks-forbidden-here – Ray Mairlot Dec 08 '18 at 18:03

3 Answers3

60

According to the API changes:

In 2.7x, you could directly (de)select an Object from its select property. This has been removed in 2.8x, in favor of some get/set functions.

bpy.data.objects['Cube'].select = True    # 2.7x
bpy.data.objects['Cube'].select_set(True) # 2.8+

Proof using the Console:

>>> bpy.context.scene.objects["Cube"].select_set(True)
>>> bpy.context.scene.objects["Cube"].select_get()
True

API Link: docs.blender.org/api/current/bpy.types.Object.html#bpy.types.Object.select_set


Example on how to select a certain object in the scene and make it the active object:

ob = bpy.context.scene.objects["Cube"]       # Get the object
bpy.ops.object.select_all(action='DESELECT') # Deselect all objects
bpy.context.view_layer.objects.active = ob   # Make the cube the active object 
ob.select_set(True)                          # Select the cube

Example on how to select multiple objects by name:

for o in ("Cube", "Camera", "Light"):
   obj = bpy.context.scene.objects.get(o)
   if obj: obj.select_set(True)

Example on how to select all objects of a certain collection:

col = bpy.data.collections.get("Collection")
if col:
   for obj in col.objects:
       obj.select_set(True)
p2or
  • 15,860
  • 10
  • 83
  • 143
brockmann
  • 12,613
  • 4
  • 50
  • 93
7

adding thoses two lines work in 2.79, but not in 2.80

bpy.context.scene.objects.active = OB
OB.select = True

the api has change for this new version, so for 2.8 its

bpy.context.view_layer.objects.active = OB
OB.select_set(state=True)

so now my correct code is this

import bpy

OB = bpy.context.selected_objects[0]
bpy.ops.object.modifier_add(type='MIRROR')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.empty_add(type='PLAIN_AXES', view_align=False, location=(bpy.context.scene.cursor_location))
OA = bpy.context.selected_objects[0]
bpy.context.object.name = "Mirror Axes"
OB.modifiers["Mirror"].mirror_object = bpy.data.objects["Mirror Axes"]
OB.select_set(state=True)
bpy.context.view_layer.objects.active = OB
OA.select_set(state=False)
open_menu("D Mirror Menu")
Fox
  • 1,892
  • 18
  • 48
1

The line bpy.context.object = OB throws an exception. Context property "object" is read-only.

In 2.79 and below, we can set the scene's active object with the scene's property:

bpy.context.scene.objects.active = OB
Leander
  • 26,725
  • 2
  • 44
  • 105
  • i already tried "bpy.context.scene.objects.active = OB" it dont work for me, 2.8, the empty is still selected – Fox Dec 08 '18 at 15:29
  • @Dorian The question doesn't say anything about 2.8... – Leander Dec 08 '18 at 15:30
  • the code change for a simple operation like that ? i didnt know that – Fox Dec 08 '18 at 15:31
  • @Dorian Yes, 2.8 works different in that area. Handling the context scenes and collections (which were only introduced in 2.8). I suggest you edit your question to include that info. – Leander Dec 08 '18 at 15:32
  • thanks for the info, i did, i aslo try it in 2.79, dont work either – Fox Dec 08 '18 at 15:33
  • @Dori If I execute this code, in the default scene in 2.79 (with the cube selected) it works as expected. – Leander Dec 08 '18 at 15:35