diff options
author | Campbell Barton <ideasman42@gmail.com> | 2010-02-28 12:36:02 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2010-02-28 12:36:02 +0300 |
commit | 5369bd9c216243c4122d03896a3f7982631a4abf (patch) | |
tree | 46d920fdd4d44071d4e9e64b1759d9203fed2f6e /release/scripts/templates/operator_modal_draw.py | |
parent | 67af290bd125c181a1a851286da0677c7d5a1dbd (diff) |
- template with an example of a modal operator drawing with opengl (draw a line on the screen)
- access to event.mouse_region_x/y
- basic type checking to callback functions (use PyCapsule names)
Diffstat (limited to 'release/scripts/templates/operator_modal_draw.py')
-rw-r--r-- | release/scripts/templates/operator_modal_draw.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/release/scripts/templates/operator_modal_draw.py b/release/scripts/templates/operator_modal_draw.py new file mode 100644 index 00000000000..d830a380250 --- /dev/null +++ b/release/scripts/templates/operator_modal_draw.py @@ -0,0 +1,69 @@ +import BGL + +def draw_callback_px(self, context): + print("mouse points", len(self.mouse_path)) + + # 50% alpha, 2 pixel width line + BGL.glEnable(BGL.GL_BLEND) + BGL.glColor4f(0.0, 0.0, 0.0, 0.5) + BGL.glLineWidth(2) + + BGL.glBegin(BGL.GL_LINE_STRIP) + for x, y in self.mouse_path: + BGL.glVertex2i(x, y) + + BGL.glEnd() + + # restore opengl defaults + BGL.glLineWidth(1) + BGL.glDisable(BGL.GL_BLEND) + BGL.glColor4f(0.0, 0.0, 0.0, 1.0) + + +class ModalDrawOperator(bpy.types.Operator): + '''Draw a line with the mouse''' + bl_idname = "object.modal_operator" + bl_label = "Simple Modal Operator" + + 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'} + + elif event.type in ('RIGHTMOUSE', 'ESC'): + context.region.callback_remove(self._handle) + return {'CANCELLED'} + + return {'RUNNING_MODAL'} + + def invoke(self, context, event): + if context.area.type == 'VIEW_3D': + context.manager.add_modal_handler(self) + + # 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'} + + +def register(): + bpy.types.register(ModalDrawOperator) + + +def unregister(): + bpy.types.unregister(ModalDrawOperator) + + +if __name__ == "__main__": + register() |