2010-07-08 16:24:24 +00:00
|
|
|
import bpy
|
2010-02-28 14:57:26 +00:00
|
|
|
import bgl
|
|
|
|
import blf
|
2010-02-28 09:36:02 +00:00
|
|
|
|
2011-01-26 07:54:27 +00:00
|
|
|
|
2010-02-28 09:36:02 +00:00
|
|
|
def draw_callback_px(self, context):
|
2010-02-28 11:18:54 +00:00
|
|
|
print("mouse points", len(self.mouse_path))
|
|
|
|
|
2011-01-26 07:54:27 +00:00
|
|
|
font_id = 0 # XXX, need to find out how best to get this.
|
2010-04-27 19:46:15 +00:00
|
|
|
|
2010-02-28 11:18:54 +00:00
|
|
|
# draw some text
|
2010-04-27 19:46:15 +00:00
|
|
|
blf.position(font_id, 15, 30, 0)
|
|
|
|
blf.size(font_id, 20, 72)
|
|
|
|
blf.draw(font_id, "Hello Word " + str(len(self.mouse_path)))
|
2010-02-28 09:36:02 +00:00
|
|
|
|
|
|
|
# 50% alpha, 2 pixel width line
|
2010-02-28 14:57:26 +00:00
|
|
|
bgl.glEnable(bgl.GL_BLEND)
|
|
|
|
bgl.glColor4f(0.0, 0.0, 0.0, 0.5)
|
|
|
|
bgl.glLineWidth(2)
|
2010-02-28 09:36:02 +00:00
|
|
|
|
2010-02-28 14:57:26 +00:00
|
|
|
bgl.glBegin(bgl.GL_LINE_STRIP)
|
2010-02-28 09:36:02 +00:00
|
|
|
for x, y in self.mouse_path:
|
2010-02-28 14:57:26 +00:00
|
|
|
bgl.glVertex2i(x, y)
|
2010-02-28 09:36:02 +00:00
|
|
|
|
2010-02-28 14:57:26 +00:00
|
|
|
bgl.glEnd()
|
2010-02-28 09:36:02 +00:00
|
|
|
|
2010-03-06 01:40:29 +00:00
|
|
|
# restore opengl defaults
|
2010-02-28 14:57:26 +00:00
|
|
|
bgl.glLineWidth(1)
|
|
|
|
bgl.glDisable(bgl.GL_BLEND)
|
|
|
|
bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
|
2010-02-28 09:36:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ModalDrawOperator(bpy.types.Operator):
|
|
|
|
'''Draw a line with the mouse'''
|
2010-02-28 11:18:54 +00:00
|
|
|
bl_idname = "view3d.modal_operator"
|
|
|
|
bl_label = "Simple Modal View3D Operator"
|
2010-02-28 09:36:02 +00:00
|
|
|
|
|
|
|
def modal(self, context, event):
|
|
|
|
context.area.tag_redraw()
|
|
|
|
|
|
|
|
if event.type == 'MOUSEMOVE':
|
|
|
|
self.mouse_path.append((event.mouse_region_x, event.mouse_region_y))
|
|
|
|
|
|
|
|
elif event.type == 'LEFTMOUSE':
|
|
|
|
context.region.callback_remove(self._handle)
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
2011-08-16 18:59:45 +00:00
|
|
|
elif event.type in {'RIGHTMOUSE', 'ESC'}:
|
2010-02-28 09:36:02 +00:00
|
|
|
context.region.callback_remove(self._handle)
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
|
|
|
|
def invoke(self, context, event):
|
|
|
|
if context.area.type == 'VIEW_3D':
|
2010-12-08 11:42:11 +00:00
|
|
|
context.window_manager.modal_handler_add(self)
|
2010-03-06 01:40:29 +00:00
|
|
|
|
2010-02-28 09:36:02 +00:00
|
|
|
# Add the region OpenGL drawing callback
|
|
|
|
# draw in view space with 'POST_VIEW' and 'PRE_VIEW'
|
|
|
|
self._handle = context.region.callback_add(draw_callback_px, (self, context), 'POST_PIXEL')
|
|
|
|
|
|
|
|
self.mouse_path = []
|
|
|
|
|
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
else:
|
|
|
|
self.report({'WARNING'}, "View3D not found, cannot run operator")
|
|
|
|
return {'CANCELLED'}
|
2011-02-12 08:04:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def register():
|
|
|
|
bpy.utils.register_class(ModalDrawOperator)
|
|
|
|
|
|
|
|
|
|
|
|
def unregister():
|
|
|
|
bpy.utils.unregister_class(ModalDrawOperator)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
register()
|