Hi I’m trying to write a code that adds or removes a input into the python scripting component and then add ValueList component to a specified input of the ghpython component. I’ve got the adding and removing of the inputs figured out but I’m having issue with the value list section of the code. If anyone can assist me with what I might be doing wrong, it’s greatly appreciated. Code and file attached below
Add Valuelist.gh (3.3 KB)
from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
def AddParam(name, IO):
assert IO in ('Output', 'Input')
params = [param.NickName for param in getattr(ghenv.Component.Params,IO)]
if name not in params:
param = Grasshopper.Kernel.Parameters.Param_GenericObject()
param.NickName = name
param.Name = name
param.Description = name
param.Access = Grasshopper.Kernel.GH_ParamAccess.list
param.Optional = True
index = getattr(ghenv.Component.Params, IO).Count
registers = dict(Input = 'RegisterInputParam'
,Output = 'RegisterOutputParam'
)
getattr(ghenv.Component.Params,registers[IO])(param, index)
ghenv.Component.Params.OnParametersChanged()
def RemoveParam(name, IO):
assert IO in ('Output', 'Input')
params = getattr(ghenv.Component.Params, IO)
param_to_remove = None
for param in params:
if param.NickName == name:
param_to_remove = param
break
if param_to_remove:
params.Remove(param_to_remove)
ghenv.Component.Params.OnParametersChanged()
def add_valuelist(self, nickname, indx, Y):
param = ghenv.Component.Params.Input[indx]
if param.SourceCount == 0:
valuelist = Grasshopper.Kernel.Special.GH_ValueList()
valuelist.CreateAttributes()
valuelist.NickName = nickname
valuelist.Attributes.Pivot = System.Drawing.PointF(
self.Attributes.InputGrip.X - valuelist.Attributes.Bounds.Width - 20,
Y - valuelist.Attributes.Bounds.Height / 2
)
valuelist.Attributes.ExpireLayout();
Grasshopper.Instances.ActiveCanvas.Document.AddObject(valuelist, False)
self.Params.Input[indx].AddSource(valuelist)
print "Added"
Inputs = ["Test1", "Test2", "Test3", "Test4"]
params_add_valuelist = ["Test1" , "Test2"]
value_list_name = ["TEST1","TEST2"]
if Enable == True:
for item in Inputs:
AddParam(item, 'Input')
else:
# Enable inputs
for item in Inputs:
RemoveParam(item, 'Input')
if Enable == True:
# Add value list to inputs
params = getattr(ghenv.Component.Params, "Input")
for i in range(len(params_add_valuelist)):
for j in range(len(params)):
if params_add_valuelist[i] == params[j].NickName:
if params[j].Attributes.InputGrip.Y == 10:
ghenv.Component.ExpireSolution(True)
Y_cord = params[j].Attributes.InputGrip.Y
valuelist_name = value_list_name[i]
input_indx = j
add_valuelist(ghenv.Component, valuelist_name, input_indx, Y_cord)