Crashing Python, and need to check for ESC


#1

This is a dual question:

1: Why does this crash Rhino, and eat up so much ram so quickly? (I would expect it to draw thousands of points prior to crashing)

2: How do I check for keyboard presses in Python? I need to be able to “esc” scripts like these.

Thanks!


#2
# Draw points test

import rhinoscriptsyntax as rs
import RhinoPython.Host as __host

print "This is a test"

done = False

ballx = 0	# Ball position variables
bally = 0
ballxmove = 0.3
ballymove = 0.7

while done == False:

    ballx = ballx + ballxmove    # Update ball position
    bally = bally + ballymove
    
    rs.AddPoint( (ballx,bally,0) )

    if ballx > 30:       # Ball reached screen edges?
        ballxmove = ballxmove * -1
    if ballx < 0:
        ballxmove = ballxmove* -1
    if bally > 30:
        ballymove = ballymove* -1
    if bally < 0:
        ballymove = ballymove* -1

#3

Apparently Pythonscripts and Discourse don’t play too well together…
Hashtags and indents kind of mess this up a bit.


Loop that crash my computer
#4

I do not know what RhinoPython.Host does, but you could just define the number of iterations, like:

import rhinoscriptsyntax as rs

print "This is a test"

ballx = 0 # Ball position variables
bally = 0
ballxmove = 0.3
ballymove = 0.7

i = 0
maxSteps = 200    # number of iterations
while i < maxSteps:

    ballx = ballx + ballxmove    # Update ball position
    bally = bally + ballymove
    
    sph = rs.AddSphere( (ballx,bally,0) ,2)
    rs.DeleteObject(sph)
    if ballx > 30:       # Ball reached screen edges?
        ballxmove = ballxmove * -1
    if ballx < 0:
        ballxmove = ballxmove* -1
    if bally > 30:
        ballymove = ballymove* -1
    if bally < 0:
        ballymove = ballymove* -1
    i += 1

Sorry if this was not of a help.


How to cancel script execution
#5

I limited it to 10K points and it didn’t crash… With 100K points it appeared to hang almost immediately, but I let it run and it finished. However, when I hit Undo, then it hung again, this time for a long time, but it finally came back too.

I thought at first that the first hang was the screen redraw, it’s probably filling some buffer somewhere. However, if I cut the redraw, still runs really slowly. I have a feeling each point added is an undo step, so that may be the hangup… Edit: after thinking about it some more, it’s probably just the time it takes to add each individual point to the document that’s causing the hang

Unless your’re trying to illustrate something animated on-screen, I would just put the point locations in one big list and then add them to the document at the end (as a point cloud even better).

–Mitch


#6

There is something definitely odd about this though. If I just try to delete the 100K points I made with the script (instead of trying to undo) Rhino also hangs for long minutes…

–Mitch


How to abort a script?
(Steve Baer) #7

I added the tags to your post so it will format python code correctly on discourse


(Steve Baer) #8

For the escape key, you can import the scriptcontext module and test for escape when you feel you want to.

import scriptcontext

for i in range(1000000):
    #do something
    #test every 100th iteration
    if i%100==0 and scriptcontext.escape_test(False):
        break #get out of the loop

#9

Thanks for looking into this guys, the script is just a “warm up” to see if I can bend my mind around Python, and this is a basically a part of a simple python game that has been altered by me to add points instead of moving a ball, and while I was testing it I encountered this oddity.

Rhino redraws fine for a while, then that stops, then Rhino hangs but the memory usage keeps on going up. And we are not talking about crazy-many points before it hangs.

And thanks for the escape sollution @stevebaer. Python is simpler yet harder to understand than Rhinoscript, but I’ll get there.


#10

Hi djordje,
Thanks for the feedback.

This was just a test to see if I could use key input, and then I discovered that I could not Esc the script like I was used to with Rhinoscript, so that became the topic.

RhinoPython.Host was something I forgot to delete. It was from an earlier “esc” test.


#11

Hi Steve, adding that code fixed the crash too… why is that?
With that piece of code Rhino can calculate thousands of points and I can ESC it when I want. But if I don’t add it then Rhino crashes after just a 200 points or so and the memory usage keeps on piling up.

So what is causing that? It seems very odd to me.


(Steve Baer) #12

It is hard to say without digging really deep into this. The escape key test may be letting the windows message pump wake up and get processed, where in your endless loop the message pump is never processed. You may actually be at millions of points when Rhino crashes; it is only at a couple hundred that you see the points.


#13

Well as I posted previously, if I limit the loop I can easily do 10K points, but not 100K. It’s not running out of memory either, memory usage is pretty reasonable. Something about adding each point to the document as you go, I suspect…?

–Mitch


(Steve Baer) #14

If you make 100K points outside of python, does undo work slowly? Make a point and then run the array command to create 100K.

Each point is an individual object in the document which mean it also has attribute information attached to each point. This is why it is going to be much “heavier” than a single point cloud with 100K points.


#15

Hi Steve,
this makes theoretic sense, but would require that Rhino suddenly turned off Redraw and superquickly added the millions of points. Why It would suddenly stop to redraw at sub 1K points makes absolutely no sense to me. Adding the ESC function should not prevent the graphicspipeline to crash…

Anyway you have the scriptdata that crashes, and superiour knowhow, so you can investigate when you want.
Good luck!