簡體   English   中英

獲取所選對象的列表作為字符串Blender python

[英]get list of selected objects as string Blender python

我面臨一個相當容易解決的問題,但我不知道該怎么做。 我希望blender列出所有被選為字符串的對象。 例如。 如果我跑:

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

解決這個問題的最快方法是通過列表理解:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM