0

I have the following code to enable the file browser using blender:

import bpy
import os
from bpy.props import StringProperty
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator
sel = ''
class OpenBrowser(bpy.types.Operator):
    bl_idname = "open.file"
    bl_label = "Select Excel File"
    bli_description = "Simulation output excel file"
    filter_glob: StringProperty(default = '*.xls;*.xlsx',options = {'HIDDEN'})
    filepath: bpy.props.StringProperty(subtype="FILE_PATH")
    #somewhere to remember the address of the file

    def execute(self, context):
        global sel
        sel = self.filepath 
        #self.selected_file = self.filepath
        #display = "filepath= "+self.filepath  
        #print(display) #Prints to console  
        #Window>>>Toggle systen console

        return {'FINISHED'}

    def invoke(self, context, event): # See comments at end  [1]
        context.window_manager.fileselect_add(self)
        global sel 
        sel = self.filepath
        #Open browser, take reference to 'self' 
        #read the path to selected file, 
        #put path in declared string type data structure self.filepath

        return {'RUNNING_MODAL'}  
        # Tells Blender to hang on for the slow user input


bpy.utils.register_class(OpenBrowser) 
#Tell Blender this exists and should be used


# [1] In this invoke(self, context, event) is being triggered by the below command
#but in your script you create a button or menu item. When it is clicked
# Blender runs   invoke()  automatically.

#execute(self,context) prints self.filepath as proof it works.. I hope.

bpy.ops.open.file('INVOKE_DEFAULT')
print(sel)

The issue I am facing is that I have declared a global variable sel to which I want to save the filepath selected from the user when running the code. However, when I run the script I see that sel has not changed and it is as it was initialized. Could someone please help me on how to access from the class the self.filepath variable? What am I doing wrong here?

2
  • Where does an instance of OpenBrowser get created? Commented Feb 18, 2021 at 0:07
  • It is called from bpy.ops.open.file('INVOKE_DEFAULT') which calls the invoke function. Commented Feb 18, 2021 at 0:08

1 Answer 1

1

If I understand correctly, you want to store that value for later. I'm not sure why 'sel' doesn't even update in your case, but I think the more correct way would be to use a property like so:

import bpy

# Assign a custom property to an existing type.
bpy.types.Scene.my_sel_value = bpy.props.StringProperty(name="Sel")

# Set property value.
bpy.context.scene.my_sel_value = "Foo"

# Get property value.
print(bpy.context.scene.my_sel_value)

Properties can be added to all ID types, but for "global" values, bpy.types.scene us ussualy used. Though there can be multiple scenes in one project and they will have separate values. Property values are stored when Blender closes.

If you are making an addon, you can also store your value in Addon Preferences. This value will be the same for all blender projects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.