Importing and using a custom grasshopper component in rhino.python

Hi,

I have built a custom grasshopper component and saved it as a user object (.ghuser). Now I want to import it and use it in Rhino.Python script. Can someone guide me on how to do this?

I have tried importing ghpythonlib.components and calling it like a regular grasshopper component, but this didn’t work.

I’m not sure how it will work, or even what you want to happen when you import a Grasshopper component into Rhino.Python.

If you want to have reusable Python code, usable in both Rhino and Grasshopper if compatible, then make normal Python modules or even packages, and save them somewhere on sys.path (or append their location to it).

Anyway. Plug-in components paths are in Grasshopper.Kernel.GH_ComponentServer.AllAddOns, I’d search its parent. But I don’t think a.ghuser is easily importable, and I’m sure it’s not in ghcomp.

I think it’s possible to place one on the canvas, just using Python code.

Are you in SDK mode? I think you have to leave Grasshopper to initialise an instance of whatever MyComponent class in the namespace it finds itself, and call its RunScript then, and every update afterwards. Naming a different object MyComponent and initialising it is not only confusing, it could create a bug by shadowing the intended MyComponent.

1 Like

A bit late here, but I hope it helps. One workaround is to register yourself in the ghpythonlib.components so you can use it (But you have to drop one component in the canvas to let it run though):

But if it’s going to be this way, it seems to be a better idea to just register yourself in the system module, so the other component can just import YourModule:

Note that I just happen to know this possibility. Not sure if it’s a good idea to do things in this way in terms of code management etc (e.g. naming conflict). Also, although you’ll be able to use either way in Rhino, you have to open the Grasshopper canvas and let the component run once. The process is not very smooth.