Dynamic Menu/Listbox or load external list in Menu/Listbox/Something

I try without success to create an operator that load from an external list some data end use it in the interface.

After some research I came with this script.
Add > Dynamic Menu , then in Toolbar appears two listbox.

The BIG problem is that can’t select any voice in the second listbox, and I don’t get any error on the console

Below the script: to make it work you must have a folder named object and change “scriptPath” to point that. Here the contents of object folder.

Fruit.py


print("EXEC PRESET Fruit
")
props.do_list = ("apple","Apple",""),("pear","Pear",""),("orange","Orange","")

Vegetables.py


print("EXEC PRESET Vegetables
")
props.do_list = ("tomato","Tomato",""),("salad","Salad",""),("carrot","Carrot","")

How to make it work? If someone have any idea please, let me know.


import os, sys
os.system("cls") # clear console screen

import bpy
from bpy.props import *


def getPresets():
    scriptPath = "C:\Documents and Settings\XXXXXXXX\Desktop" #os.path.dirname(__file__)
    print(scriptPath)
    presetPath = os.path.join(scriptPath, "objects")
    print(presetPath)
    presetFiles = os.listdir(presetPath)
    #presetFiles.sort()
    presets = [(presetFile, presetFile.rpartition(".")[0], presetFile)
                for i, presetFile in enumerate(presetFiles) if presetFile.endswith(".py")]

    #print(presets)
    return presets, presetPath


def setProps(props, preset, presetsPath):
    #bpy.ops.script.python_file_run(filepath=presetsPath + '\\' + preset)
    file = open(os.path.join(presetsPath, preset))
    for line in file:
        exec(line)
    file.close()
    return

class add_mesh_dynobj(bpy.types.Operator):
    bl_idname = "mesh.dynobj_add"
    bl_label = "Add Objects"
    bl_options = {'REGISTER', 'UNDO'}
    bl_description = "add something"
    
    presets, presetsPath = getPresets()
    
    print(presets)
    print(presetsPath)

    first = presets[0]
    first_preset = first[0]
    
    do_presets = EnumProperty(attr='do_presets',
            name='Type',
            description="Type",
            default=first_preset,
            items=presets)
    
    last_preset = None
    last_part = None

    def update(self):
        print("UPDATING...")
        sce = bpy.types.Scene
        if True:
            print("UPDATE PART LIST...")
            tmp=self.do_list[0]
            first=tmp[1]
            print(first)
            sce.do_parts = EnumProperty(
                    name="Subtype",
                    description="Subtype",
                    items=self.do_list)
            self.last_part = sce.do_parts
    
    def draw(self, context):
        print("DRAW INTERFACE...
")
        layout = self.layout
        col = layout.column()
        sce = bpy.context.scene
        
        col.prop(self, 'do_presets')
        col.separator()
        col.prop(sce, 'do_parts')

    def execute(self, context):
    
        print('EXECUTING...')
        print(str(self.do_presets)+"="+str(self.last_preset))
        if not self.last_preset or self.do_presets != self.last_preset:
            print('setting Preset', self.do_presets)
            setProps(self, self.do_presets, self.presetsPath)
            self.last_preset = self.do_presets
            print(self.do_list)
        self.update()
        return {'FINISHED'}
        
    def invoke(self, context, event):
        print('
___________START_____________')
        self.execute(context)
        return {'FINISHED'}

def add_mesh_dynobj_button(self, context):
    self.layout.operator(add_mesh_dynobj.bl_idname, text="Dynamic Menu", icon="PLUGIN")

def register():
    bpy.utils.register_module(__name__)
    bpy.types.INFO_MT_mesh_add.append(add_mesh_dynobj_button)

def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.INFO_MT_mesh_add.remove(add_mesh_dynobj_button)
    
if __name__ == "__main__":
    register()

Odd…my guess is that you are actually defining the enum in the update. It should be defined outside the update, right?

intersting way to getting data

did you succeed to make it work?

hope it does work fine

thanks happy 2.5

Thank you for your interest, but maybe I didn’t explain it well… but just for information it DOESN’T work.

This is what I want to do:

  • I would like to list or show in menu some categories (in my script are preset filename)
  • categories are limitless, just add a file in the folder and then a new category appears
  • any category has items, they are in external files so I can add them easily
  • when i pick up a category i get a list of all the items in it

The script seems to work but I can’t select a value in the second listbox.

@Atom if I define outside the update the enum property doesn’t update…

This script is made from paste and copy other code snippets/examples/addon found in the internet. But I didn’t actually fully understood how to make a working dynamic list/menu.

If anyone has an idea how to do that, pleeeease let me know.