I called GHpython in grasshopper and copied this example code that got from a website and copied it into the editor. I did do the same with similar code, and received the same message of error in the last line that both were similar (one was “AddPoint” and another “Addcircle”).
I thought it was because of the difference in Python versions, so I deleted those parentheses but received another error about “AddPoint” or AddCilrcle.
What is the problem and how can I resolve that?
Of course I had to add " Class A" to the code by myself due to receiving a message of error initially.
If you’re writing about print in Python, then yes, before Python 3.0, print used to be a keyword, whereas currently it’s treated like a function. Rhino 7 and Grasshopper support IronPython 2.7.
I’d say stay away from classes, unless you’ve familiarized yourself beforehand with object oriented programming and it’s concepts. Many things can be done with functional programming only.
Declaring class A to only put an import statement inside is pointless. import statements always go on top of the file in Python, and except in rare exceptions, shouldn’t be put in function or class scope!
If you want to script in Grasshopper, it’s also pretty pointless to use rhinoscriptsyntax. It will only lead to weird, unnecessary inconveniences at some point down the line. The API, also called RhinoCommon, is meant to be used in GHPython. There’s a documentation for it here. Under Rhino.Geometry, you’ll find all the geometry objects you can create, but you can also search for distinct keywords.
Your above code could be as simple as this:
import Rhino.Geometry as rg
center_pt = rg.Point3d(1, 2 ,4)
circle = rg.Circle(center_pt, 5.0)
a = circle # output
First, let me show what happened after I used the code you suggested. The result was this: ( of course compiler showed no error and also GHpython turned into the red in the compass, like before)
I started learning python itself in recent weeks. As I am going to learn the language in a short time, in the first stage, I tried to understand it, so I began reading the relative tutorials fast, without memorizing its commands and operations (except a little). I decided to memorize the necessary things, after understanding python, in general.
As I was in a hurry to learn python as soon as possible, I noticed python in Rhino.
As you know, python is used for various purposes, but as for I am a designer and modeler relatively, and have worked with Rhino a few years, I wished to learn python for Grasshopper recently.
The problem was that I didn’t know how to use it in Grasshopper due to difference of its particular statements.
So, I decided to learn Grasshopper’s python experimentally by analyzing the short written codes.
You pointed out a matter that was important to me.
What is the difference between Rhino script and Grasshopper’s python?
RhinoSyntax is more about manipulating objects inside Rhino.
GH’s Python component is, usually, about manipulating data (incl. geometry) inside Grasshopper.
So since you are learning from scratch, it’s highly unrecommend for you to mix them together, which is bad for understanding how things work in Grasshopper, in general.
I am an architect and learned the basics of Python in a few weeks. It’s not difficult but I think it’s important to lay a concrete foundation at first.
For example, RhinoScriptSyntax / rs.AddCircle directly puts geometry into the current Rhino document, complicating the effort to manipulate it in GH afterwards.
My personal opinion is to have a good comprehension of Python first, then learn how to use Python in Rhino/GH.
You are right. Your advice shows the same regular way of learning GHpython; nevertheless, I’m not patient enough to spend time learning python solely aside from working with GHpython simultaneously.
I have to journey in GHpython commands and operations to understand it by thinking, discovering, and manipulating the prepared codes, though it seems an odd way!
But it doesn’t mean I will leave learning python itself. I will study and memorize the parts that mostly are relative to the GH’s codes.
I need the list of statements of GHpython (rs, rg…and so on) at present.
Where can I find the list and the type of function of each one of the statements?
Have you an instructional file relative to that statement list?
I listened to your advice and did the same as you recommended. I wrote the codes by myself.
That is my first masterwork!
I drew a circle with three points and created entirely of the code by myself. I think I passed the hardest part of scripting which was understanding the orders of scripts and learning a little of GHpython terms. I think I know what I must do hereafter.
In this initial stage, the greatest obstacle is that I improve my knowledge and memorize a lot of methods and terms relative to Rhino python to be able to step into the world of programming and programmers.
What I would recommend is start with a base course in Python to get the fundamentals in (variables, loops, conditionals, definitions, classes, etc.). There are many out there. Codecademy for example has a free, interactive one.
After that you need practice. I wouldn’t bother actively memorizing the Rhino API, since that will happen automatically as you work with it.
I have studied what you notified about fundamental things of python but didn’t stay on the contents to memorize the rules entirely. But I need to go back to the instructions, review them, study more deeply and memorize what is necessary to write a program independently.
Previously, I hadn’t a firm motivation and interest to learn python solely, but now Rhino and grasshopper created a warmth in my spirit to take python as a lather so that I can go up to a higher stage of modeling and programming.
As you advised, exercise is the second part of learning Python and other programming languages and is very important.
I have installed Python 3 and Thonny editor for python on my system from six months ago and worked occasionally, but now I spend more time to go advance in python just for Rhino and grasshopper.
Now I need a voluminous mind and enough time for this purpose.