Grasshopper

algorithmic modeling for Rhino

How to refer to points in python... data matching and more

I actually have like... 3 questions.. I searched widely but I am still confused.. I'll really appreciate it if you guys can help

I wanna calculate the distance from a list of points to a single point.

1. How do I refer to the points in the for loop?

    I have a list of points in grasshopper (not rhino objects) and I plugged them into python and named the input 'points'.

    Then I had something like this:

        for pt in points:
            ptCoord = rs.PointCoordinates(pt)
            x = ptCoord[0]
            y = ptCoord[1]
            z = ptCoord[2]

    But I got error report in the bolded line saying 

Parameter must be a Guid or string representing a Guid

2. How do I match multiple data to one

    I know it's weird since the script can't read the points.. but sometimes the 'Distance' rhinoscript does work... however, it's giving me a single distance while I need a list of distances from multiple points to a target point

        for pt in points:
            distance = rs.Distance(targetpoint,pt)

        print distance

    I don't know why it worked (it gave me a number so it seems to be reading the points).. However, it's giving me one single value. Is there anyway I cold make it a list?

3. This is extra.. but the ghpython doesn't fully work on my computer and it's weird..

    It's ok if you guys completely ignore this question..

    I just can't add new ghpython component to my computer.. Every time I tried to duplicate existing components, it gives me an error report saying it's missing the plug-in "python interpreter".. Also, even though grasshopper can successfully open this particular file that I'm working on, it gives me an error report and quit loading ghpython component every time I try to open another file containing ghpython..

    I downloaded my ghpython from food4rhino, which works fine in my classmates' computers... I 'installed' (drag the ghpython into grasshopper) multiple times but it still doesn't seem to be working

    Then I downloaded IronPython because some say it might work? But it didn't..

Thank you guys so much for this!

Views: 2812

Replies to This Discussion

Hi Samson,

1. & 2. I'd suggest using RhinoCommon in lieu of rhinoscriptsyntax. Rhinoscriptsyntax seems to cause memory leaks and has some other hiccups in GHPython. See image below.

In this case I'm printing the values as a list. If I were to set "distances" to "a," say, the script would only return one value. To output the entire list, you first have to create an empty list, then append that list. See below.

3. I'm not sure about this one. Mind posting a screen capture with the error(s)?

Hope this helps,
/SPM

Hi Samson

there is no problem in using RhinoScript in GhPython, too. I encourage you to read the GhPython 'help', located in the context menu after right-clicking the center of the component.


You basically need to use the RhinoScriptSytax hint (right click on the input parameter, -> Type Hints -> the second one). That is on by default. Then, every geometry object is added to the Grasshopper document and you can use its ID just as you are used to in the editor with rhinoscriptsytax. If you want to see all objects that have been added, assign an output variable to ghdoc (a = ghdoc, by default).

You also need to get familiar with at least 'Item Access' and 'List access'. This is also covered in the help. In short, you can get a single item at a time in a script repetition, or each whole list together. If you need the whole list, you can use a for loop as Stephen mentions. Rhinoscriptsytax is perfectly fine for that job.


Stephen, I'd like to hear more about 'the memory leaks' and 'the hiccups' you are having, so I or someone else can fix them. I do not believe there are any memory leaks in rhinoscriptsyntax, but I'd be glad to hear about them. 


Finally, the error at 3. is just due to a very old Rhino Service Release (SR). Official Rhino remind you of updates. You need to update it to a fresher SR: there are additions that were necessary and were missing in early releases.

Thanks,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hi Giulio,

Thanks for pointing out the "Help" and covering some of the basics. I forgot to do so in my first reply.

Concerning the memory leaks, I misspoke (mistyped). I really meant overall performance. In my experience, I've found RhinoCommon to be perform better than RhinoScript, especially in the GHPython component. This is the primary "hiccup." Now, clearly this would not be the case when calculating the distance between points or when drawing primitive geometry, but when script become more complex, RhinoCommon seems to be the best course of action. This is just in my experience, mind you.

And the only memory leak I'm aware of occurs when multithreading as mentioned by Andrew here. Forgive my slip-up.

Best,
/SPM

P.S. Samson (and anyone else who may be interested): I created an "Intro. to GHPython" document, which you can download here. Please note this is not comprehensive by any means. It's a short intro. to help get you going. Giulio, I'd love your feedback!

Wow!

I really like that introduction!

Can I talk with the right people at McNeel to see how to reference better that document?


Yes, that occasional memory leak in the ghpythonlib.parallel.run is not related with rhinosciptsyntax and it is due to way IronPython caches methods -- it's a bug in the language itself we had looked into but cannot fix.

Python is not the most performing language because it is dynamic, but it's readable and easy-to-follow. If performance of single mathematical operations (like number additions, root calculations etc) were paramount, I would suggest to use the C++ SDK, or secondarily C# in Grasshopper. Note that all operations provided by Rhino, that make 90%+ of rhinoscriptsyntax, are written in the C++ SDK no matter in which language you call them.

The distance drill can be a "one liner" (really 2 with import statement) in rhinoscriptsyntax:

import rhinoscriptsyntax as rs
if x and y: a = rs.Distance(x, y)

See the file for more comments.

Attachments:

Ha, of course! Feel free to either shoot me a private message or an email if you want to discuss dissemination further. Glad you like it!

Thanks for the clarification above. I knew all that already; I really don't know what I was thinking when I replied the first time. (Maybe I wasn't thinking. Ha!)

/SPM

Thank you guys so much! It solv Sorry for the late reply.. I really appreciate it!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service