bpy.ops.object.select_name doesn't work?

Hi there, i am in the middle of writng a little script, and what i need there is to take the list selected meshes, and select each one of meshes from list and do some action on them.

I have got creation of list working just fine, but the problem is when i want to select object by name… I thought using fuction bpy.ops.object.select_name(name="", extend=False) will be appropriate here, but this seems not to be working. I mean ths little script

import bpy

obs = bpy.data.objects


for ob in obs:
    bpy.ops.object.select_name(name = ob.name, extend=True)

should firstly deselect all meshes, which is working, and then select all objects in the scene in a loop, one by one. But it doesnt. It also doesnt return any error messages…
Writing following codein the console always gives me {‘CANCELLED’}, no matter if default cube was selected or not.

>>> bpy.ops.object.select_name(name='Cube', extend=False)

What am i doing wrong here?
Any help then how to select object by its name?

P.S. I am working on newest cog windows build from today(Trunk Blender 2.52 r28900 Win32)


This code worked for me using 28719. It moved all objects in my scene to Z location 10 and printed out their names.

import bpy

obs = bpy.data.objects


for ob in obs:
    print (ob.name)
    bpy.ops.object.select_name(name = ob.name, extend=True)
    ob.location[2] = 10

Maybe they changed the API. You have seen that big fat RED warning haven’t you?

Of course Ive seen it, but when I am using autocomplete it is showing me that such function exists, but it does not work. your code works fine as it refers to each object in each iteration, it does not have anything to do whith object being selected or not. This version will not work:

import bpy

obs = bpy.data.objects


for ob in obs:
    print (ob.name)
    #This should select diffrent object in each iteration
    bpy.ops.object.select_name(name = ob.name, extend=False)
    #As I have in each iteration a list containing one element, this should
    #change its Z axis location, but I am geting 'index out of range' error, as 
    #previous function does not work. 

It looks like you are correct, that function does not work. You should report it in the bug tracker.

But that does not mean you can not write your own version of that function.

import bpy

def returnObjectByName (passedName= ""):
    r = None
    obs = bpy.data.objects
    for ob in obs:
        if ob.name == passedName:
            r = ob
    return r

obs = bpy.data.objects


for ob in obs:
    print (ob.name)
    myObj = returnObjectByName(ob.name)
    if myObj != None:
        print (dir(myObj))
        myObj.selected = True
        myObj.location[2] = 10
        myObj.selected = False

There seems to be some error in it:

>>> c=bpy.data.objects['Cube']
>>> c
<bpy_struct, Object("Cube")>
>>> bpy.context.selected_objects.append(c) #no complaint!
>>> bpy.context.selected_objects  #should have c inside!
[ ]

Thanks Atom, that works fine!

not a similar approach, but I’ve had to make different objects the “active” one to use some operators on a selection before…

scn = bpy.context.scene
#store the active object so it can be restored....
ob_active = bpy.context.active_object

objs = bpy.context.selected_editable_objects

for ob in objs:  
    scn.objects.active = ob
    # perform operations with operators on the active object here...

#restore the original active object
scn.objects.active = ob_active

it certainly works… maybe it’s what you need?

can you remind what the difference between active objects , selected objects
selected editable objects?

i touhgt the active object was the last selected or created object
or may be i’m wrong here!

in the example fiven with c an object
it’s not included in active object cause it’s another list
so it’s not added to the selected or active object i think !

happy 2.5

the active object is usually the last selected object… (though as in my example it can be set by a script)

most operators only affect the “active object” (but many python operators will be designed to work with all the selected objects…)

“Selected objects” is a list of all the selected objects, that includes those that are linnked from libraries, lights, cameras… the lot…

“selected editable objects” is just the selected mesh objects that are not library objects either…

I’m sorry for bringing up and old topic, but i can’t seem to get bpy.ops.object.select_all(action=‘DESELECT’) to work.

I load up the default scene, select everything so it has the orange outline behind it and called the script bpy.ops.object.select_all(action=‘DESELECT’) but nothing changes.

Whereas if i call

obs = bpy.data.objects
for ob in obs:
print (ob.name)
ob.selected = False

it properly deselects everything, where am i going wrong?

I actually want to use bpy.ops.object.select_name, but i can’t seem to get that to work either.

edit: My bad, this works fine in 2.53, i was checking it in 2.5 alpha 2

You can use select_pattern instead. In the following example, I select an object named counter.

bpy.ops.object.select_pattern(extend=False, pattern=“counter”, case_sensitive=False)

theer si aklso nother i think call base somerhing
which are not certain what it does

which vesion of blender are you using

the 626 sems to be worhin fine !

also for selecte4d object list you annot use this list to delete all object in list
cause it;s changing the list and wont work

you need to use another approch !