2014-12-03 77 views
2

我面臨一個相當容易解決的問題,但我只是不知道該怎麼做。我希望攪拌器列出所有選作字符串的對象。例如。如果我運行:獲取選定對象的列表作爲字符串Blender python

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'] 

回答

4
>> 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'] 
+0

感謝大衛:) – 2015-05-11 01:48:33

3

解決,最快的辦法是通過列表理解:

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) 
+0

歡呼爲:P – 2017-03-10 12:47:33

相關問題