Grasshopper Python: Can I get the current value of a number slider in GH using its GUID?

I have the GUID of a number slider. How can I get the current value of this in python script so that I don’t have to connect the slider around the grasshopper file all the time?

Thanks in advance

import Grasshopper as gh
slider = ghenv.Component.OnPingDocument().FindObject(x, False)
a = slider.Slider.Value

SliderValue.gh (15.2 KB)

4 Likes

WOW, Amazing!
btw, I got some error messages like grasshopper breaking points. Does it matter?

Thanks again

This one will be listening to GH_SliderBase.ValueChanged Event and updates whenever the slider value changes:

private void RunScript(Guid x, ref object A)
  {
    if(_slider == null)
    {
      _slider = Component.OnPingDocument().FindObject(x, true) as Grasshopper.Kernel.Special.GH_NumberSlider;
      _slider.Slider.ValueChanged -= OnValueChanged;
      _slider.Slider.ValueChanged += OnValueChanged;
      _currentValue = (double) _slider.Slider.Value;
    }
    if(_slider.InstanceGuid != x)
    {
      _slider.Slider.ValueChanged -= OnValueChanged;
      _slider = null;
    }
    A = _currentValue;
  }

  // <Custom additional code> 
  Grasshopper.Kernel.Special.GH_NumberSlider _slider;
  double _currentValue;
  void OnValueChanged(Object sender, GH_SliderEventArgs e)
  {
    _currentValue = (double) e.Value;
    Component.ExpireSolution(true);
  }

SliderValue.gh (15.0 KB)

3 Likes

Thanks dude <3

That’s actually a pretty cool idea. Maybe a component should exist that allows the user to reference another component without having to connect a cable. This is a nice way to avoid spaghetti monsters. @DavidRutten

1 Like

One more question.
Is it possible to update the value automatically using python script?