Grasshopper

algorithmic modeling for Rhino

Is it possible to change the value of a number slider component from a python component? If anyone knows how to do this or where to look to find out, it would be really appreciated. Thanks -

Views: 7775

Replies to This Discussion

First you need to get a reference to the slider instance. You can iterate over all objects in the relevant GH_Document, and see which ones are of type Grasshopper.Kernel.Special.GH_NumberSlider 

GH_NumberSlider has a public readonly property called Slider which returns a Grasshopper.GUI.Base.GH_SliderBase object. However you can also use the SetSliderValue() overloads on the GH_NumberSlider instance.

I don't know how to tell you this in python though.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

David, thanks so much for the tip. It's not the answer I was hoping for, but at least it sounds possible. This probably also means I cannot load the min/max values for a slider from a config file/script. Can these parameters be accessed via the grasshopper SDK through a call from the C# scripting component?  I searched the SDK for "NumberSlider" and did not find any documentation - is this the wrong place to look?

It is possible to set all properties of the slider via the SDK. There are a lot of classes in Grasshopper that have not been included in the SDK Documentation. The entire Grasshopper.Kernel.Special namespace is missing.

I attached a C# script that modifies a slider min, max & decimals properties. Note that I have to disable the slider while I change it, so the output of the slider won't actually update until you drag it.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Attachments:

Wow, the example is much appreciated, works like a charm. By the way, I noticed when I opened the example that you have a newer version of grasshopper than posted via the downloads page ... any chance you need beta testers for the push button feature?

Not yet. Pretty close though, just finished all remaining cluster features yesterday but a lot of internal testing still needed.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

You can force an update+recomputation by calling ExpireSolution(true) on an object.

--

David Rutten

david@mcneel.com

Seattle, WA

Hi David,

Do you happen to know the python equivalent for setting the slider maximum? I have the following code that successfully creates a slider, however the maximum value is set at 1.000, regardless of what number is used as input for the SetSliderValue() overload.

import Grasshopper as gh

Slider = gh.Kernel.Special.GH_NumberSlider()

Slider.SetSliderValue(1.600)

Thanks!

See the GH_Slider methods in the Grasshopper SDK.chm help file:

The dot structure command should be gh.GUI.GH_Slider.Maximum, but exactly how to use it  isn't obvious to me from the command help page.

I assume you set this first to have it control subsequent slider creation.

Thanks Nik for the images. I'm familiar with the SDK, but translating the commands into python equivalents doesn't always work.

In my case, the maximum command gives me the error that "GH_Slider has no attribute 'Maximum' for the following statement (GH_Slider.Maximum, GH_Slider.SetMaximum).

When I try (GH_Slider.Slider.Maximum = 10), I receive the error 'Decimal is not callable.' I believe that there is a way to set it, but have not found the correct command and input format just yet in python.

Thanks again for making the effort to include the SDK information, I appreciate it!

Have a look at the attached file. This is the most straightforward method I've found for manipulating GH objects. Hope that helps :)

Attachments:

Hi Anders,

Thanks very much for sharing your file. It helped greatly. I realized that my error was trying to set the slider value and maximum before adding it to the GH document. It was a simple reordering of commands. (Slider.Maximum =) seems to work perfectly fine.

Thanks again!

Anders' Python script called R:

""" Reset named sliders """

import Grasshopper as gh

# Get to the GH objects
ghObjects = ghenv.Component.OnPingDocument().Objects

# Iterate the GH objects
for obj in ghObjects:
    
    # Set the named slider values
    if obj.NickName in Sliders:
        if type(obj) is gh.Kernel.Special.GH_NumberSlider:
            
            # Set min/max
            obj.Slider.Minimum = 0
            obj.Slider.Maximum = 10
            
            # Set value
            obj.Slider.Value = Values[Sliders.index(obj.NickName)]
            
            # Update slider
            obj.ExpireSolution(True)

Now I'm super confused, since this method I can't even find a hint of in the Grasshopper SDK help file. This isn't some Python-related formatting either, for the dot structure commands would be more or less the same in VB or C#. It's only scriptcontext related to Rhino vs. Grasshopper that slightly alters dot structured commands.

I can comment out his paranoid "if type(obj)..." line and it still works, and if you give it the wrong name it's going to fail the same way anyway.

So out of the blue appears the obj.Slider.Maximum object, but if I search Grasshopper SDK help file, nowhere does a simple Slider command appear, nor, frustratingly, am I getting the usual Python pop-up command tips after I type a period after obj, or after Slider, so that's no help.

All I see is GH_Slider, GH_SliderBase or GH_Slider_Obsolete. Let's try GH_Slider instead. For that matter nor do I see Tracy's SetMaximum, which is why I figured she just made it up, wrongly. Ah, I do get pop-up command tips if I start typing "gh." and then I access all the way through to gh.GUI.GH_Slider.Maximum, just like the SDK file. I assume Anders is using an old legacy method then? I just have no idea how to use this official command, since there is no sample code, just "{ get; set; }" which is not clear what it means. I think GH_Slider may be a Windows slider instead of a Grasshopper canvas one ("Provides a standard Grasshopper slider as a winforms control.")?!

So, Anders, where do I look things like this up? Is it even in the SDK help file for Grasshopper?

The bare minimum working code is:

import Grasshopper as gh

ghObjects = ghenv.Component.OnPingDocument().Objects

for obj in ghObjects:
    if obj.NickName in Sliders:
     obj.Slider.Maximum = 10

Can I do the same thing with an official SDK command that uses the pop-up dot structure command hints, so I can sleep at night since I understand what's going on?

ghenv itself isn't even in the SDK file (!), though it mercifully does appear in the pop-up command hints, but that's still another deal killer for understanding how to use Grasshopper via scripting, since well, it's not in the manual.

I found this info: "...ghdoc/ghenv are just two variables. The first one contains an instance of GrasshopperDocument, the second one of PythonEnvironment. They list a series of RhinoCommon/Grasshopper/Python references, that might occasionally be useful. They are not needed to create geometry, but just to address special topics that might occasionally be useful. In fact, GrasshopperDocument does the same as RhinoDoc with ObjectTable, while ghenv really does specific things like connecting with Intellisense, providing the Python interpreter instance, or the local scope (the object that represents the executing place of the script)." (http://www.grasshopper3d.com/xn/detail/2985220:Comment:973147 )

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service