Grasshopper

generative modeling for Rhino

# How to input and use point in GhPython directly?

Hey,

I am a beginner of GhPython.

I want to use the points from grasshopper directly, but I cannot.

I guess there must be some ways to declare x should be a point.

So, what is the right method that I can use points generating from grasshopper directly?

Thank you so much.

Tags: Grasshopper, Python

Views: 642

### Replies to This Discussion

Hi Suzy,

To create a point from scratch, there are two ways to do it:

and:

in general the two ways to create geometry from scratch are:

1. to use the rhinoscriptsyntax commands like "AddPoint()" and "AddLine()". Commands like this will return Guids that refer to objects, and are great for working in rhinoscriptsyntax. this is illustrated in the first example.
2. to make objects directly using constructor methods in Rhino.Geometry. These commands will return Geometry objects such as Point3d, Curve, and Surface objects (instead of Guid objects). This approach is good for working with RhinoCommon, and is illustrated in the second example.

I hope that helps.

Thanks a lot,Benjamin.

I guess my picture explaining my question in a wrong direction.

Let me try to explain my question again.

Actually, my final target is delete one of two lines generated in grasshopper, if the distance between them is lower than a special number(like 2mm).

So, according to this logic, the first step should be input lines from grasshopper, and them calculate the distance between them. After delete one.

However, in the first step , although I already set the input x and y as point3d, I still cannot add a circle or a line with x and y.So, I guess I cannot use the points generated from other operation from grasshopper,which means I have to generate the initial geometry from GhPython.

Actually, I just read your impressive example drainScript , and I found you used the startpoint generated from other operation in grasshopper(not GhPython). However, I still cannot figure out how you made it work.That is the key point for my whole python script. Would you mind modify my script in the same way?Thank you so much.

Regards,

Suzy

You can change the inputs to anything you want, and you can add or delete inputs. Right click on the component and go to "Input Manager". Using Input Manager, you can name, add or delete your inputs.

Now right click on one of your inputs. Here you can decide if you want the script act on each item separately, or to act on the whole list of items, using "Item Access" or "List Access". then you can give it a type hint to indicate what type of data you want to input (Curve, Line, Point3d, Boolean, Number, etc.)

In order to use Line objects and Curve objects, rather than Guid objects (Guid objects are the default) you need to right click on the component and select "Rhinoscriptsyntax usage > RhinoCommon / Provide ghdoc variable". Now your inputs will contain RhinoCommon objects (such as Point3d, Line, Curve objects), NOT Guids (rhinoscriptsyntax uses Guids to reference objects).

(Note: this next is not necessary, just a preference) You can also hide the code input if you want by doing this:

Once I have done the steps above, I can use a script like this to filter by length:

Notice that in the script above, where I write "if line.Length", then the variable name "line" must contain a Line object, NOT a Guid object. Line objects have a "Length" property, therefore I can write "Line.Length" or "pumpkin.Length" if "pumpkin" contains a Line object. If it contained a Guid object, I would instead use rhinoscriptsyntax functions.

You might want to try adding a "length" number input, and then comparing the input of each line to the length number you input with a slider.

You can also change outputs.

Attachments:

One more thing that might not be obvious.

the names I give to the inputs are the variable names I use in the script.

If I named an input "holes" and input a list of circles into the "holes" input, I could then access the list of circles in my script with that variable name: "holes"

for example: "print [c.Radius for c in holes]"

Wow~~~ Thank you so much.

By the way, If I try to use rhinoscriptsyntax functions,is there anything I should change? Cause I fail again when I try to use rhinoscriptsyntax for the example you gave me. I already changed the "Rhinoscriptsyntax usage" to "Rhinoscriptsyntax/Automatically marshal Guids"

here is my script:

import rhinoscriptsyntax as rs

# note that "Curves" is not equal to "curves"

# variables in python are case sensitive

print len(Curves)

print len(Lines)
# create an empty listlinesToKeep = []
# loop through the lines

for line in Lines:

# if the length of this line is long enough    i

f line.Length > 0.5:

rs.MoveObject(line,(0,0,5))

linesToKeep.append( line )

# after the loop, output our list from a

a = linesToKeep

Regards,

Suzy

Also, a simpler solution to your problem:

Attachments:

Here I found same question, and don't understand why I failed all the time when I tried to use Rhinoscriptsyntax.

what does the error message say when you hold your mouse over the red bubble?

Attachments:

Hmm.

I think you hit a bug. It should work, but I still get errors.

Attached is the same file with the correct type hints and input methods.

You might want to post this in the Errors forum for Giulio or others to look at.

Attachments:
I already updated the Rhino5 and grasshopper. It solved my problems. Thank you so much for your help.