Grasshopper

algorithmic modeling for Rhino

Hello,

Is it possible to add a Grasshopper component (i.e. a slider) to the grasshopper canvas with python script? I am aware of an example of doing so with C#, but am finding it difficult to do so in python. 

For example, a sample script would involve prompting the user to select points on the Rhino canvas, and I would like the python script to create a slider for each point that the user selected on the grasshopper canvas.

Any advice would be greatly appreciated!

Tracy H.

Views: 3059

Replies to This Discussion

Hi Tracy,

Try this. It creates a basic slider with a lower limit of 0.000 and an upper limit of 1.000. Its default value is 0.250. The slider is placed at the top left of the canvas work space. Edit the script per your needs.

Best,

/SPM

Attachments:

Wow--this is very helpful. Many thanks!

My next attempt is to add the slider to the same Python Script component (the "y" input slot for example). I'm working with the lead of the AddSource method (attached), but so far have been unsuccessful. Any advice for this task would be most greatly appreciated!

Tracy H.

If you know the ComponentID of the object you wish to place, you can use one of the GH_Canvas.InstantiateNewObject overloads. It allows you to specify where you want the component to be.

Thank you, David. I am having trouble with the four input parameters for the InstantiateNewObject method (id, init_code, at, update).

I've sifted through the Grasshopper SDK help database, but can't seem to find out to obtain the appropriate ID of the slider component with the creation method (AddObject) above to feed to this method. I can't seem to find what "init_code" and "update" are supposed to provide.

It seems that "at" requires a PointF (not sure what this means for python, but i'm assuming the coordinates of a specific point), but rather than indexing a location on the canvas, is there a way to attach it to the Python Script component itself? I've attempted so with the AddSource method (attached in the reply above), but so far have been unsuccessful.

Many thanks for your time and advice! 

Hi, David. I forgot about using overloads. Don't think I'm using it correctly though. I quickly tried to adjust the previous code but it keeps crashing Rhino/Grasshopper. What am I missing here?

import Grasshopper as gh
import random as r
import System.Drawing as sd

ghdoc = ghenv.Component.OnPingDocument()
canvas = gh.GUI.Canvas.GH_Canvas()


def createSlider():
mySlider = gh.Kernel.Special.GH_NumberSlider()
id = mySlider.ComponentGuid
mySlider.SetSliderValue(0.25)

x = r.randint(0,100)
y = r.randint(0,100)

ptF = sd.PointF(x,y)

bool = gh.GUI.Canvas.GH_Canvas.InstantiateNewObject(canvas, id, ptF, False)
return mySlider

mySlider = createSlider()
ghdoc.AddObject(mySlider, bool, 1)

Thanks,

/SPM

A few issues I see - one is that you're instantiating a new instance of GH_Canvas instead of using the currently active one, which can be accessed via gh.Instances.ActiveCanvas. I also couldn't get "InstantiateNewObject" to work, but I don't mind the AddObject method. 

The code below also provides an example of how to hook the slider up to the python component. This gets a little bit tricky - because you're creating objects that are supposed to be a part of a solution, while a solution is actually happening. Ideally, you should 1. make sure that you check that a slider hasn't already been created, so you don't add a slider every single time the component solves, and 2. schedule a solution AFTER the current solution so that slider value is actually correctly passed into the python component. I haven't provided examples of either of those operations for now. 


import Grasshopper as gh
import System.Drawing as sd

comp = ghenv.Component
ghdoc = comp.OnPingDocument()
canvas = gh.Instances.ActiveCanvas

#create the slider
Slider = gh.Kernel.Special.GH_NumberSlider()

#add the slider
ghdoc.AddObject(Slider,False,ghdoc.ObjectCount+1)

#the array accessor lets you pick which input you want
targetInput = comp.Params.Input[1]

thisPivot = targetInput.Attributes.Pivot
#set the slider location
Slider.Attributes.Pivot =sd.PointF(thisPivot.X-200,thisPivot.Y-10)

#connect the slider
targetInput.AddSource(Slider)

Here's a more complete example that does the two things I described in the above post. If you DON'T expire the solution, you'll notice that, even if you change the slider, the python component won't see "y" as having any input.


import Grasshopper as gh
import System.Drawing as sd

comp = ghenv.Component
ghdoc = comp.OnPingDocument()
canvas = gh.Instances.ActiveCanvas

#the array accessor lets you pick which input you want
targetInput = comp.Params.Input[1]

#Define a function to expire this component. We'll need this later!
def expireThis(doc):
comp.ExpireSolution(False)

#Make sure the input doesn't already have a slider
if targetInput.Sources.Count==0:
#create the slider
Slider = gh.Kernel.Special.GH_NumberSlider()

#add the slider
ghdoc.AddObject(Slider,False,ghdoc.ObjectCount+1)

thisPivot = targetInput.Attributes.Pivot
#set the slider location
Slider.Attributes.Pivot =sd.PointF(thisPivot.X-200,thisPivot.Y-10)

#connect the slider
targetInput.AddSource(Slider)
#Recompute this component so that it has access to the value of the slider
ghdoc.ScheduleSolution(5,expireThis)

#Do something with the value:
a = y

Thanks, Andrew. I caught the ActiveCanvas after posting (one of those silly "face palm" moments).

The Pivot trick is slick. It addresses why I was looking into the "InstantiateNewObject" method well.

I created a boolean input so I can expire the solution. I noticed it was really slowing down GH and every time I changed the slider's value it jumped back to its creation point.

Silly addition, but if you replace "targetInput" with the following:

comp.Params.Input[len(comp.Params.Input)-1]

you can force it to create a slider for the last input only.

Grasshopper.Instances.ActiveCanvas.InstantiateNewObject(Grasshopper.Kernel.Special.GH_NumberSlider.SliderGuid, new Point(100,100), true);

Many many thanks, Andrew, Stephen and David! 

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