Generated ValueList not working

Hi everyone,

I’m fairly to GH developement and I’m having a problem with reading input value from a generated ValueList.

I want to make a custom component, which would, when dropped on canvas, generate a ValueList, connect the list to the component’s input parameter. I have managed to generage the ValueList and connect it to the component, but I don’t know how to read the values from the list as Input Parameters.

I guess, I need to create the ValueList before registering the input parameter, but I don’t know how.

my Visual Studio C# code:

protected override void RegisterInputParams(GH_InputParamManager pManager)
        {
            pManager.AddTextParameter("Group", "Group", "Component Group", GH_ParamAccess.item);
        }

        protected override void RegisterOutputParams(GH_OutputParamManager pManager)
        {
            pManager.AddTextParameter("Component", "Component", "Component", GH_ParamAccess.item);
        }

        protected override void BeforeSolveInstance()
        {
            Component = this;
            GrasshopperDocument = OnPingDocument();

            var grpList = new Grasshopper.Kernel.Special.GH_ValueList();
            var groups = Compdata.GetMaterialsList();

            grpList.CreateAttributes();
            grpList.Name = "Group";
            grpList.NickName = "Group";
            grpList.ListMode = Grasshopper.Kernel.Special.GH_ValueListMode.DropDown;
            grpList.Attributes.Pivot = new PointF(Attributes.Pivot.X - 400, Attributes.Pivot.Y - 10);
            grpList.ListItems.Clear();

            foreach (var item in groups)
            {
                grpList.ListItems.Add(new Grasshopper.Kernel.Special.GH_ValueListItem(item, item.ToString()));
            }

            GrasshopperDocument.AddObject(grpList, false);
            Params.Input[0].AddSource(grpList);           
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            string selected = null;
            if (!DA.GetData(0, ref selected)) { return; }
            DA.SetData("Component", selected);
        }

Is my idea even possible?

Defo no expert here, but if the issue is as you suspect creating the ValueList before registering the input parameters, cant you call ExpireSolution(true), so it re runs the solution with the data the correct way, although it will create an new value list component so perhaps try checking if the valuelist exists, if not then create it, else get its value?

Also dunno if this is a thing in c# but in vb i have always used:
DA.SetData(0, selected);

I noticed that the generated ValueList doesn’t give its output parameter.

image

Do I need to set all the items in ValueList as output parameters?

1 Like

Is it being re-created every time the solution re-computes?

try changing:

GrasshopperDocument.AddObject(grpList, false);

to:

GrasshopperDocument.AddObject(grpList, true);

GrasshopperDocument.AddObject(grpList, true);

This gets me stuck in an infinite loop :frowning:

I create a pre-populated value list in Human UI - you can see the source here

private void createAccentList(object sender, System.EventArgs e)
        {
            GH_DocumentIO docIO = new GH_DocumentIO();
            docIO.Document = new GH_Document();

            //initialize object
            GH_ValueList vl = new GH_ValueList();
            //clear default contents
            vl.ListItems.Clear();
            //add all the accent colors as both "Keys" and values
            foreach (string color in ACCENT_COLORS)
            {
                GH_ValueListItem vi = new GH_ValueListItem(color, String.Format("\"{0}\"", color));
                vl.ListItems.Add(vi);
            }
            //set component nickname
            vl.NickName = "Accent Colors";
            //get active GH doc
            GH_Document doc = OnPingDocument();
            if (docIO.Document == null) return;
            // place the object
            docIO.Document.AddObject(vl, false, 1);
            //get the pivot of the "accent" param
            PointF currPivot = Params.Input[3].Attributes.Pivot;
            //set the pivot of the new object
            vl.Attributes.Pivot = new PointF(currPivot.X - 120, currPivot.Y - 11);

            docIO.Document.SelectAll();
            docIO.Document.ExpireSolution();
            docIO.Document.MutateAllIds();
            IEnumerable<IGH_DocumentObject> objs = docIO.Document.Objects;
            doc.DeselectAll();
            doc.UndoUtil.RecordAddObjectEvent("Create Accent List", objs);
            doc.MergeDocument(docIO.Document);
            //doc.ScheduleSolution(10);
        }

Check if the valuelist exists, then if not create it in your BeforeSolveInstance method. so you only create it once

Hello,
I had the same problem. What I have find out inspired by post by Andrew Heumann, is that if you add quotation marks it works.
So when you format your value while creating GH_ValueListItem like Andrew did, it probably works for you.