Unable select item on Grasshopper Dropdown List

Hi All,

I am trying to modify the data in grasshopper value list using python.

Does anyone know why I cant select the item in the list?

Python_Grasshopper_ValueList.gh (3.4 KB)

Hi,

Each time to try to change the item, it triggers the Python component downstream, which, as shown in the code, clears all the items and stores new ones. With this process, the value list automatically goes back to the first item as the selection.

Either store the selected index before removing all of them, or add a button to your component to trigger the update, or disconnect your value list once the update of the values is complete.

The intention is I will be updating the ‘database’ from time to time. So I hope to have the python component link to my value list. Can you reckon how shall I modify my code to allow for such changes?

"""
Populate Value List
    Inputs:
        Name: {item,str}
        Keys: {list,str}
        Values: {list,str}
    Outputs:
    Remarks:
        Author: Anders Holden Deleuran (BIG IDEAS)
        Rhino: 6.30.20266.14531
        Version: 210108
"""

ghenv.Component.Name = "PopulateValueList"
ghenv.Component.NickName = "PVL"

import Grasshopper as gh

if not Values:
    Values = Keys
if Keys and Values:
    for obj in ghenv.Component.OnPingDocument().Objects:
        if type(obj) is gh.Kernel.Special.GH_ValueList:
            if obj.NickName == Name:
                selected = obj.FirstSelectedItem.Name
                obj.ListItems.Clear()
                for k,v in zip(Keys,Values):
                    vli = gh.Kernel.Special.GH_ValueListItem(str(k),str('"' + v + '"'))
                    obj.ListItems.Add(vli)
                if selected in Keys:
                    obj.SelectItem(Keys.index(selected))
                obj.ExpireSolution(True)

PVL_2023Mar19a.gh (8.6 KB)