I have the following code to create a file browser:
import bpy
from bpy.props import StringProperty
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator
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")
selected_file = 'No file selected'
#somewhere to remember the address of the file
def execute(self, context):
self.selected_file = 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)
#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(OpenBrowser.selected_file)
My issue is that I would except from this code to print the file path, but I get the initialization of the global class variable ('No file selected'). Could you please tell me how to access the filepath from this bpy operator?
EDIT AFTER BATFINGER'S SUGGESTION:
WINDOWS MANAGER LAST TIME PROPS:
As BatFinger suggested this does not work in a script, since the return value will be no filepath. In the console it works fine.
CLASS METHOD APPROACH:
I then used the following script:
import bpy
from bpy.props import StringProperty
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator
import time
sel = 'global'
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")
@classmethod
def save_filepath(cls,filepath):
cls.save_filepath = filepath
return cls.save_filepath
def __init__(self):
print("Start")
def __del__(self):
print('End')
def execute(self, context):
global sel
#self.save_filepath(self.filepath)
sel = self.filepath
print('2 '+sel)
#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):
context.window_manager.fileselect_add(self)
#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'}
bpy.utils.register_class(OpenBrowser)
bpy.ops.open.file('INVOKE_DEFAULT')
print(OpenBrowser.__class__.save_filepath)
The print at the end causes an error because no attribute save_filepath
is detected (please correct me if i am doing something wrong).
Last approach I could check is with the check method
, but I am not sure how to implement it. In the meantime, if you have a script that works and it is able to get the filepath from the Operator's class into the main script body, I would be more than grateful if you could share it.