How to go beyond Grasshopper

In my team we have been using Rhino and Grasshopper to handle 3D objects in a robotic context (so our scripts create robotic trajectories from a 3D object).

Grasshopper allowed us to quickly develop something that works and we are going to keep using it. However it has its speed limitations and we want to integrate what we did in a proprietary application.

So what are the best ways to translate those scripts into C++ or C# code? Should I opt for C++ or C#? What are the librairies that would help me the most to handle 3D in C++/C#.

I haven’t got the chance to test Rhino Inside yet since I am still on Rhino6, but would that be a good way to integrate my scripts in terms of speed?

I am trying to be ready as much as I can before going into the unknown and possibly on the wrong path… So I would love if someone could help me with this!

→ opennurbs

If you know one of them, use that one.

I tend to implement parts of GH definitions in C# ScriptComponents. The benefit is that this can be done directly on the GH Canvas so you can test the code as you go. Then it’s easy to move the code to VS and compile it.

Not working with Robots but at times I make IK-solvers to enable demo-animations of concepts. Pictured below a mobile crane I’m working on as a side project (will be used to demonstrate a new invention to be attached).

The crane has many joints (double linkage at red rings, moved with IK-calculations) and translations controlled in a “IK-fashion” by moving a point around (see Gumball) with the mouse. The tip of the arm has three extension cylinders, aso.

Over time I tend to replace the GH components with C# script components until performance is good enough. The component solving the double links (emitting Transforms) is the green-ish selected component.

// Rolf

2 Likes

Thank you! opennurbs seems pretty interesting, however it has been made for rhino files, and I am more interested in Grasshopper files

When creating a Grasshopper script you are indirectly using Rhinocommon. A large proprietary .dll that ships with Rhino. You cannot simply reference or replace that. As mentioned, there is an open-source subset called ‘OpenNurbs’, but seriously, piping together some GH components is from a developer’s perspective only a tiny piece of the cake. So unless you implement most of it by yourself, its even questionable to sell it as your intellectual property. It’s like going to a restaurant, buying a meal , putting some topping on it and then trying to resell it as yours.
What you want is a OpenGL or DirectX application. You can do that in C#, but anything in that domain is better solved by using C++. You could also use an engine like Unity and write in C#, but then you have pay license fees. Personally I believe C# is easier to deal with, but it’s not the best choice for this purpose. So go for C++.

2 Likes

Yes, but it’s much more than that. It’s basically a fully featured C++ geometry framework. It can do all the basic stuff that Rhino or Grasshopper can, and if something is missing you can always look it up and try to implement it yourself.

In C++ there’s also the boost framework, which has many neat geometry libraries that you can combine with OpenNurbs, if something is missing or you want to turbocharge stuff.

Pretty much everything that is written in C# can be easily decompiled, which means that you can look at the original code for inspiration. Now this might be illegal, and I’m not saying that you should copy and/or steal somebody else’s code, but there’s little harm in taking a peak for inspiration. You have to learn somehow.

@TomTom is right. What OpenNurbs doesn’t have is a multi media feature set that can build your app with a UI, or do mouse and keyboard interactions. You can use something like SDL, SFML, openFrameworks, or Cinder for that.

Also if speed is a requirement, absolutely go for C++.

2 Likes

Yes I totally agree with you, and that is how we have been using Gh for, just proof that what we do can work.
That was my guess that C++ was better suited for all this, but I asked for C# because it’s easier indeed.

@diff-arch thank you for the libraries, I’ll look it up !

Thank you both for your insight!

Thank you! This is pretty interesting and might be useful to us, I’ll talk to my team about writing C# ScriptComponents to increase speed

This is not guaranteed to be faster in my opinion. The Grasshopper components are basically DLLs (dynamic linked libraries) that are renamed to GHA. These are compiled, whereas the C# scripting component is only interpreted, and this should always make it slower. You can build your own GHAs though.

1 Like

The C# scripting components are actually compiled at runtime, but I agree that this is not the best solution.

I wouldn’t recommend immediately jumping to C++ and opennurbs as the amount of extra work involved can potentially completely kill the project. Instead, I would recommend learning to make a custom grasshopper component in C# that can perform all of the logic that you are building up in your grasshopper definitions. Once you have all of the code in a custom grasshopper component, you may have enough to fit for your needs. If you want to take the project further, you will already have the code written by this point and can explore moving the algorithms to work in other environments outside of grasshopper.

4 Likes

Sounds like a good way to move smoothly towards an external environment. I still have to code the app so maybe I’ll do both at the same time!

Not really. A ScriptComponent is compiled the first time it runs (so watch out for the compile time at first run, typically around 250-350 ms IIRC)) After that first run they run like any other compiled components. Exception: The input & output params are slower than components compiled separately in VS.

Depending on problem to be solved, ScriptComponents is often a super boost when replacing slower GH components which tends to get complex and messy and the logic barely readable. Not to mention when loops and recursion is required (IK tends to require iterative fitting to constraints within tolerances, at least with my redneck approaches).

ScriptComponents is a very good first step to speed up and to clean up messy GH definitions, sometimes also enabling running code in parallel. Using the plugin Parasite you can transform the code to Visual Studio as you type your script and test it.

If already having GH definitions, hopefully well structured in logical groups (or Clusters), a ScriptComponent (+Parasite or Copy & Paste into VS) is a very natural next step.

If need be, make dlls with C or C++ for algorithms which needs that extra speed or for time critical cases where the GC may cause trouble.

// Rolf

3 Likes