0
$\begingroup$

I have this operator code which I call with layout.operator("wm_filetype_source_operator") function in my Panel class:

import bpy

class EasyImport_OT_File_Type_Op(bpy.types.Operator):

    bl_label = "File Type"
    bl_idname = "wm.filetype_source_operator"

    filetype_enum : bpy.props.EnumProperty(
        name = "",
        description = "Select an Source",
        items = [
            ('OP1', "FBX", "Set FBX as File Type"),
            ('OP2', "GLTF", "Set GLTF as File Type"),
            ('OP3', "BLEND", "Set BLEND as File Type"),
            ('OP4', "ABC", "Set ABC as File Type")
        ]
    )

    def invoke(self, context, event):
        wm = context.window_manager
        return wm.invoke_props_dialog(self)

    def draw(self, context):
        layout = self.layout
        layout.prop(self, "filetype_enum")
        

    def execute(self, context):

        global filetype
        if self.filetype_enum == 'OP1':
            filetype = 'FBX'
        elif self.filetype_enum == 'OP2':
            filetype = 'GLTF'
        elif self.filetype_enum == 'OP3':
            filetype = 'BLEND'
        elif self.filetype_enum == 'OP4':
            filetype = 'ABC'

        return {'FINISHED'}

The script works fine, but I cannot figure out how to save the filetype variable outside of the function.

So far I have tried the following:

  • Set filetype variable to global

  • Define the filetype variable outside the class and set it to global

  • Return the filetype variable

None have worked and there doesn't seem to be any info on the doc about how to do this

$\endgroup$

2 Answers 2

1
$\begingroup$

It's usually a bad idea to use global variables. I think it would be more straightforward to display your property in a panel. You can attach it to the current scene for instance.

import bpy


class HelloWorldPanel(bpy.types.Panel):
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        self.layout.prop(context.scene, "my_filetype")
        


def register():
    bpy.utils.register_class(HelloWorldPanel)
    bpy.types.Scene.my_filetype= bpy.props.EnumProperty(
        name="Filetype", 
        items=(("FBX",) * 3, ("GTLF",) * 3,("BLEND",) * 3,("ABC",) * 3))



if __name__ == "__main__":
    register()

That way you can access it directly with bpy.context.scene.my_filetype.

enter image description here

It creates a panel in the scene properties, but you can layout the prop anywhere in your custom UI. If you absolutely want to use an operator, you can just modify the draw method :

    def draw(self, context):
        layout = self.layout
        layout.prop(context.scene, "my_filetype")
$\endgroup$
-1
$\begingroup$

Simple fix You need to put the global variable inside of each if statement.

if self.filetype_enum == 'OP1':
    global filetype = 'FBX'
    filetype = 'FBX'

and then just repeat that for each elif statement.

Your welcome.

$\endgroup$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .