Suggestions for exiting a Python loop

Hello,

First, I’m not really proud of this code - but it does what it needed to do. The user enters a starting number and then everywhere they click it creates a new Dot with an incremental value. However, I thought that I could exit the infinite loop with the ESC key, random profanity, or some other key. Instead, I basically have to close the application to get the script to stop.

import rhinoscriptsyntax as rs

DotNumber = rs.GetInteger("Number of the next dot", 1, 1, 100)
if DotNumber: 
	while True:
		ClickedPoint = rs.GetPoint("Pick dot location")
		if ClickedPoint:
			rs.AddTextDot( str(DotNumber),ClickedPoint)
			DotNumber += 1

I found some mentions of RhinoApp.EscapeKeyPressed but I didn’t understand how to apply it to a simple script like this.

Could somebody please offer a better way to accomplish this?

Thanks for the help.

You could get a whole bunch of points in one go instead. Rhino - RhinoScriptSyntax

If rs.GetPoints doesn’t provide its own way of exiting, the num of points can be capped by setting max_points, then afterwards the user can be sent to a:

https://developer.rhino3d.com/api/RhinoScriptSyntax/#userinterface-MessageBox

with message = 'Add more points?' and buttons = 4, and break the loop depending on their choice

The most pythonic way to write this specific loop could be a oneliner

for num in count(start=(rs.GetInteger("Number of the next dot", 1, 1, 100) or 1)):
    if rs.GetPoint("Pick dot location") is not None:
        rs.AddTextDot(str(num), _)

However you should just use

GetPoints(draw_lines=False, in_plane=False, message1=None, message2=None, max_points=None, base_point=None)

Farouk

Thank you for the suggestions. I wasn’t able to get the suggested code to run… maybe I messed something up?

The issue with GetPoints is that you have no indication of where you clicked because the list of points is only returned when it finishes. Hopefully, there is a way to run a loop similar to the original one but respect the ESC key.

Cheers,
Henry

for num in range(count)

1 Like

This means that the user has to know how many points they want to create before they start, correct?

Yes, the user has either got to pick a number of dots in advance, or they’ve got to take some action (clicking a window or pressing Esc) every so often to choose to either break the loop or continue placing text dots. But stop right there…

The code’s fine. Its taught you a lot about how both Python and Rhino works, but before you spend even more time on this approach (let alone look into packaging it for distribution), I think you’re going down a bit of a rabbit hole.

rs.AddTextDot is just adding what Rhino natively calls “annotation dots”

image

If you were planning on doing further processing of the list of dots afterwards, there are lots of ways to select objects in Rhino (that work especially well in Grasshopper), Putting “further processing” in its own script or its own GhPython component achieves a Separation of Concerns. As well as empowering your user, and giving them more verstile tools.