Pick Points in Rhino and increment point name with Python

python
#1

Dear All,

I tried to write a small script in python to pick/snap points in rhino. I would like to insert the desired point number as a text object and change the point name to the same number. The goal is to increment the next number by 1. However I don’t know how to loop the procedure and increment the value/name by 1 of a new snapped point? At the moment I get 3 points for the picked point. I don’t know why?Test-Coordinate.py (909 Bytes)

I hope that somebody has time to have a look to the .Py file

Many Thanks,

Jaap Bijlstra

(Graham) #2

Hello,
You are getting 3 points because when you select a point it returns a tuple of x,y,z coordinates. You then iterate over these 3 coordinates but on each iteration you use the original tuple to create your point.
You can use the sticky dictionary to remember the previous point number so you can increment it.

#3

Hello Graham,

Thank you for you reply. I found this topic about sticky dictonary:https://developer.rhino3d.com/samples/rhinopython/sticky-values/ . This is the right one is it?
I will try to change the script with your comments.

Kind Regards,

Jaap

(Graham) #4

You’re welcome!
Something like this should do the trick for you:

import rhinoscriptsyntax as rs

def numpoints():
    number = rs.GetInteger("pick a number", 1)
    
    while True:
        pt = rs.GetPoint("select a point to number %i" % (number))
        if not pt:
            break
        newpt = rs.AddPoint(pt)
        rs.ObjectName(newpt, number)
        number += 1
    
if __name__ == "__main__":
    numpoints()

You only need the sticky dictionary if you want to remember the number between function calls

#5

Thank you for the ideas about how to accomplish the incrementing and looping. Two question: where is the % sign used for, to pick the list value of the next looped index number?

Last question what does the" if_name__=="_main":, numpoints() do?

Many Thanks,

Jaap

(Graham) #6

The % sign is used for string formatting to insert the value of number into the string.

I have wrapped the logic in a function called numpoints, defined using a def call . I then call that function. It is a good practice when writing scripts and allows you to reuse the same function, calling it from another script for instance.

For more on main functions in Python see here:

#7

Hello Graham,

Thank for this usefull information. Then I can better understand the use of main functions.

Kind Regards,

Jaap