diff options
author | Campbell Barton <ideasman42@gmail.com> | 2010-05-20 11:49:41 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2010-05-20 11:49:41 +0400 |
commit | 79a45e75d352e73c543cedc46b3c62d89e38b754 (patch) | |
tree | 5b6f18b4e2a400b0107403cb7133290a0c2feaf3 /release/scripts/templates/operator_modal_view3d.py | |
parent | 37ecf15f3f43e71de89d7aee57b4938f5d3c1263 (diff) |
view3d modal operator template
Diffstat (limited to 'release/scripts/templates/operator_modal_view3d.py')
-rw-r--r-- | release/scripts/templates/operator_modal_view3d.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/release/scripts/templates/operator_modal_view3d.py b/release/scripts/templates/operator_modal_view3d.py new file mode 100644 index 00000000000..2158d9dcc3f --- /dev/null +++ b/release/scripts/templates/operator_modal_view3d.py @@ -0,0 +1,56 @@ +from mathutils import Vector +from bpy.props import FloatVectorProperty + +class ViewOperator(bpy.types.Operator): + '''Translate the view using mouse events.''' + bl_idname = "view3d.modal_operator" + bl_label = "Simple View Operator" + + offset = FloatVectorProperty(name="Offset", size=3) + + + def execute(self, context): + v3d = context.area.spaces[0] + rv3d = v3d.region_3d + + rv3d.view_location = self._initial_location + Vector(self.properties.offset) + + def modal(self, context, event): + v3d = context.area.spaces[0] + rv3d = v3d.region_3d + + if event.type == 'MOUSEMOVE': + self.properties.offset = (self._initial_mouse - Vector((event.mouse_x, event.mouse_y, 0.0))) * 0.02 + self.execute(context) + + elif event.type == 'LEFTMOUSE': + return {'FINISHED'} + + elif event.type in ('RIGHTMOUSE', 'ESC'): + rv3d.view_location = self._initial_location + return {'CANCELLED'} + + return {'RUNNING_MODAL'} + + def invoke(self, context, event): + active_space = context.area.spaces[0] + + if active_space.type == 'VIEW_3D': + v3d = active_space + rv3d = v3d.region_3d + + context.manager.add_modal_handler(self) + + if rv3d.view_perspective == 'CAMERA': + rv3d.view_perspective = 'PERSP' + + self._initial_mouse = Vector((event.mouse_x, event.mouse_y, 0.0)) + self._initial_location = rv3d.view_location.copy() + + return {'RUNNING_MODAL'} + else: + self.report({'WARNING'}, "Active space must be a View3d") + return {'CANCELLED'} + + +bpy.types.register(ViewOperator) |