50
$\begingroup$

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

$\endgroup$

3 Answers 3

66
$\begingroup$
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
$\endgroup$
7
  • $\begingroup$ Atomic -- this was precisely was looking for (last suggestion). I don't know how I missed this in the online literature. Thank you!!! $\endgroup$
    – AaronJPung
    Commented Sep 16, 2015 at 1:36
  • 5
    $\begingroup$ 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() $\endgroup$
    – Will S
    Commented Jan 5, 2019 at 16:35
  • 4
    $\begingroup$ But it works another way - here is the solution: blender.stackexchange.com/questions/132825/… $\endgroup$
    – piotao
    Commented Mar 30, 2019 at 20:31
  • 6
    $\begingroup$ Correct version on how to set the active object: bpy.context.view_layer.objects.active = bpy.context.scene.objects['Cube'] (in 2020), see: blender.stackexchange.com/a/132829/31447 $\endgroup$
    – brockmann
    Commented Apr 1, 2020 at 16:18
  • 3
    $\begingroup$ "Please don't edit someone elses question to reflect edits to your answer." Thanks. From the revisions of the question: blender.stackexchange.com/posts/38618/revisions Notice that a moderator already rolled back your ediits as well. $\endgroup$
    – p2or
    Commented Jun 21, 2021 at 8:11
20
$\begingroup$

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.

$\endgroup$
5
$\begingroup$

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.

$\endgroup$
2
  • 2
    $\begingroup$ 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.... $\endgroup$
    – Noir Talon
    Commented Sep 16, 2019 at 11:49
  • 2
    $\begingroup$ @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. $\endgroup$
    – sambler
    Commented Sep 19, 2019 at 3:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .