Over the years, there have been lots of threads here and on the old Grasshopper forum that deal with setting slider values in Grasshopper. They all have some advantages and disadvantages, but they have something in common: they too often end up breaking the Grasshopper SDK rule “never edit values while the definition is running”.
This thread shows a way of fixing that. This is a Rhino-6-only possibility, because this feature is new in Rhino 6. By overriding the Component Context menu, we can set the values outside of the solution, in a very Grasshopper-classic way.
Like this definition by Anders Holden Deleuran on the old Grasshopper forum, this uses the logic of searching sliders by name. You can however adapt it to using slider InstanceGuids, so that they can be renamed.
Here is the code:
""" Reset named sliders. Right-click on the component and choose "Update Sliders!" to update their values. Inputs: Sliders: (strings) A list with names of sliders Values: (numbers) A list with values of sliders Remarks: This component does not retuns any values. """ from ghpythonlib.componentbase import executingcomponent as component import Grasshopper, GhPython import System import Rhino import rhinoscriptsyntax as rs class UpdateSliders(component): def RunScript(self, Sliders, Values): if not Sliders or not Values: Sliders = None Values = None return self.Sliders = Sliders self.Values = Values if len(Sliders) != len(Values): raise Exception("Different lenghts of sliders and values") self.Component = ghenv.Component self.GhDef = None if self.Component: self.GhDef = self.Component.OnPingDocument() def AppendAdditionalComponentMenuItems(self, menu): item = Grasshopper.Kernel.GH_Component.Menu_AppendGenericMenuItem( menu, "Update Sliders!", self.OnClicked, self.Icon_24x24, None, True, False); item.ToolTipText = "Update the sliders now"; def OnClicked(self, obj, args): try: self.UpdateValues(obj, args) except Exception, ex: System.Windows.Forms.MessageBox.Show(str(ex)) def UpdateValues(self, obj, args): if not self.GhDef: return if not self.Sliders or not self.Values: return ghObjects = self.GhDef.Objects # Get to the GH objects # Iterate the GH objects for obj in ghObjects: # Set the named slider values if type(obj) is Grasshopper.Kernel.Special.GH_NumberSlider and \ obj.NickName in self.Sliders: # Set min/max #obj.Slider.Minimum = 0 #obj.Slider.Maximum = 10 # Set value obj.Slider.Value = self.Values[self.Sliders.index(obj.NickName)] # Update slider obj.ExpireSolution(True)
Have a look and see if it helps!
for Robert McNeel & Associates