I find myself often manually dragging the width of sliders to their minimum. This is getting a bit old, so of course I’m trying to script it. As far as I can tell the slider property one needs to manipulate is probably Slider.Rail.Width or Slider.Rail.Size.Width. However I’m not having any luck with either, following the forum pattern for e.g. setting the slider value:
Changing the slider object’s Bounds attribute is very close to what happens when you resize the slider with the mouse. The minimum size bound is a private property but still enough is available to make this work:
from Grasshopper.Kernel import GH_FontServer
from Grasshopper.Kernel.Special import GH_NumberSlider
for obj in ghenv.Component.OnPingDocument().Objects:
if obj.NickName in ("Foo","Ham"):
if type(obj) is GH_NumberSlider:
bounds = obj.Attributes.Bounds # Includes the name box
name_size = GH_FontServer.MeasureString(obj.ImpliedNickName, GH_FontServer.StandardAdjusted)
name_width = name_size.Width + 10 # For borders
slider_width = 150 # Set to 0 if you want everything at minimum size, layout code will bound it
bounds.Width = slider_width + name_width
obj.Attributes.Bounds = bounds
obj.ExpireSolution(True)
Excellent, thanks so much Pierre. I would have never figured this out. I reduced/edited the code a bit, but this appears to work as intended. Quick followup question, why can’t we set the obj.Attributes.Bounds.Width property directly (i.e. without assigning it to a new variable)?
Not sure where this goes wrong exactly, but obj.Attributes.Bounds.Width = 0 does not call the setter for the bounds attribute at all. Probably somewhere in the Python / C# / VB interfaces a reference to the Bounds object gets lost.