Edit "List Item" component output names through Python

Hi all,
Is there a way to change the names of the output parameters of the “List Item” component (default name = “i”, “+1”…) through python (or C#)? for example in the image below, I have a list with 19 outputs, and I am hoping to automatically rename the output names to the list on the right.
image

Thanks in advance!

the output of List Item is not a name assigned to each item, but it’s just the index (position) of the item itself in the data structure of the List Item input L

maybe it could be helpful to know what use you intend to do for each name?
is it just for “visual labeling” purposes, so you could see each value with its name tag, or those data will be used in a custom script component or… ? :slight_smile:

Thanks,
I am completely OK with a custom component actually. The input will be 19 values in this case - I want to set the label for each value for visual purposes.
So for example, “i” has a value of 0.6 currently. I want to rename “i” to “WWR”. If this is possible through a custom component - that works too.

I found another thread that does something similar but having difficulties in running the script correctly. https://www.grasshopper3d.com/forum/topics/variable-input-parameters-ghpython

This is what I am doing. While it adds outputs with the correct names (from list input x), it does not assign the relative values (from list y). Also, every time I run, it creates additional output parameters.

import rhinoscriptsyntax as rs
import Grasshopper.Kernel as kr
import Rhino.Geometry as rg

def AddParam(name):
param = kr.Parameters.Param_String()
param.NickName = name
param.Name = name
param.Access = kr.GH_ParamAccess.list
index = ghenv.Component.Params.Output.Count
ghenv.Component.Params.RegisterOutputParam(param)
ghenv.Component.Params.OnParametersChanged()

for i in range(0,len(y)):
var_name = str(x[i])
AddParam(var_name)
exec(“%s = %s” %(var_name,y[i]))

IDK if it’s useful for your case, but this is easy enough to do with metahopper:

2 Likes

Thanks @andheum. This is good to know. For this one - I am trying to avoid additional plug-ins and wanted to do the task using native GH components or max with python/c#

in c#, this works

using System.Linq;
var itemComp = Grasshopper.Instances.ActiveDocument.ActiveObjects().First(o => Component.Params.Input[0].DependsOn(o)) as GH_Component;
var outputs = itemComp.Params.Output;
for(int i=0;i<outputs.Count;i++) {
    var output = outputs[i];
    var name = Names[i];
    output.NickName = name;
 }
 itemComp.Attributes.ExpireLayout();
A = itemComp;
1 Like

wow… seems super compact!
But… it doesn’t work on windows… (?)

can you show the error on windows?

Translating from italian:

  1. Error (CS0117): ‘Grasshopper.Instances’ non contiene una definizione per ‘ActiveDocument’. (line 58)

  2. Error (CS0117): ‘Grasshopper.Instances’ does not contain a definition for ‘ActiveDocument’. (line 58)

Ah, okay. Replace Grasshopper.Instances.ActiveDocument with Grasshopper.Instances.ActiveCanvas.Document, I think

var itemComp = Grasshopper.Instances.ActiveCanvas.Document.ActiveObjects().First(o => Component.Params.Input[0].DependsOn(o)) as GH_Component;
var outputs = itemComp.Params.Output;
for(int i=0;i<outputs.Count;i++) {
    var output = outputs[i];
    var name = Names[i];
    output.NickName = name;
 }
 itemComp.Attributes.ExpireLayout();
A = itemComp;
1 Like

Super!

Hi @andheum nice method to rename, with a similar method could you help me to edit the buttonname in c#.

I edited your code to work with standard components but this doesn’t work with the button component. Any advice I would appreciate, thank you!

var itemComp = Grasshopper.Instances.ActiveCanvas.Document.ActiveObjects().First(o => Component.Params.Input[0].DependsOn(o)) as GH_Component;
var nameComp = itemComp.Attributes.GetTopLevel.DocObject as IGH_Component;
{
  var _name = nameComp;
  var name = N;
  _name.NickName = name;
}
itemComp.Attributes.ExpireLayout();

The Button is not an IGH_Component, it’s an IGH_Param, but this should be an easy change in the script.

var button = Grasshopper.Instances.ActiveCanvas.Document.ActiveObjects().First(o => Component.Params.Input[0].DependsOn(o)) as IGH_Param;
button.NickName = N; // assumes you have a string input called N
button.Attributes.ExpireLayout();

Also, the technique shown above with Metahopper should work well for a button — you can use it to rename any object. If you don’t explicitly need to script, I’d recommend that.

1 Like

Thank you @andheum very clear, I was working on a false start button like your toggle,
this requires connecting the button twice, but works,

private void RunScript(string x, bool y, ref object A)
{

var button = Grasshopper.Instances.ActiveCanvas.Document.ActiveObjects().First(o => Component.Params.Input[0].DependsOn(o)) as IGH_Param;
if(y){
  bo = bo ? false : true;
}
button.NickName = bo.ToString();
button.Attributes.ExpireLayout();
A = bo;

}
//
public bool bo;