Return values for each point in the list

Hi, I have this code, which returns the coordinates for only one 3dpoint, although I selected more points. How can I get the coordinates for each point that I select?

import rhinoscriptsyntax as rs

points = rs.GetObjects(“referentiaza punctele”,1)

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

print “x: %.2f, y: %.2f, z:%.2f” %(x,y,z)

Well, in the forum post, you lost your formatting, so it’s hard to tell - but in order to print the coordinates of each point, the print statement needs to be inside the loop so it runs on each iteration. Otherwise you will only print the last point. This works:

import rhinoscriptsyntax as rs

points = rs.GetObjects("referentiaza punctele",1)

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

You can also collect the point coordinates in a list, and then loop through the list:

import rhinoscriptsyntax as rs

points = rs.GetObjects("referentiaza punctele",1)

#create a list of point3d objects from your selected Rhino points
#the syntax is called a "list comprehension"
pts=[rs.coerce3dpoint(ptID) for ptID in points]
for pt in pts:
    #point3d objects have properties .X, .Y, .Z which are the coordinates
    #Below uses the "format" statement to format the numbers
    print "x: {:.2f}, y: {:.2f}, z: {:.2f}".format(pt.X,pt.Y,pt.Z)

HTH, --Mitch

Scripting is beyond me but it can be easily done in Grasshopper?

print was outside the loop. When i put it inside the loop (indent it), it works. Thanks a lot.

I’m trying to learn a little bit how to get things done in Python. But thank you.

1 Like

Thank you very much. I struggled for two days with this.

I thought as much :slight_smile:

I’m trying to learn Grasshopper so looking for solutions helps me to improve my knowledge in that area, although it’s not as impressive as learning Python. Plug in and play doesn’t have the same kudos, it’s like trying to impress people with your new iPhone. You didn’t make it, you only bought it :smiley:

step by step