Start in GhPython -tips

Hi all,

I just (finally) started my to train my Python skills and learn to script more of my grasshopper workflows.

Since I just started I have some open questions and secondly a call for tips.

1. There are many ways to Rome, is Python the one?
I see a lot of VB and C++ scripts when I open the RhinoCommon API info page. Python is mostly not there as example. What is the reason for it? As far as I understand Python is asking the RhinoCommon to run a small C++ script, but I am not sure.

2. I was wondering where to find the source code of the basic component scripts?
I thought to learn the basics is to re script basic grasshopper components, from easy intersects (although I am not sure yet how easy it is :wink: ) to more advanced components like make2d.

3. What are your experiences in connecting software with Rhino?
In the future future, I would like to use other programs to connect. Excel as the easy one for some basic tables, ArchiCAD for more BIM work, Illustrator and other Adobe stuff for some presentation (although Rhino 7 got more powerful in presentation) Anyhow, main goal, let the computer do the work. I have little knowledge in connecting API’s and see for instance on the doodlebug script (Bitbucket) @andheum an advanced code built in another environment. Is this kind of work possible in Python? Or is Python the first step to learn this new world I am getting into?

4. What are good sources to learn?
I am currently 128 kya starter in www.codewars.com and it is awesome! What kind of learning resources / library / primers can you recommend?

5. General Tips
Every tip is welcome. What where your experiences when you started? What libraries / workflows astonished you?

Short thing about me, I am an architect (of buildings) and use Rhino as my digital pen and paper. I sometimes have to switch to other software, but they never give me even a glimpse of the satisfaction of using Rhino.

All the best,

(a little bit in advance)
Merry Christmas!

Georges

Hello,

Great idea learning to code - it sounds like you are on the right track. Different people on this forum will give you good reasons to choose Python or C# - there is no wrong answer here. Python is a great choice and will serve you well across a range of tools and generally on your coding journey.

VBScript is an older option and generally less popular and less loved as time goes by. C++ is lower level and harder to learn - i would stick with Python (or C#) for the time being unless you are really motivated by another choice.

Happy coding!

Graham

4 Likes

It’s probably C# not C++. The Python version that Rhino implements is called IronPython i.e. a Python flavour for .NET that is written entirely in C#. You can therefore think of IronPython as a high level and dynamic wrapper around C# with Python syntax. This comes at the cost of performance, where especially large computations will be slower than directly written in C#. For more direct considerations about language differences, have a look at this article from old forum.

Generally speaking I use GHPython for most of my daily coding (generating/analysing geometry and solving problems fast, iteratively, and on the fly) and C# whenever I need more performant code. I personally find that GHPython provides less friction when scripting directly on the canvas than the C# component/language. Conversely, writing compiled C# code in Visual Studio when developing “real” Grasshopper plugins is very low friction. So if your goal is to write a plugin I’d recommend starting with C#. Although you can “compile” your GHPython scripts it doesn’t offer performance benefits (that I know of at least) and is really rather cumbersome.

I’m not sure what you mean by “basic component scripts”. But if you wanted to say inspect the code of the rhinoscriptsyntax Python module to see how it implements the RhinoCommon API, you can use the methods described in this thread. The source code of Grasshopper itself isn’t open, so you’d have to decompile it to inspect it. Which would be a breach of the license.

Absolutely great! The only caveat I can think of is that IronPython doesn’t support all Python modules (e.g. numpy). But being able to use all of .NET tends to make up for that (e.g. MathDotNet).

My primary GHPython tip is to start using RhinoCommon directly as soon as possible:

Understanding the different geometry types, how they relate to each, their properties, and methods is the most important aspect of learning how to code Rhino geometry (in my experience). And putting unnecessary and confusing layers of indirection around that is a detriment to learning (again, in my experience). That’s not to say that rhinoscriptsyntax or node-in-code doesn’t have it’s place.

Finally, learning how to Google coding questions is probably the most important skill you’ll need :wink:

Edit: To answer this question directly:

Yes, you should be able to implement most .NET APIs directly in GHPython. See for instance this thread, which documents how I implemented the Google Sheets .NET API. That said, you will likely have an easier time implementing .NET APIs in C#. As the documentation and examples likely will be written in this. You can also implement C++ directly in GHPython, like I did here implementing the ShapeOp library. And of course you can implement Python libraries, at least the ones written in pure Python. Python also has many standard libraries for interfacing with databases, internet protocols etc. that I find super useful in my daily work.

8 Likes

Many coding beginner make the mistake to stick to a single technology or language. Using Rhinocommon is easy if you understand how to work with libraries in general. It’s not wrong to learn more than one language at once, understanding advantages and disadvantages of them. Python and C# are both excellent choices in modern software development, so definitely learn both. I made the mistake not to learn the language itself first and comparing it with others. That’s why it took me a very long time to really understand the purpose of many details. I don’t know, it maybe just takes a long time, but the more you invest in understanding the core principles, the faster you will really enjoy coding. This usually starts at the point where you just type down your ideas just as a writer would do (or you exactly know where to search for).

Until then find ways to keep you motivated by solving little puzzles and tiny useful scripts. Keep it small and simple!

Never be overwhelmed, this feeling will stay.
Oh and really learn how to debug code , how to read exceptions and how to prevent errors. And rather trust the older guys on youtube, not the hordes of coding teens. Just because something works, doesn’t mean it’s good. It’s the same with CAD, you can immediately see who is an experienced professional and who is not, just by looking at the geometry itself.

7 Likes

Lots of resources for leaning python are listed here :

1 Like

Thanks for a really great reply.

The first time I red it I was overwhelmed by all the different languages and API’s.
Now I get these huge rewards when I run a code and it works! Feels like a new world of possibilities opened up. :slight_smile:
I want to start C# when I am in more control of Python.

I foresee some future issues with IronPython (2.7) vs 3.9 and I don’t know what the .NET framework is. But before we are there I will probably learned enough to answer this question :wink:

3 Likes

This is what I did, I mean I was using Python, and then jumped to C#, and I think it’s a really good path. Just as many of us started with Grasshopper and then switch to typical programming language. All of this learning process is not hard, but requires time, so if you have some free time to learn, then you will become fluent in both languages :slight_smile:

I have another question.

I am coding a bit in atom for some hobby projects. (website etc.)
The coding of GH Python I do in the Grasshopper interface.

Is this the way you work as well or is there a best practice to code in a more friendly environment and link it directly to Grasshopper script?

This could be a noob question, probably it is. Still curious. :slight_smile:

I’ve tried a few different options over the years (if you run a search there are many threads on this topic), but have always ended up back developing directly in the GHPython editor. It might not be perfect, but it’s certainly the simplest and least brittle solution (IMO in my ten year experience with it). For CPython code outside Rhino I prefer to develop in Sublime Text, it’s quite delicious.

2 Likes

A grasshopper script component is basically in a scope of a single class. So if you listen to many principles of coding, this will always be a rather short piece of code. The good news, most mistakes are
simple to identify.

This also means the majority of all tools any ‘better’ code editor or IDE offers would be nonesense anyway. Autocompletion and debugging is the biggest problem in the inbuild editor, but in general Python’s nature doesn’t give you the best experience in these domains anyway. Lots of runtime errors and missing object relations are one of the disadvantages. In my oppinion, the best code editor in CPython is PyCharm in this regard. It offers a great debugging experience.

Now, since you are lacking these features, this essentially means, you are forced to use a lot of print statements and to memorize the library from the documentation. This is not bad, its just a more oldschool way of coding. Good for learning actually.

So yes, I experienced the same in Python and C# within Rhino. Just use the inbuild editor and learn to live with the limitations. If you plan to develop a plugin, go for Visual Studio and take one of the best IDE’s around.

3 Likes