【问题标题】:How to get my variable out of this loop? - Blender如何让我的变量脱离这个循环? - 搅拌机
【发布时间】:2016-02-20 23:26:59
【问题描述】:

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

它可以工作,但只会打开列表中的最后一个 obj。

我该如何解决?我想让这个工作!

到目前为止我的代码:

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 上,因为在绘制按钮的循环之后,它具有列表中最后一项的值。但我不知道如何解决它。

【问题讨论】:

    标签: python object for-loop import blender


    【解决方案1】:

    我还没有加载代码来测试它,但据我所知,newbutton 是一个变量。 for 循环一遍又一遍地设置相同的变量。这就是为什么您只能获得列表中的最后一个值。

    您可能想要做的是定义一个实例化您的对象的函数。该函数将需要将该对象构建到场景中所需的所有参数。为循环的每次迭代调用该函数将使用预期数据为场景实例化一个新对象,因为每个对象都会封装您传递给它的参数。

    希望对你有帮助!

    【讨论】:

    • 感谢他们的回复!所以我想为列表中的每个文件都有一个按钮,这样当我单击其中一个按钮时,我可以单独导入它们。所以我不希望通过运行脚本来实例化它们,而是通过单击右键来实例化它们。有什么建议吗?
    【解决方案2】:

    在搅拌机的界面中,一个按钮链接到一个操作员,单击该按钮会使操作员执行它的任务。与其为每个按钮生成一个新的操作符,更好的方法是向操作符添加一个属性并设置用于每个按钮的属性。

    通过将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
    

    【讨论】:

    • 非常感谢!我需要 self.objfile - 这成功了! :)
    猜你喜欢
    • 2018-02-11
    • 2016-04-22
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    相关资源
    最近更新 更多