Run a Grasshopper component from within your C# code

How to call and calculate a Grasshopper component from within some C# code, and read and return the component outputs.

A Grasshopper component is essentially a visual interpretation of a method in programming. It has inputs, it does calculations, and it produces outputs. Grasshopper components are also saved within DLL libraries, and can be referenced. So, surely, it’s possible to access a component programatically from within something like a Visual Studio project? This is a quick first attempt to do so.

The code below attempts to read one of my own components (“CalcSunDirection”). The component runs with all inputs at default values, and then returns the first data item within output 2 (the third one in human-speak). This is copy-pasted from the Solve_Instance method when creating Grasshopper components in Visual Studio.

        protected override void SolveInstance(IGH_DataAccess DA)
            //Create the component and run calculations
            var cs = new MyComponents.Components.CalcSunDirection();

            //add to document
            GH_Document GrasshopperDocument = this.OnPingDocument();
            GrasshopperDocument.AddObject(cs, false);

            //read output[2]
            var rtnval = cs.Params.Output[2].VolatileData.get_Branch(0)[0];

            //remove that component
            GrasshopperDocument.RemoveObject(cs.Attributes, false);

            //send output
            DA.SetData(0, rtnval);

Grasshopper component for calculating sun direction written in C#, being called from a second test component

As we can see, the test component produces the same output as the CalcSunDirection component it is referencing. The above image is for comparison – in reality, the user wouldn’t see the CalcSunDirection, only the Test component.

I believe it is hypothetically possible to do this trick with any component, assuming that the DLL/GHA file containing the component you want to use is referenced in your Visual Studio project.

I tried to do this with some native Grasshopper components (such as the circle from CNR component). I looked up the namespace of the component using this tool, but I had trouble locating the DLL containing the component, so I was unable to add it to my VS project. (For example, the Circle CNR component has namespace CurveComponents.Component_CircleCNR, which is not within Grasshopper.dll. Anyone have any idea where I can find it?) Edit – found them! See below…

As a first attempt, the code above also seems inefficient in that you have to physically add the component before the outputs become readable. I get around this from a user experience point of view by removing that component after, but computationally, it still feels heavy and unnecessary.

Upon reflection, what I’ve essentially created is a programmatic version of the clustering feature of Grasshopper. Is this the best way to go about it? If you have any interesting suggestions, please let me know 🙂

Update – geometry GHA files

Many thanks to Andrew Heumann for answering my question above – many of the components are not contained within Grasshopper.dll but are saved elsewhere.

It appears that since version 0.8.0012 or so, they have been kept buried within the Program Files. On various computers, I’ve found them at:

C:\Program Files\Common Files\McNeel\Rhinoceros\5.0\Plug-ins\Grasshopper\\Components


If you are struggling to find them, and you don’t mind giving third party software direct access to your computer’s Master File Table (!), then UltraSearch is the fastest way to find the GHA files.

Within this folder is a collection of GHA files (which is essentially a DLL with the extension changed).

The full list is:

  • Curve.gha
  • Field.gha
  • Galapagos.dll
  • GalapagosLibrary.gha
  • IOLibrary.gha
  • LegacyScript.gha
  • Mathematics.gha
  • Script.gha
  • Surface.gha
  • Transform.gha
  • Triangulation.gha
  • Vector.gha

Grasshopper GHA extension files

So for the Circle CNR component, I would find it by referencing the Curve.gha file.

Update 2

See this post for updated code. This updated code allows you to create a dummy document, so you don’t need to paste components on the live document.


Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.