Why do I get "traceback" error?

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.

Thank you in advance

Lines 6 and 7 make no sense. Move line 7 to line 5, delete line 6. Make sure there is no indentation once you have move 7 to 5.

Please do yourself a favor and find some tutorials on the basics of Python (including classes).

1 Like

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

It’s hard to say what you want to do though.

1 Like

Hello diff-arch

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)

script 2

Get rid of everything above line 6.

1 Like

Hello, senior

I did the same and received this error, while compile showed no error but GHpython remained red:

I deleted all the lines in the top but nothing changed

Just put this into a GHPython component!

import Rhino.Geometry as rg

center_pt = rg.Point3d(1, 2 ,4)
circle = rg.Circle(center_pt, 5.0)

a = circle  # output

Nothing else!

1 Like

It worked!!

Many thanks

As I am at the starting point of learning RhinoPython programming, It will be a good pattern for me for similar works.

Also, the instruction page you introduced was useful for me, and I will study and memorize those necessary contents.

Thank you again :slight_smile:

You are mixing RhinoScript context with the Grasshopper’s Python context, which is wrong.

1 Like

Hi, Keyu Gan

May you explain more and enlighten me?

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.

1 Like

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?

You really need to understand the fundamental basics of Python. Otherwise it’s likely you become lost.

For example, “rs” and “rg” are just aliases designated by import clauses. The aliases can be anything.

As for a complete reference, here’s the RhinoCommon doc: Namespaces

1 Like

Hi, Mr. Nathan

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.

coding

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.

1 Like

You are right

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.

Thank you so much for your kind guidance, sir :slight_smile: