I want to list some blend files from a folder in the UI. Reading the folder content already works. The output lands in a list. Now I need to display the content of this list in a template_list box. I am totally lost with this template_list.

My problem is basically:

mylist = []
row.template_list(what has to stand here to display the content of my list?)

So how do I get the list content into my template_list?

The as much as possible reduced script is at the moment in this state:

import bpy
import os

 # -----------------------------------------------------------------------------------------------------
 # the panel is in the properties sidebar.  
 # -----------------------------------------------------------------------------------------------------

mylist = []

class VIEW3D_read_dir(bpy.types.Operator):
    bl_idname = "view3d.read_dir"
    bl_label = "Read Dir"

    def execute(self, context):

        mylist = [] # empty the list
        start_path = 'C:/Users/x/Documents' # current directory

        for path,dirs,files in os.walk(start_path):
            for filename in files:
                if filename.endswith(".blend"):
                    mylist.append(os.path.join(filename)) # fill the list
        print (mylist) #debugging
        return {'FINISHED'}  

class MyPanel(bpy.types.Panel):
    bl_label = "Testpanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'

    def draw(self, context):
        layout = self.layout

        layout.operator("view3d.read_dir", text="read directory") # Button to read the directory

        scene = context.scene
        row = layout.row()

        row.template_list("UI_UL_list", "keying_sets", scene, "keying_sets", scene.keying_sets, "active_index", rows=1) #Grabbed from Keying Sets to have a template list to play with

# ------------------------------ register unregister --------------------------------------------------------  

def register():


def unregister():


if __name__ == "__main__":
  • $\begingroup$ Have a look at blender.org/api/blender_python_api_current/… the scripts can be found under the "UIList Simple" (ui_list_simple.py) template in the templates > python menu of the text editor $\endgroup$ – batFINGER Oct 15 '16 at 6:06
  • $\begingroup$ Thanks batFINGER. Seems to be a bigger operation than thought. I will go through the example now. $\endgroup$ – Tiles Oct 15 '16 at 6:29
  • $\begingroup$ I am still lost, sorry. I don't want to load materials. I want to load the content of my already existing list. $\endgroup$ – Tiles Oct 15 '16 at 7:04
  • 1
    $\begingroup$ Takes a while to properly sync the properties. I'll come back when I got more time @Tiles. $\endgroup$ – p2or Oct 17 '16 at 19:59
  • 1
    $\begingroup$ Ok, done @Tiles. Here a gist for testing purposes. It's basically the code of this answer (adds a custom panel to the TEXT_EDITOR) combined with an ImportHelper to get multiple blend files at once. This is what you want right? $\endgroup$ – p2or Oct 22 '16 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.