简体   繁体   中英

Blender 2.6: Select object by name through Python

How do you select objects by name through Python in Blender 2.6?

In 2.4-2.5 , one could simply use:

bpy.ops.object.select_name("OBJECT")

... but this has been discontinued in 2.6 , to be replaced by what?


In 2.6 , one can get the currently selected objects like so...

bpy.context.selected_objects

And there's a way to set the scene's active object...

bpy.context.scene.objects.active = bpy.data.objects["OBJECT"]

And one can also select via operations , like select_all() or select_by_type() ...

bpy.ops.object.select_all(action="TOGGLE")

But I can't find a way to select simply by name.

Thanks very much.

bpy.data.objects['OBJECT'].select = True

Selection data is contained within the individual objects. You can read and write them as shown. In a slightly more readable form:

object = bpy.data.objects['OBJECT']
object.select = True

bpy.ops.object.select_name()已取代bpy.ops.object.select_pattern() (2.62左右,我觉得呢?),这是一个更强大的版本(也可以选择一个确切的名字,而且还使用模式与通配符,不区分大小写等):

bpy.ops.object.select_pattern(pattern="Cube")
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

bpy.ops.object.select_all(action='DESELECT')

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

Not my code, not guaranteed to work.

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM