I am currently working on a hull optimization for my MSc thesis and I am using Grasshopper to make the parametric changes to the hull. I have a Genetic Algorithm that is responsible for assigning the hull parameters and a panel code that I am using to evaluate the fitness of the hull.
I am currently able to solve my grasshopper script with new assigned variables and my panel code program from a Python script in Rhino but the genetic algorithms are all written C++. I would like to make a call for this python script to be solved from the C++ code , any easy approach to this? I saw it can be easily done with a VB script on this thread (Launching Rhino 5 with a batch file and running python scripts).
Ideally I would have written everything on C++ but I couldn’t find a way to assign new variables to a Grasshopper slider and call for the script to be solved as I have in Python. Any guidance on this is also welcome.
I know two options to directly call c++ code from python.
b.) boost library
Furthermore you can google for “interprocess communication” in order to communicate between your c++ programm and a python script component.
Why not translating your C++ code to any of the GH supported languages (which I would do). I mean if you are already familiar with C++ you won’t have a hard time in learning any of those three languages (Py,C#,VB).
Thanks for your answer!
I will check ctypes but the boost library comes with several libraries apart from Boost.Python which makes it excessively heavy. I then tried pybind11 but had some issues there given Rhino uses a Iron Python distribution.
Converting the genetic algorithms would be quite time consuming for something that should be relatively simple to do. There would also be a significant loss in performance (The same GA in Python can be 2-3x slower than in C++). I just need C++ to tell rhino to run a python script and the data communication I plan on doing via text files.
ctypes is quite simple. You load the dll, declare the argument types and call the function. Its however a bit tricky with collections, especially char arrays. But after reading some “Stack Overflow” threads you will get used to it. It might be worth to mentioned that IronPython, which is used in GH, is actually C# under the hood. If you write efficient IronPython/C# code and you compile it instead of interpreting, you will get very similar performance. The C# compiler is actually very good. I personally think there is only one argument which speaks against dot.net -> Reflection. Reflection is good for productivity, but bad for protecting intellectual property, because its way to easy to reverse dot.net code, even with code obfuscation.