gpt4 book ai didi

python - 如何让我的变量脱离这个循环? - blender

转载 作者:太空宇宙 更新时间:2023-11-04 03:23:40 24 4
gpt4 key购买 nike

我正在尝试制作一个脚本,该脚本读取 .txt(存储 .obj 名称的位置),然后在 blender 中制作服装 - 按钮。如果单击其中一个按钮,它应该会根据 txt 中的名称打开文件。

它有效,但只会打开列表中的最后一个对象。

我该如何解决?我想让它起作用!

到目前为止我的代码:

import bpy 

class directoryPan(bpy.types.Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_label = "Biblio"
bl_category = "Import" #

def draw(self, context):

self.layout.row().label("Import :")
self.layout.operator("import.stuff", icon ='FILE')
obj_list = []

biblio_one = open("C:\\Users\\Jasmin\\Desktop\\liste.txt")

for line in biblio_one:
obj_list.append(line.rstrip())
biblio_one.close()

print("start")

for i in obj_list:
newbutton = i
import_obj = "import." + i

self.layout.operator(import_obj, icon ='FILE')
######
class ScanFileOperator(bpy.types.Operator):
bl_idname = import_obj
bl_label = newbutton

def execute(self, context):

pfad = "C:\\Users\\Jasmin\\Desktop\\" + newbutton+ ".obj" ###

bpy.ops.import_scene.obj(filepath= pfad, filter_glob="*.obj;*.mtl", use_ngons=True, use_edges=True, use_smooth_groups=True, use_split_objects=True, use_split_groups=True, use_groups_as_vgroups=False, use_image_search=True, split_mode='ON', global_clamp_size=0, axis_forward='-Z', axis_up='Y')
bpy.ops.object.origin_set(type = 'GEOMETRY_ORIGIN')

return {'FINISH'}
def register():
bpy.utils.register_module(__name__)


def unregister():
bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
register()

我知道问题出在 newbutton,因为在绘制按钮的循环之后,它具有列表中最后一项的值。但我不知道如何修复它。

最佳答案

在 blender 的界面中,一个按钮链接到一个运算符(operator),单击该按钮会使运算符(operator)执行其任务。与其为每个按钮生成一个新的运算符,更好的方法是向运算符添加一个属性并设置用于每个按钮的属性。

通过添加 bpy.props对于运算符类,您将获得一个属性,可以在面板中显示时为每个按钮设置该属性,然后可以在运行运算符时访问该属性。

class ScanFileOperator(bpy.types.Operator):
'''Import an obj into the current scene'''
bl_idname = 'import.scanfile'
bl_label = 'Import an obj'
bl_options = {'REGISTER', 'UNDO'}

objfile = bpy.props.StringProperty(name="obj filename")

def execute(self, context):
print('importing', self.objfile)

return {'FINISHED'}

class directoryPan(bpy.types.Panel):
bl_space_type = "VIEW_3D"
bl_region_type = "TOOLS"
bl_label = "Biblio"
bl_category = "Import"

def draw(self, context):

col = self.layout.column()
col.label("Import :")

obj_list = []
biblio_one = ['obj1','obj2','obj3','obj4','obj5',]

for line in biblio_one:
obj_list.append(line.rstrip())

for i in obj_list:
import_obj = "import." + i

col.operator('import.scanfile', text='Import - '+i,
icon ='FILE').objfile = import_obj

关于python - 如何让我的变量脱离这个循环? - blender ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33793414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com