Blender 2.6:通过 Python 按名称选择对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19472499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Blender 2.6: Select object by name through Python
提问by Jollywatt
How do you select objects by name through Python in Blender 2.6?
如何在 Blender 2.6 中通过 Python 按名称选择对象?
In 2.4-2.5, one could simply use:
在2.4-2.5 中,可以简单地使用:
bpy.ops.object.select_name("OBJECT")
... but this has been discontinued in 2.6, to be replaced by what?
... 但是这个在2.6已经停产了,取而代之的是什么?
In 2.6, one can get the currently selected objects like so...
在2.6 中,可以像这样获取当前选定的对象...
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()
...
并且还可以通过操作进行选择,例如select_all()
或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.
非常感谢。
采纳答案by Jollywatt
bpy.data.objects['OBJECT'].select = True
Selection data is contained within the individual objects. You can read andwrite them as shown. In a slightly more readable form:
选择数据包含在各个对象中。您可以如图所示读取和写入它们。以更易读的形式:
object = bpy.data.objects['OBJECT']
object.select = True
回答by Azeirah
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.
不是我的代码,不能保证工作。
回答by mont29
bpy.ops.object.select_name()
has been replaced by bpy.ops.object.select_pattern()
(around 2.62, I think?), which is a more powerful version (it can select an exact name, but also use patterns with wildcards, be case-insensitive, etc.):
bpy.ops.object.select_name()
已被bpy.ops.object.select_pattern()
(大约 2.62,我认为?)取代,这是一个更强大的版本(它可以选择一个确切的名称,但也可以使用带通配符的模式,不区分大小写等):
bpy.ops.object.select_pattern(pattern="Cube")