24
$\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$
34
$\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,
# 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$
  • $\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 Sep 16 '15 at 1:36
  • 4
    $\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 Jan 5 '19 at 16:35
  • $\begingroup$ 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. $\endgroup$ – piotao Mar 30 '19 at 20:29
  • 2
    $\begingroup$ But it works another way - here is the solution: blender.stackexchange.com/questions/132825/… $\endgroup$ – piotao Mar 30 '19 at 20:31
6
$\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
    $\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 Sep 16 '19 at 11:49
  • $\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 Sep 19 '19 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.