1

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):
    """Blubb"""
    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():

    bpy.utils.register_module(__name__)

def unregister():

    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()
Leander
  • 26,725
  • 2
  • 44
  • 105
Tiles
  • 2,028
  • 16
  • 27
  • Have a look at https://www.blender.org/api/blender_python_api_current/bpy.types.UIList.html the scripts can be found under the "UIList Simple" (ui_list_simple.py) template in the templates > python menu of the text editor – batFINGER Oct 15 '16 at 06:06
  • Thanks batFINGER. Seems to be a bigger operation than thought. I will go through the example now. – Tiles Oct 15 '16 at 06:29
  • I am still lost, sorry. I don't want to load materials. I want to load the content of my already existing list. – Tiles Oct 15 '16 at 07:04
  • The materials collection is used as an example of a blender collection. You can make a custom collection from your list using a CollectionProperty https://www.blender.org/api/blender_python_api_current/bpy.props.html#collection-example A blender collection is used in a UIList widget, not a simple python list. – batFINGER Oct 15 '16 at 07:28
  • Wow, so complicated. No wonder is there no easy answer. Thanks. Will start some experiments now. – Tiles Oct 15 '16 at 08:01
  • @Tiles Do you mean something like: http://blender.stackexchange.com/questions/30444/create-an-interface-which-is-similar-to-the-material-list-box/30446#30446 How your list looks like? – p2or Oct 17 '16 at 18:58
  • Ah, another list example to study. Thanks @poor. I try still to wrap my head around how i could add my list content into the UIList element that Blender requires for the template_list. – Tiles Oct 17 '16 at 19:03
  • @poor , the code snippet is attached above. It's a simple list variable at the moment. mylist [] . And it contains the content of a folder. The blend files in there. – Tiles Oct 17 '16 at 19:07
  • @Tiles Do you need to return the file path for each item at the end? – p2or Oct 17 '16 at 19:09
  • @poor, In the template_list i need just the file name. I may need the file path though to load files at a later point. – Tiles Oct 17 '16 at 19:10
  • That's key to know, then we need to store it in another hidden property @Tiles – p2or Oct 17 '16 at 19:12
  • @poor. I see. I want to be able to append the content of the blend file at a later point. So yes, we might need the hidden property. I try to develop a little asset manager just for lights. – Tiles Oct 17 '16 at 19:14
  • 1
    Takes a while to properly sync the properties. I'll come back when I got more time @Tiles. – p2or Oct 17 '16 at 19:59
  • 1
    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? – p2or Oct 22 '16 at 12:31
  • @poor, Yes. This is a perfect example. Many thanks for your efforts :) – Tiles Oct 22 '16 at 16:00
  • Great, then I'll write an answer in the next days... – p2or Oct 22 '16 at 17:00

0 Answers0