Creating Global Sticky Variables
How can DataTrees be accessed and created in GhPython.
Creating a definition wide Global sticky variable can be helpful in Grasshopper. Each of you components can access it.
http://www.grasshopper3d.com/forum/topics/functions-in-sticky-to-make-them-available-to-other-python https://discourse.mcneel.com/t/reference-input-values-between-different-ghpython-components/17425 http://www.grasshopper3d.com/forum/topics/exchanging-basic-python-types-between-separate-python-components
Converting DataTrees to Python Lists.
The GhPython conponent can be found on the Math > Script toolbar:
Drag and drop the component onto the canvas:
The default script component has two inputs and two outputs. The user can change the names of input and output, the data type and data structure of the input and also add more inputs and outputs parameters or remove them.
- x: first input of a .NET type (object).
- y: second input of a .NET type (object).
- Out: output string with compiling messages.
- A: Returned output of type object.
To edit the Python script in the component, simply double-click the component:
Simple type int he Python code, clicking on OK will save the code in the component.
By default, there are two input parameters named x and y. It is possible to edit parameters names, delete or add new ones. By zooming into a component new controls will appear on the component:
Use + and - buttons to add and subtract inputs.
You can also right-click on a parameter to change its name. The name is also the variable name in the Python script.
Please note that the names of the parameters must follow Python identifier naming . Just make sure that these names are reflective of what the parameter is, and that they also do not contain white spaces or special characters.
The inputs will show up in the GhPython editor as global variables. For instance, here is a simple definition:
The code to process the
x input is as follows:
if x: print("x is true.") else: print("x is false.")
As the Boolean toggle is changed, the
x value changes. The
Like the inputs, you can define as many outputs or returns as you need when you “zoom in” and push the “+” sign. Unlike input parameters, there are no default datatypes associated with output. The output datatype will be assigned inside your script.
To actually send information to the output from the script, simply assign the value(s) to the output variable name. For instance, in this case the output is a:
To send the information out to a enter this line in the editor:
a = "This string will be sent to the output"
Each time Grasshopper recalculates the definition, the Python component will run and send out the value(s) to the output.
Out parameter is there by default to provide some useful information about your code. I often find myself connecting the out parameter to a panel component to be able to read the information directly without having to hover over the out parameter.
There are two types of messages that you can get in the out parameter.
- The compile-time messages. Those include errors and warning about your code that prevent your component from running or can potentially give incorrect results. These are very helpful information to help point you to the lines in code that the compiler is having trouble with so that you can make appropriate adjustments.
- The run-time messages. You can send bits of text to the out parameter. You can use those to track information generated by your code as it is running.
Advanced Input Properties
For most basic work, the instructions above are all that are needed. But, there are a few advanced options that can be set on the GhPython inputs that can be useful in certain situations.
To access the input options, right-click on any of the inputs.
The top entry is the Parameter or variable name: you can click on it and type a new name.
The next section is the general attributes or functions common to most GH components.
Then is a section to input data directly to the component and to manage that data.
The third section is most powerful to use the Type hint. Input parameters are set by default to the .NET type “object”. Sometimes is best to specify a datatype on input to make the code more readable and efficient. Types can be primitive such as integers or doubles, or geometry specific RhinoCommon types that are used only in Rhino such as “Point3d” or a “Curve”. You need to select the appropriate type for each input.
The rhinoscriptsyntax Type Hint is special: when it is selected, any geometry will be added to the Grasshopper document, and then its ID (Guid) inside it will be passed in the variable. With the rest of the hints, GhPython will call into Grasshopper and perform the same type of operations that Grasshopper does when it requires a special data type. This conversion is costly. If you want to make things as quick as possible, choose ‘No Type Hint’. However, users of your component will benefit greatly from using a meaningfully-set and appropriate type hint.
That lays out the basics of the GhPython component. Next is a look into the component Python editor for Grasshopper.