47

As shown below, I have 2 nested spheres, each with a different material (inner = "1", outer = "2"). Ultimately, I need to select the inner sphere (material "1") via python scripting.

I've found a number of ways to select (or make active) one of the spheres. However, even though Blender says that it's selecting the correct sphere (indicated by orange line around center sphere), I don't believe it's active -- adding a modifier applies it to the outer sphere instead. That, and the material properties show the properties of the outer sphere.

different materials

Duarte Farrajota Ramos
  • 59,425
  • 39
  • 130
  • 187
AaronJPung
  • 855
  • 1
  • 9
  • 15

3 Answers3

63
bpy.data.objects['Sphere.017']

refers to an object. (Lets assume all names in quotes are the names of objects in your Blender Scene)

2.8 Recent Version of the API

bpy.data.objects["Cube"].select_set(True)
# to select the object in the 3D viewport,

current_state = bpy.data.objects["Cube"].select_get()

retrieving the current state

this way you can also select multiple objects

bpy.context.view_layer.objects.active = bpy.data.objects['Sphere']

to set the active object

enter image description here

https://docs.blender.org/api/current/bpy.types.LayerObjects.html#bpy.types.LayerObjects


⚠Older Version of API 2.79

A python script can do something with that.

You can also use

bpy.data.objects['Cube'].select = True
# to select the object in the 3D viewport,
# this way you can also select multiple objects

additionally you can use

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

to make it the active selected object

atomicbezierslinger
  • 14,279
  • 28
  • 42
  • Atomic -- this was precisely was looking for (last suggestion). I don't know how I missed this in the online literature. Thank you!!! – AaronJPung Sep 16 '15 at 01:36
  • 5
    For some inexplicable reason, in Blender 2.8 you have to use obj.select_set(True) with a different method for checking selection: obj.select_get() – Will S Jan 05 '19 at 16:35
  • This answer no longer works for Blender 2.8. I've tried it and there are no such fields in object like select, or active. – piotao Mar 30 '19 at 20:29
  • 4
    But it works another way - here is the solution: https://blender.stackexchange.com/questions/132825/python-selecting-object-by-name-in-2-8 – piotao Mar 30 '19 at 20:31
  • 5
    Correct version on how to set the active object: bpy.context.view_layer.objects.active = bpy.context.scene.objects['Cube'] (in 2020), see: https://blender.stackexchange.com/a/132829/31447 – brockmann Apr 01 '20 at 16:18
  • 3
    "Please don't edit someone elses question to reflect edits to your answer." Thanks. From the revisions of the question: https://blender.stackexchange.com/posts/38618/revisions Notice that a moderator already rolled back your ediits as well. – p2or Jun 21 '21 at 08:11
  • Is there any other easier ways of doing that, like copy python reference from the outliner, or having it displayed in the "info" pannel so it's easier to copy it afterward ? Like for example if I select from the outliner, I have "bpy.ops.outliner.item_activate(deselect_all=True)" it would be better to have full text. – softyoda yoann Feb 12 '24 at 11:37
21

I'm just doing my duty here. Quick answer that works in 2.8:

objectToSelect = bpy.data.objects["objectName"]
objectToSelect.select_set(True)    
bpy.context.view_layer.objects.active = objectToSelect

You need that last line. Without it I couldn't switch into edit mode. I got the answer here. I wanted to post it here, which is actually the question that comes up when you google this, but it has been locked and now only points to this question which thanks to @piotao pointed me to the answer.

user875234
  • 373
  • 2
  • 9
5

You say that the modifier doesn't get added to the right object - the selected and active objects are only an issue if you are using operators, by directly manipulating data you don't have to bother with what is selected or active unless you want to know the selected object/s as the items that the script will work on.

The line -

mod = bpy.data.objects['Cube'].modifiers.new(name='subsurf',type='SUBSURF')

will add a subsurf modifier to the object named 'Cube' and return the modifier item to the mod variable. You can then use mod.levels = 3 to set the viewport subdivisions and mod.render_levels = 3 to set the render subdivisions.

For the type value of a modifier you can find a list here.

sambler
  • 55,387
  • 3
  • 59
  • 192
  • 2
    NOTE: this does not work in 2.8 If you find this article and are working on 2.8 or newer the syntax is now: ob = bpy.context.scene.objects["Cube.031"] bpy.objects is NO MORE.... – Noir Talon Sep 16 '19 at 11:49
  • 2
    @NoirTalon bpy.data.objects still exists in 2.80. Using the 2.80 release I see that this works but there is a delay in updating, the viewport and properties don't show the result until you do something else (like zoom the viewport or click on the object), this is no longer the case with the daily build so will be fixed in 2.81. – sambler Sep 19 '19 at 03:49