2
$\begingroup$

Hello I am trying to create a list of blend files from a specific directory however I want to get the lists from a selected directory, which requires StringProperty to be used in the UI props. But in order to create a list, it needs to be initialized before def execute(self, context) but in order to get the information for the directory e.g. context.scene.prop_tool.example_tool, it requires it to be execute in the def execute(self, context) or else it returns the error calling context does not exist. How can I obtain the information for the properties without context?

example code:

class Properties(bpy.types.PropertyGroup):
    items = ()
    mytool = context.scene.my_tools #this code will not run due to this context outside execute
    location = bpy.path.abspath(mytool.directory)
    
    blends = [f for f in os.listdir(location) if f.endswith(".blend")]

    for blend in blends:
        blend_path = os.path.join(location, blend)
        with bpy.data.libraries.load(blend_path) as (data_from, _):
            for c in data_from.collections:
                collitem = (c, c, "")
                items = (collitem,) + items

    local_search : EnumProperty(
        name="Local Search",
        items = items
    )    
$\endgroup$
1
  • $\begingroup$ Please paste the text of your current code into your question. $\endgroup$ Commented Apr 4, 2022 at 0:20

1 Answer 1

1
$\begingroup$

You don't need a string property to get the list, you can use a string constant and pathlib:

from pathlib import Path

directory_name = Path(PATH_TO_DIRECTORY)
file_list = [file.name for file in directory_name.glob("*.blend")]

Will give you the list of blend files in the directory.

If you attach the directory name as a stringProperty, say DIRECTORY_NAME to something like bpy.types.Scene, you can initialize it in your register routine and give it a default value. Then you can reference it as bpy.context.scene.DIRECTORY_NAME if you're not in a routine that has a context argument or context.scene.DIRECTORY_NAME if you are.

$\endgroup$
2
  • $\begingroup$ Sorry I should have added an example code the file lists are for addon panel search lists as seen in the link I put in the post $\endgroup$
    – KripC2160
    Commented Apr 4, 2022 at 0:58
  • $\begingroup$ @KripC2160 You need to put your entire code into your question. If you want to do file selection, you don't want to do it with a PropertyGroup, but with the filebrowser but without knowing how this all fits together, it's impossible to show you specifically what to do. $\endgroup$ Commented Apr 4, 2022 at 3:54

You must log in to answer this question.

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