Python for Rhino & Grasshopper


#1

Hi guys …

I knew it’s maybe indirect to Ladybugging and Honeybeeing! But I know the experts I want to ask reside here also :slight_smile:

I am new to Python … However each time I am getting started to it, it looks like a piece of cake, especially when watching tutorials that talk about ordinary programming work (loops, variables, conditions … functions …)

I am trying to analyse the code in the image below … but it confuses me … :roll_eyes:

As I am understanding it … it is supposed to create the geometry shown from the sum of the collections of planes … created by each line … but … if I hash (comment) any of the program lines shown, even the gp declaration, the whole geometry disappears …!

Could you please give a brief explanation … ? and How can I touch tutorials that good in this regard?

Thanks …


#2

Hi @Ahmed_Jendeya,

This question is very generic and really hard to reply. Can you be more specific about what are you trying to accomplish? Do you want to know how to use Rhinocommon or rhinoscripsyntax to create geometry? Do you want to learn Python in general?


#3

Hi Mostapha …
I do want to learn Python …
But at this stage … I am trying to understand the necessary parts related to Geometry (simple ones).
So any advice regarding that?
and also … regarding learning Python very well in the very near future?


#4

Also … let’s say I’m trying to understand how this specific code works …?


#5

Honeybee code, for the most part, is pretty object oriented and uses some quirky things within the grasshopper namespace like a dictionary called “sticky”, rhinoscriptsyntax and the Rhino API itself (which inturn is a wrapper for a C# -based framework of classes and methods).
To complicate all of that, Honeybee then relates the Rhino-geometry, user-defined inputs from the grasshopper canvas, and certain predefined constants to link to external programs like energyplus, therm, radiance and more (the complexity of what happens with the code, all the while presenting the user with a simple interface is what makes Honeybee awsome!).
If you are just starting out with Python, I would suggest starting off with the ladybug code as it is more straight forward. For the most part, as far as I know, it does not link to external programs to do simulations. All the “magic” that happens is within the confines of the Python code and Rhino/Grasshopper API. Finally, I would suggest getting at least a basic understanding of Python classes and related object oriented methods.

Once you start creating your own components, or hacking existing Ladybug/Honeybee ones, you will find answers to most of your questions on this forum. Here is Mostapha answering a question of mine back in 2015: Radiance view file from Rhino Viewport

Tagging @devang if has a different take on this.


#6

Thank you @sarith for inviting me to the topic.

Dear @Ahmed_Jendeya,
Welcome to the world of Bugs!

Please be happy to know that you have realized a fact that the earlier one realizes the better. We can find a lot of videos and articles online stating “how easy it is to code.” I used to believe it myself only to realize it later that that’s just not how things work.

As you have pointed out yourself,

Learning to code is easy. But coding to create a powerful, reasonably fast, user-friendly, reliable, expandable, and maintainable piece of software is not easy.

I am aware that you are just trying to understand this piece of code that you shared and not trying to create a world-class software. But I felt necessary to share this reality so that you channel your time and energies accordingly.

Now coming back to your question. How to interpret the code you shared?
First of all, you’ll need to know and understand the Object-Oriented Programming. I am going to try to give you a gist of what OOP is, but you should still look at more authentic documentation or guides on OOP for Python.

OOP is a paradigm that is prominent in other strictly typed languages such as C#. If you are learning python and if you feel like you have to hit the breaks when you encounter OOP the first time. Fret not. It happens to everyone for whom Python is their first computer language.

So why do we need OOP?
The idea of OOP is to separate software development into logical parts of Data and Methods.I will try to explain the use of OOP by using the example of geometry objects.


In the image above, Geometry is a namespace that hosts classes such as Curve class, Solid Class, and many more. What is a class? It is a template in the simplest terms. What is the real world use of any template? It is to make repetition easy and to ensure quality at the same time.

Here we have a Curve class.

A curve has properties such as length.
It can be constructed by points.
We can have functions to extend the curve.

Now you can write python functions to for this. But every time writing such functions for every curve is not efficient and does not inspire order, therefore, we have classes. You can and should use this template to define any curve, i.e. a line or an arc. Now these curves can have their own properties and methods in addition to what they get from the template (Curve Class). For instance, an arc will a property named radius which is not part of the parent class. The arc curve receives all the properties and methods from the Curve class. This is called Inheritance.

The class of solid can be similarly used to create a cube or a sphere.

So now if I need to create a curve from two points, I could write something like
Rhino.Geometry.Curve.CreateFromPoints(point01, point02)
The use of periods here is called Dot Notation and it is universal in OOP.

Now please give a thought to what I mentioned earlier and then visit this link that is the documentation of WorldXYPlane(). The first method used in the code you shared. Please try to understand how this works and slowly you will get used to reading and using methods and properties using the dot notation.

designalyze has some tutorials on using python with grasshopper.

Hope this helps!


#7

Thanks for elegant comment @sarith
That explained the reason of the complexity (linking to other external resources) …
I do still in the Lady_bygging stage and I will consider your kind advice :slight_smile:


#8

Hi @devang

You know … YOU ARE GREAT … :slight_smile:

One of the topics that I came to learn, from months, was Python. I stopped in the very first steps due to a sudden project. when I have reached to my first (and last :sweat_smile:) lesson in OOP, I realized that it is a very special way to focus on … but I was not able to go further after only one video.

Grasshopper nowadays pulled me back to Python. Therefore, and I think this time I will not let its hand :slight_smile:

I got your nice and elegant explanation … and its generous and Thanks for the extra links.

I will be back soon to my huge file that this code is the on it’s first block :sweat_smile:, and I will be back here of course if not to asking more questions … to just let you know that you’re a very good motivator :slight_smile: