2 from mathutils import Vector
3 from bpy.props import FloatVectorProperty
6 class ViewOperator(bpy.types.Operator):
7 '''Translate the view using mouse events.'''
8 bl_idname = "view3d.modal_operator"
9 bl_label = "Simple View Operator"
11 offset = FloatVectorProperty(name="Offset", size=3)
13 def execute(self, context):
14 v3d = context.space_data
17 rv3d.view_location = self._initial_location + Vector(self.offset)
19 def modal(self, context, event):
20 v3d = context.space_data
23 if event.type == 'MOUSEMOVE':
24 self.offset = (self._initial_mouse - Vector((event.mouse_x, event.mouse_y, 0.0))) * 0.02
26 context.area.header_text_set("Offset %.4f %.4f %.4f" % tuple(self.offset))
28 elif event.type == 'LEFTMOUSE':
29 context.area.header_text_set()
32 elif event.type in ('RIGHTMOUSE', 'ESC'):
33 rv3d.view_location = self._initial_location
34 context.area.header_text_set()
37 return {'RUNNING_MODAL'}
39 def invoke(self, context, event):
41 if context.space_data.type == 'VIEW_3D':
42 v3d = context.space_data
45 context.window_manager.modal_handler_add(self)
47 if rv3d.view_perspective == 'CAMERA':
48 rv3d.view_perspective = 'PERSP'
50 self._initial_mouse = Vector((event.mouse_x, event.mouse_y, 0.0))
51 self._initial_location = rv3d.view_location.copy()
53 return {'RUNNING_MODAL'}
55 self.report({'WARNING'}, "Active space must be a View3d")