获取选定对象的列表作为字符串 Blender python
Posted
技术标签:
【中文标题】获取选定对象的列表作为字符串 Blender python【英文标题】:get list of selected objects as string Blender python 【发布时间】:2015-01-31 16:09:34 【问题描述】:我面临一个相当容易解决的问题,但我不知道该怎么做。我希望搅拌机列出所有选择为字符串的对象。例如。如果我跑:
selection_names = bpy.context.selected_objects
print (selection_names)
它给了我这一行:
[bpy.data.objects['Cube.003'], bpy.data.objects['Cube.002'], bpy.data.objects['Cube.001'], bpy.data.objects['Cube']]
但我想要selection_names
打印为:
['Cube.001','Cube.002','Cube.003','Cube']
【问题讨论】:
【参考方案1】:解决这个问题的最快方法是通过列表理解:
selection_names = [obj.name for obj in bpy.context.selected_objects]
完全等同于:
selection_names = []
for obj in bpy.context.selected_objects:
selection_names.append(obj.name)
【讨论】:
【参考方案2】:>> selection_names = bpy.context.selected_objects
>>> print (selection_names)
[bpy.data.objects['Armature 05.04 p'], bpy.data.objects['Armature 04.08 l'], bpy.data.objects['Armature 04.07 p'], bpy.data.objects['Armature 04.07 l'], bpy.data.objects['Armature 04.04 p'], bpy.data.objects['Armature 04.05 p'], bpy.data.objects['Armature 04.05 l']]
>>> for i in selection_names:
... print(i.name)
...
Armature 05.04 p
Armature 04.08 l
Armature 04.07 p
Armature 04.07 l
Armature 04.04 p
Armature 04.05 p
Armature 04.05 l
如果你想让它们成为数组中的对象,你可以这样做:
>>> SelNameArr=[]
>>> for i in selection_names:
... SelNameArr.append(i.name)
...
>>> SelNameArr
['Armature 05.04 p', 'Armature 04.08 l', 'Armature 04.07 p', 'Armature 04.07 l', 'Armature 04.04 p', 'Armature 04.05 p', 'Armature 04.05 l']
【讨论】:
以上是关于获取选定对象的列表作为字符串 Blender python的主要内容,如果未能解决你的问题,请参考以下文章
如何将 discord.py remove_roles 用于多个角色? (作为参数的对象列表)