Just brushing up my OpenGL programming skills by programming a simple overlay script in the 3D viewport. I am planning to implement a custom panel fro the viewport.
Second try
WIP Code
- In the above screenshot, the vertical yellow bar and the mouse position inside the 3d view are created by my script.
- I am currently developing a framework for quickly writing OpenGL overlay scripts to be displayed in the 3D view.
==========================================
# SPACEHANDLER.VIEW3D.DRAW
import Blender
from Blender.BGL import *
from Blender.Draw import *
from Blender.Window import *
# Aliases
v3 = glVertex3f
#--------------------------------------------------------------
def Bounds(pt, ptmin, ptmax):
return pt >= ptmin and pt <= ptmax
#--------------------------------------------------------------
def TransformMouseCoords(view):
view['moux'], view['mouy'] = GetMouseCoords()
vieworigin = view['vertices'][:2]
view['vmoux'] = view['moux']-vieworigin[0]
view['vmouy'] = view['mouy']-vieworigin[1]
#--------------------------------------------------------------
def GetExtents(rect):
return ((rect[2] - rect[0]), (rect[3] - rect[1]))
#--------------------------------------------------------------
def Inside(view):
x, y = view['moux'], view['mouy']
rect = view['vertices']
return (Bounds(x, rect[0], rect[2]) and Bounds(y, rect[1], rect[3]))
#--------------------------------------------------------------
def GetViewInfo():
view3ds = GetScreenInfo(Types.VIEW3D)
for view in view3ds:
if view['id'] == GetAreaID():
return view
return None
#--------------------------------------------------------------
def UpdateInfo(view):
TransformMouseCoords(view)
view['width'], view['height'] = GetExtents(view['vertices'])
#--------------------------------------------------------------
def InitGL():
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glShadeModel(GL_FLAT)
glClearColor(0,0,0,1)
#--------------------------------------------------------------
def InitGLOverlay(view):
x, y, = view['vertices'][:2]
w, h = view['width'], view['height']
glViewport(x, y, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0, w, 0, h)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
#--------------------------------------------------------------
def DeInitGLOVerlay():
pass
#--------------------------------------------------------------
def DrawWidgets():
glBegin(GL_POLYGON)
v3(0, 0, 0)
v3(100, 0, 0)
v3(100, 100, 0)
v3(0, 100, 0)
glEnd()
#--------------------------------------------------------------
view = GetViewInfo()
if not view:
print "Could not retrieve view info. something's wrong"
else:
UpdateInfo(view)
if not Inside(view):
print "Mouse outside of view3d"
else:
print view['vmoux'],view['vmouy']
InitGL()
InitGLOverlay(view)
DrawWidgets()
DeInitGLOVerlay()



