def execute(self, context):
# set the string path for the file here.
# this is a variable created from the top to start it
bpy.context.scene.custompath = self.properties.path
print(self.properties.path) # display the file name and current path
# Call function to check file type and read file's datas
print ()
print ('File s name =', self.properties.path )
print ('bpy.context.scene.custompath =', bpy.context.scene.custompath )
after I select a file in current folder
it does not have the file’s name anymore
so how can this be corrected ?
also is there a way to separate the path - filename and the extension for the selected file ?
I need to check that the file extention is the right one !
I basically have same operator but mine is not working
not certain why !
try it
########
class OBJECT_OT_CustomPath(bpy.types.Operator):
global p
bl_idname = "object.custom_path"
bl_label = "Custom Path"
__doc__ = "OBJECT_OT_CustomPath"
#this can be look into the one of the export or import python file.
#settings
#need to set a path so so we can get the file name and path
# path = p1.StringProperty(name="file path", description="getting file path", maxlen= 1024, default= "")
filepath1 = bpy.props.StringProperty(subtype="FILE_PATH")
print(' before here filepath1 = ',filepath1)
def execute(self, context):
# set the string path for the file here.
# this is a variable created from the top to start it
# print(' here filepath1 = ',filepath1) # error
print(' here name filepath1 = ',self.properties.filepath1) # display the file name and current path
bpy.context.scene.custompath = self.properties.filepath1
print ( ' ok path =', self.filepath1 )
print ()
return {'FINISHED'}
# def invoke(self, context, event):
# wm = bpy.context.window_manager
# bpy.context.window_manager.fileselect_add(self)
def invoke(self, context, event):
bpy.context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
# return {'RUNNING_MODAL'}
#####
After a bit of testing and documenting I’ve managed to fully understand how the file selector works and I’ve broken it down for you in a simple example using your code as basis. Let me just explain a few facts first.
The file selector can return 4 properties:
filepath: full path of selected item (path+filename)
filename: name of selected item
directory: directory of the selected item
files: a collection containing all the selected items’ filenames (if you selected multiple files in the file selector)
So basically filepath = directory+filename.
If you selected multiple files, you can get the nth file’s path through: directory+files[n].name
The first 3 properties are of StringProperty type. It is recommended to set the subtype to ‘FILE_PATH’ as well as options to ‘HIDDEN’ and ‘SKIP_SAVE’ so that the properties cannot be manipulated (via drivers, IPOs…) or saved in presets, and to prevent further access.
The last is a CollectionProperty of a special type (OperatorFileListElement) which is not listed in the bpy.props elements but in bpy.types. It is also recommended to use the ‘HIDDEN’ and ‘SKIP_SAVE’ options for this property.
One last somehow unrelated thing: in your code you used self.properties.filepath1, I’ve never used the “properties” term and it’s always worked fine. I don’t really know if it’s necessary to use it…
Hopefully I made myself clear in explaining all this, sorry for the long post