Tutorial: creating a Grasshopper component with the Python GHPY compiler

Thanks a lot Giulio… it was all because of a hyphen in my module name!?

1 Like

This was very helpful! When using the built-in complier, is there a way to add more detail to the help section for the component? (maybe special characters to use in the docstrings?) I’m sure I’m missing something obvious, but when I add a line break/return to the the first line, it fails to compile. If it’s all on one line, it works as expected.

Any suggestions would be greatly appreciated.
Thanks!

1 Like

I’ll check this @chanley

Hi @chanley

I see the problem. It should probably use a triple-quote string to help with that.

There is a “remarks” section, but it is currently not compiled. I’ll make sure it gets added in the next version of the WIP. Of course, you can bypass the problem by compiling code yourself at present.

Provides a scripting component.
    Inputs:
        x: The x script variable
        y: The y script variable
    Output:
        a: The a variable
    Remarks:
        This is a remark.
        The remark has more lines.

RH-38167 is fixed in the latest WIP

Wow, this looks interesting. But before wasting time, can the compiled components also be used in Rhino 5 (Gh0.9)?

// Rolf

“Unfortunately” not.

Hm.

So, is the “Rhino Script Compiler for Rhino5” project abandoned?

// Rolf

That’s a different compiler, @RIL: Rhinoscriptsyntax and RhinoScript

A post was split to a new topic: Question on different compilers

A post was split to a new topic: GhPython compiled components Hints issue

6 posts were split to a new topic: Problems loading GHPY assembly

Hi, Giulio. Happy New Year. Thanks for your toturial, that’s very helpful.
But there is a error that troubles me a lot and I can’t figure out why. Here is the code:

from ghpythonlib.componentbase import executingcomponent as component
import Grasshopper, GhPython
import System
import Rhino
import rhinoscriptsyntax as rs
import scriptcontext as sc

class MyComponent(component):
    def RunScript(self, crv_id):
        sc.doc = Rhino.RhinoDoc.ActiveDoc
        crv_o = sc.doc.Objects.Find(crv_id)
        a = crv_o.Attributes.Name
        sc.doc = ghdoc
        return a

Compiling this code follow your tutorial, I got a new GH component without any error. But when I put this component into canvas and loaded input it get red and says:

Solution exception: global name “ghdoc” is not defined.

Do you know why? Can’t run correctly after compiling into GH component. But this code runs OK before the final step.

I have change “ghdoc” into GhPython.DocReplacement.GrasshopperDocument() and solved this problem. I hope I’m not disturbing you.

1 Like

Thanks, this is reported at RH-43345.

Hi,Giulio Piacentino.
Is user have to install IronPython to load ghpy files as GH plug-in into Rhino?
And can Rhino5 support ghpy files?

Hi Giulio!
Following your tutorial 1. Compiling a single component in an assembly with the wizard
I have a Runtime error


and the compile did not work
GHPython%20compilatin%20error
could you tell me how to fix it? why the name ‘component’ is not defined, is not defined already in line 18 ?
classs MyComponent(component):

Thanks in advance!

The import part should not be within the triple-quotes. Pay attention to that! @andresobregonlopez

Hi @piac,

I use this code below often to dynamically add/remove inputs. Is there a way to translate this functionality within the compiled component?

   # automnatically set the right input names and types (when using + icon) 
    numInputs = ghenv.Component.Params.Input.Count
    accessList = ghenv.Component.Params.Input[0].Access.list
    accessItem = ghenv.Component.Params.Input[0].Access.item
    typeFloat = GhPython.Component.NewFloatHint()
    typeBool = gh.Parameters.Hints.GH_BooleanHint_CS()
    
    for input in range(numInputs):
        access = accessList
        typeHint = typeString   
        if input == 0: inputName = 'width'; access = accessItem; typeHint = typeFloat
        elif input == 1: inputName = 'thickness'; access = accessItem; typeHint = typeFloat
        elif input == 2: inputName = 'spacing'; access = accessItem; typeHint = typeFloat
        elif input == 3: inputName = 'rotation'; access = accessItem; typeHint = typeFloat
        elif input == 4: inputName = 'offsetFromWall'; access = accessItem; typeHint = typeFloat
        elif input == 5: inputName = 'verticalOrientation' ; access = accessItem; typeHint = typeBool   
        else: continue
        
        ghenv.Component.Params.Input[input].NickName = inputName
        ghenv.Component.Params.Input[input].Name = inputName
        ghenv.Component.Params.Input[input].Access = access
        ghenv.Component.Params.Input[input].TypeHint = typeHint
    ghenv.Component.Attributes.Owner.OnPingDocument()