Problem with continuous draw.register

Ok, I modified some tutorial code to draw some parabolas and draw some data from a data file. This used to be the code that changed background colors with mouse button clicks. Anyway, I think it is continuously redrawing the scene, which I do not want because it is going through a lot of operations for the same picture and slowing my machine down. Is there a way that it will run once, then hold the image on the screen until i press q or esc?

import math
import Blender
from Blender.BGL import *
from Blender import Draw
R = G = B = 1.0
A = 1
title = "Testing BGL  + Draw"
instructions = "Use mouse buttons or wheel to change the background color."
quitting = " Press ESC or q to quit."
len1 = Draw.GetStringWidth(title)
len2 = Draw.GetStringWidth(instructions + quitting)
#
def show_win():
  glClearColor(R,G,B,A)                # define color used to clear buffers 
  glClear(GL_COLOR_BUFFER_BIT)         # use it to clear the color buffer
  glColor3f(0,0,0)            # define default color
  for i in range(1000):
    for j in range(1000):
      y1 = (((i-500)**2)/(-150.0)) + 700
      y2 = (((i-500)**2)/(-300.0)) + 850
      if i<500:
        y5 = 200*math.cos(6.28318530781*(i+150)/500)+200
      else:
        y5 = 200*math.sin(6.28318530781*(i-25)/500)+200
      if ((j<y1 and j>y2) or (j > y1 and j <y2))and j>y5:
        glBegin(GL_POLYGON)
    glVertex2f(i/2.0-0.25, j/2.0-.25)
    glVertex2f(i/2.0-0.25, j/2.0+.25)
    glVertex2f(i/2.0+0.25, j/2.0+.25)
    glVertex2f(i/2.0+0.25, j/2.0-.25)
    glEnd()
  glColor3f(.5,.5,.5)
  for i in range(1000):
    for j in range(1000):
      y3 = (((i-500)**2)/(-200.0)) + 750
      y4 = (((i-500)**2)/(-250.0)) + 800
      if i<500:
        y6 = 200*math.cos(6.28318530781*(i+150)/500)+225
      else:
        y6 = 200*math.sin(6.28318530781*(i-25)/500)+225
      if ((j<y3 and j>y4) or (j>y3 and j<y4)) and j>y6:
        glBegin(GL_POLYGON)
    glVertex2f(i/2.0-0.25, j/2.0-0.25)
    glVertex2f(i/2.0-0.25, j/2.0+0.25)
    glVertex2f(i/2.0+0.25, j/2.0+0.25)
    glVertex2f(i/2.0+0.25, j/2.0-0.25)
    glEnd()
          
  f=file('c:\documents and settings\sshumat\desktop\cppdata.txt','r')
  f.seek(0)
  str=f.readline()
  temp=str.split()
  numberdata=float(temp[0])
  str=f.readline()
  temp=str.split()
  popmax=float(temp[0])
  for i in range(numberdata):
    str=f.readline()
    temp=str.split()
    x=float(temp[0])
    y=float(temp[1])
    pop=float(temp[2])
    shade=(pop/(4*popmax)+0.75)
    glColor3f(shade, shade, shade)
    glBegin(GL_POLYGON)
    glVertex2f(x/2.0-0.25, y/2.0-0.25)
    glVertex2f(x/2.0-0.25, y/2.0+0.25)
    glVertex2f(x/2.0+0.25, y/2.0+0.25)
    glVertex2f(x/2.0+0.25, y/2.0-0.25)
    glEnd()
def ev(evt, val):                      # event callback for Draw.Register()

  global R,G,B,A                       # ... it handles input events
  if evt == Draw.ESCKEY or evt == Draw.QKEY:
    Draw.Exit()                        # this quits the script
Draw.Register(show_win, ev, None)      # start the main loop

Problem of your script isn’t in Draw.Register(), problem is that you are making a loop with 2.000.000 calculations and that’s what make your script slow.


for i in range(1000):
  for j in range(1000):
  # 1000x1000 = 1.000.000 of calculations
for i in range(1000):
  for j in range(1000):
 # 1000x1000 = 1.000.000 of calculations

That piece of your code need’s 6 secs to execute on my machine and that’s way too slow for drawing.

Obviously I’m not concerned with real time drawing at this point. If I change the data file from which the script gets the information to be drawn while the program is running, then the image will update. So it seems to be constantly redrawing until I press q or escape. What I want is for it to draw the data, then keep the image up on the screen even if i change the data file. I don’t want it to constantly be trying to figure out if the data file has been changed so it can update.