Addon with menu select some TXT file how to?

any example showing how to use a panel addon
with button to select a TXT file in local folder ?

i need to show all TXT file in local folder and select one
then get that file name so i can open it up and read it

thanks for feedback
happy bl

Combine this:
https://pynative.com/python-list-files-in-directory-with-extension-txt/#:~:text=Use%20the%20os.,all%20files%20from%20a%20list.
With this:
https://blender.stackexchange.com/questions/167862/how-to-create-a-button-on-the-n-panel

first part is working
i can list the TXT files

second link is too general
i already have some panel addon

remaining problem how to have some sort of dynamic menu button to list the TXT files and select one specific file
and get the file’s name

thanks
happy bl

found this template
ui_previews_dynamic_enum.py included in blender

but when i run on the local folder
it returns only //

not certain how this works or should work

anyone can explain how it is supposed to work
can it list only TXT files may be somehow ?

thanks
happy bl

I believe this is close to the absolute minimal number of lines of code you’d need for that. It will create a panel in the object properties that lets you choose a txt file in your current file directory.

import bpy


class SelectFilePathOperator(bpy.types.Operator):
    bl_label = "Select Text File"
    bl_idname = "file.select_my_filepath"
    filepath: bpy.props.StringProperty()
    filter_glob: bpy.props.StringProperty(
        default="*.txt",
        options={"HIDDEN"},
        maxlen=255,  # Max internal buffer length, longer would be clamped.
    )

    def invoke(self, context, event):
        self.filepath = bpy.data.filepath
        context.window_manager.fileselect_add(self)
        return {"RUNNING_MODAL"}

    def execute(self,context):
        print(self.filepath)
        return {"FINISHED"}


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 = "object"

    def draw(self, context):
        self.layout.operator("file.select_my_filepath")


bpy.utils.register_class(SelectFilePathOperator)
bpy.utils.register_class(HelloWorldPanel)
1 Like

getting close

now ounce you select a file
how do you get the file’s name from the menu selection ?

i need that name to read that selected file

thanks
happy bl

just saw name on console

i added these lines to get path and file name

		print(self.filepath)
		head, tail = os.path.split(self.filepath)
		print ('head = ',head)
		print ('tail = ',tail)
		print ()

now is it possible to have another panel and passing the file name to another panel

may be using a scene property !

thanks
happy bl

1 Like

Sure in your register you can do something like that :
bpy.types.Scene.my_filename = bpy.props.StringProperty()
This will create a property on all scenes called my_filename

and then in your execute :

context.scene.my_filename = tail

i change the register section

	
	
classes = (
	HelloWorldPanel,
	SelectFilePathOperator , 
)
	
	
def register():
	
	from bpy.utils import register_class
	
	bpy.types.Scene.my_filename = bpy.props.StringProperty()
	
	for cls in classes:
	
		register_class(cls)
	
	
def unregister():
	
	from bpy.utils import unregister_class
	
	for cls in reversed(classes):
	
		unregister_class(cls)
	
###
	
if __name__ == "__main__":
	register()
	

have to try this new scene prop in my other addon i’m working on and see how it works with this scene property

is there an un register for the scene prop ?

thanks
happy bl

@Gorgiou

do you know if this panel could work in the UI window tool tab
some panel function might not work in the UI panel ?

working on some Group prop and having some bug
have to work a bit on this to resolve that bug

thanks
happy bl

done a more elaborate panel base on the simpler working one here

and it show the right folder but no TXT files shown

got the panel and operator for filepath
and it is like the filter is not working

any idea why it would not filter it ?

also is there more doc on that filter command
can you do more elaborate filter
like file starting like PRdata*.txt ?

thanks
happy bl

To unregister you simply call del bpy.types.Scene.my_filepath. For the other questions I’d have to have a look at your code to tell you more.

i reviewed the code this morning and was able to correct it
now it works fine to open TXT file in new Addon

will keep going adding new features

can you explain how this filter glob thing works ?
any other way to filter things ?

thanks
happy bl

got a label problem in GUI panel

Txmes1 = my_prop.mat2 + ’ TXT File name ’

self.layout.label(text= Txmes1)

is it possible to pass the content of a string prop
to a label some how ?

i cant print the prop value but it is not showing the right value

i had already assign some value to it but refuse to show it in panel

file is getting a bit big
i could upload it to pastal and PM link if needed

but there is still a lot of useless things inside !
which i will clean up alter on

thanks
happy bl

According to the docs :
https://docs.blender.org/api/current/bpy.types.FileSelectParams.html?highlight=filter_glob#bpy.types.FileSelectParams.filter_glob

UNIX shell-like filename patterns matching, supports wildcards (‘*’) and list of patterns separated by ‘;’

So you can define several patterns that are filtered automatically. Use ‘*’ as a wildcard. For your specific need replace the default argument in filter_glob with :

default="*PRdata*.txt",

(I don’t know why the first wildcard is needed but it doesn’t work if you don’t use a wildcard on the first character.)

Regarding the string prop, what exactly doesn’t work ? It throws an error ? It is empty ? assuming my_prop.mat2 is a StringProperty it should already be converted to a string when accessed. In any case you can cover yourself with str(my_prop.mat2) to force casting it to a string.

1 Like

group string prop

i can assign some value in main part
and can print it - works fine in main !

but in panel

cannot work with string prop
i can work with float checkbox but not string prop !

here is

		print (' @@@@@@@@@  = ' , str(my_prop.mat2) )
		Txmes1 = str(my_prop.mat2) + ' TXT  File name '
		self.layout.label(text= Txmes1)



or this one 


		print (' @@@@@@@@@ str1  = ' , bpy.context.scene.my_prop.str1 )
		print (' @@@@@@@@@ str1  = ' , my_prop.str1 )
	

printed on console from inside panel

In class Panel Group Class MyCkeckbox1 = True
@@@@@@@@@ str1 =
@@@@@@@@@ str1 =

also for label it should accept a string var but may be i’m wrong !

on console the string is empty and no error
tried with the STR and still empty

the string prop is

str1 : bpy.props.StringProperty(description="Plane / Cube",
		options={'SKIP_SAVE'})

thanks
happy bl

that looks like several bugs !

tried it in bl 3.0 and it works better
but still cannot get the assigned value to string in panel
i get only the default values

happy bl