If my cursor is within a scecified range…
Let’s say for the sake of illustration > x100 ,< x200, > y100 and <y200.
What is my best bet for expressing that in Python?
If my cursor is within a scecified range…
Let’s say for the sake of illustration > x100 ,< x200, > y100 and <y200.
What is my best bet for expressing that in Python?
I’m a newbie to Python, but I think I can figure it out if only I can find the command to return the cursor location. So far, this has eluded me. Also, are you talking about a mouse cursor, or text? I’m assuming mouse, but you never know…
Use the Blender.Draw.MOUSEX and Blender.Draw.MOUSEY events to do that in your key event function. The value is then the coordinate of the cursor (it is NOT absolute coordinate, but relative to the screen IIRC).
Martin
Martin:
That is good to know about the absolute value, but not really what I am after unless you are just writing MILES over my head. Not beyond the realm of possibility.
Let me try this again…
I am trying to determine whether my cursor is over a specified area in my GUI.
That 100x100 box in my example… for example.
How do I say (in Pythonese) that the cursor has entered this zone?
Merc:
Check this out. I understood a lot when I accidently wrote this.
from Blender.Draw import *
from Blender.BGL import *
def gui():
glClearColor(.5,0,0, 10)
glClear(GL_COLOR_BUFFER_BIT)
glRasterPos2i(50, 0)
Button("Exit", 1, 10, 10, 150, 25, "Blow Merc's computer up!!!")
def event(evt, val):
#####Uncomment this next line to see everything
#print "event : ", evt , val
if (evt == 4):
print val
if (evt == 5):
print val
if (evt == 11):
print "Up"
if (evt == 10):
print "Down"
def bevent(evt):
if (evt == 1): Exit()
Register(gui, event, bevent)
I included this because I just made it and it’s Cool.
http://www.door3.org/Knob.zip
Haven’t checked out your zip file yet, but the script keeps saying
def event(evt, val):
Syntax error.
I know I’ve had this happen before, and it was always something stupid that I told myself I’d remember, but for the life of me, I can’t remember what it was . But thanks anyway, even without being able to run it, I learned a thing or two.
I just copied it to the clipboard and then to Blender and got errors too. Then I copied it to notepad (both times I took out those nasty space characters that get added when you copy from HTML) and it worked out just fine.
That is a nice feature that 2.27 has in being able to cut and paste but it really does not work reliably with anything copied from outside of Blender.
Now back to business here, can’t somebody tell me how to know if I’m in the zone? :o
In the zone? That would be be when your eyes don’t cross as you think of a tree falling in the woods to the sound of one hand clapping.
from Blender.Draw import *
from Blender.BGL import *
Cursor = [0,0]
def gui():
glClearColor(.5,0,0, 10)
glClear(GL_COLOR_BUFFER_BIT)
glRasterPos2i(50, 0)
Button("Exit", 1, 10, 10, 150, 25, "Blow Merc's computer up!!!")
def event(evt, val):
global Cursor
if (evt == MOUSEX):
Cursor[0] = val
print "inside the box!"
if (evt == MOUSEY):
Cursor[1] = val
if 100 <= Cursor[0] >= 200 and 100 <= Cursor[1] >= 200:
print "inside the box!"
if (evt == QKEY and val):
Exit()
def bevent(evt):
if (evt == 1):
Exit()
Register(gui, event, bevent)
try this
Martin
Well…Thats not working so good.
I messed around with it most of the day today, but I am not getting it.
I am guseeing that there may be another line of code between these two that did not find it’s way into this post.
Cursor[0] = val
print "inside the box!"
I am at a very frustrating point in this Python learning curve!
silly error on my part.
that block needs the same if clause as the other, of course.
Martin
Nope I could not make that work either.
Comments Please:
Is this a dumb or impractical way to approach this problem?
How would I approach it differently if I knew what I was about?
(It does work like this…)
from Blender.Draw import *
from Blender.BGL import *
D0=3
D1=3
D2=3
D3=3
D4=(D0+D1+D2+D3)
def gui():
glClearColor(.5,0,0, 10)
glClear(GL_COLOR_BUFFER_BIT)
glRasterPos2i(50, 0)
Button("Exit", 1, 10, 10, 150, 25, "Yikes!!!")
BOX(200,100,100,200)
def event(evt, val):
global Cursor,D4,D3,D2,D1,D0
if ( evt == 4):
if val >= 200:
D0=1
D4=(D0+D1+D2+D3)
if val <= 200:
D0=0
D4=(D0+D1+D2+D3)
if val <= 100:
D1=1
D4=(D0+D1+D2+D3)
if val >= 100:
D1=0
D4=(D0+D1+D2+D3)
if ( evt == 5):
if val <= 200:
D2=0
D4=(D0+D1+D2+D3)
if val >= 200:
D2=1
D4=(D0+D1+D2+D3)
if val >= 100:
D3=0
D4=(D0+D1+D2+D3)
if val <= 100:
D3=1
D4=(D0+D1+D2+D3)
if (D4 == 0):
print "In The Zone!"
elif(D4 != 0):
print "Not in The Zone!"
if (evt == QKEY and val):
Exit()
def bevent(evt):
if (evt == 1):
Exit()
def BOX(x1,y1,x2,y2):
glColor3f (0.0, 0.0, 0.0);
glLineWidth (1);
glBegin (GL_LINE_LOOP);
glVertex2f (x1, y1);
glVertex2f (x2, y1);
glVertex2f (x2, y2);
glVertex2f (x1, y2);
glEnd ();
Register(gui, event, bevent)
Note: To work correctly this GUI would need to run in the lower left hand corner without a header. Any suggestions as to how that would be overcome would be gratefully recieved.
from Blender.Draw import *
from Blender.BGL import *
Cursor = [0,0]
lbl = "Outside"
SBox = Buffer(GL_FLOAT, 4)
def gui():
global SBox
glGetFloatv(GL_SCISSOR_BOX, SBox)
glClearColor(.5,0,0, 10)
glClear(GL_COLOR_BUFFER_BIT)
glRasterPos2i(100, 100)
Text(lbl)
Button("Exit", 1, 10, 10, 150, 25, "Blow Merc's computer up!!!")
def event(evt, val):
global Cursor, lbl, SBOX
if (evt == MOUSEX):
Cursor[0] = val
if 100 <= (Cursor[0]-SBox[0]) <= 200 and 100 <= (Cursor[1]-SBox[1]) <= 200:
lbl = "Inside"
Redraw()
else:
lbl = "Outside"
Redraw()
if (evt == MOUSEY):
Cursor[1] = val
if 100 <= (Cursor[0]-SBox[0]) <= 200 and 100 <= (Cursor[1]-SBox[1]) <= 200:
lbl = "Inside"
Redraw()
else:
lbl = "Outside"
Redraw()
if (evt == QKEY and val):
Exit()
def bevent(evt):
if (evt == 1):
Exit()
Register(gui, event, bevent)
try that, it works for real.
My first mistake was to think that the cursor position was relative to the screen, but it is not, it’s global.
In that script, the SBox variable contains the GL_SCISSOR_BOX values. It’s an array of 4 items, the first two being the X, Y coordinates of the screen and the 3rd and 4th being the width and height of the screen.
My second mistake was in the logical operations in the if clause, but it’s fixed now.
Martin
another thing, if you want to draw rectangles, use the Blender.BGL.glRecti function, like this:
from Blender.Draw import *
from Blender.BGL import *
Cursor = [0,0]
lbl = "Outside"
SBox = Buffer(GL_FLOAT, 4)
def gui():
global SBox
glGetFloatv(GL_SCISSOR_BOX, SBox)
glClearColor(.5,0,0, 10)
glClear(GL_COLOR_BUFFER_BIT)
glRecti(100,100,200,200)
glRasterPos2i(100, 50)
Text(lbl)
Button("Exit", 1, 10, 10, 150, 25, "Blow Merc's computer up!!!")
def event(evt, val):
global Cursor, lbl, SBOX
if (evt == MOUSEX):
Cursor[0] = val
if 100 <= (Cursor[0]-SBox[0]) <= 200 and 100 <= (Cursor[1]-SBox[1]) <= 200:
lbl = "Inside"
Redraw()
else:
lbl = "Outside"
Redraw()
if (evt == MOUSEY):
Cursor[1] = val
if 100 <= (Cursor[0]-SBox[0]) <= 200 and 100 <= (Cursor[1]-SBox[1]) <= 200:
lbl = "Inside"
Redraw()
else:
lbl = "Outside"
Redraw()
if (evt == QKEY and val):
Exit()
def bevent(evt):
if (evt == 1):
Exit()
Register(gui, event, bevent)
Martin
Yeah Baby!
Thats what I was hoping for.
Martin: Once again, my grattitude!
Door3