How to select object by name

Hi all,

My script broke down because after 2.61 the bpy.ops.object.select_name function has apparently been removed :mad:

I can not find it’s replacement so how do I select an object by its name in Blender with python?


import bpy
ob = bpy.data.objects.get("my_object_name_here")
if ob != None:
    print("My object exists and I can operate upon it.")
else:
    print("My object does not exist.")

You must examine the resulting object. If it is None then the object does not exist.

Thanks for the quick reply, but maybe I need to clarify. I want to select only the object and make it the active object in the 3D view.
Previously I could do it like this

bpy.ops.object.select_name(name= <i>ob_name</i>, extend=False)

This no longer works, so I can indeed do this

bpy.ops.object.select_all(action='DESELECT')
ob = bpy.data.objects.get(<i>ob_name</i>)
ob.select = True

It is a not so nice work around, but it also does not make it the active object…

So how then to make the object selected and active?

It’s a little tricky because “select” is a property which belongs to each object (True or False). You might guess that “active” is a property of each object as well, only by some magic, if one object.active = True, all the others must become active = False. But, it’s not that way. The active object is a property of a scene which also seems pretty logical. Browse these (esp the 2nd link) and see what you come up with and I bet you will figure it out. Post your results and let us know. If you need more help, come back again :slight_smile:

http://www.blender.org/documentation/blender_python_api_2_63_7/bpy.types.Scene.html?highlight=scene#bpy.types.Scene

http://www.blender.org/documentation/blender_python_api_2_63_7/bpy.types.SceneObjects.html#bpy.types.SceneObjects

Ok thanks, just needed to know where to search.

I made this function to replace the old one and it seems to work fine :yes:

def select_name( name = "", extend = True ):
    if extend == False:
        bpy.ops.object.select_all(action='DESELECT')
    ob = bpy.data.objects.get(name)
    ob.select = True
    bpy.context.scene.objects.active = ob

Hi Guys,

in Blender v2.76, I have stored the name of an incoming object at the begining of function def
old = bpy.context.object.name

at the end I would like to select this object again but all of these selections makes “ORANGE” selection instead of “YELLOW”


v1:
    bpy.data.objects[old].select = True


v2:
    bpy.ops.object.select_by_type(type='EMPTY')    
    bpy.ops.object.select_pattern(pattern=old)
    
v3:
    bpy.ops.object.select_all(action='DESELECT')
    ob = bpy.data.objects.get(old)
    ob.select = True

I am not able to run another function on the selected object, without selecting the object with mouse once again

please could you help me out, many thanks

Jozef

I recently posted an addon that does this by typing in what it starts with.
Check out the released scripts and addons forum.

v3)


bpy.ops.object.select_all(action='DESELECT')
o = bpy.data.objects[old]
o.select = True
bpy.context.scene.objects.active = o

many thanks JuhaW, this was the line to make it work :slight_smile:

any reason you used example “o” to store the object, instead of “ob” ?
is it just a habit, or “ob” can cause some issues ?

Just my lazy habbits from python console, I’ll try always use ‘o’ for object, ‘objs’ for objects.

thumbup :wink:

Thanks for this, I needed it for a script that names an original object to call it back after.