2010-07-08 16:24:24 +00:00
|
|
|
import bpy
|
2010-05-20 07:49:41 +00:00
|
|
|
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):
|
2010-05-23 05:34:45 +00:00
|
|
|
v3d = context.space_data
|
2010-05-20 07:49:41 +00:00
|
|
|
rv3d = v3d.region_3d
|
|
|
|
|
2010-09-09 18:03:57 +00:00
|
|
|
rv3d.view_location = self._initial_location + Vector(self.offset)
|
2010-05-20 07:49:41 +00:00
|
|
|
|
|
|
|
def modal(self, context, event):
|
2010-05-23 05:34:45 +00:00
|
|
|
v3d = context.space_data
|
2010-05-20 07:49:41 +00:00
|
|
|
rv3d = v3d.region_3d
|
|
|
|
|
|
|
|
if event.type == 'MOUSEMOVE':
|
2010-09-09 18:03:57 +00:00
|
|
|
self.offset = (self._initial_mouse - Vector((event.mouse_x, event.mouse_y, 0.0))) * 0.02
|
2010-05-20 07:49:41 +00:00
|
|
|
self.execute(context)
|
2010-10-02 21:02:40 +00:00
|
|
|
context.area.header_text_set("Offset %.4f %.4f %.4f" % tuple(self.offset))
|
2010-05-20 07:49:41 +00:00
|
|
|
|
|
|
|
elif event.type == 'LEFTMOUSE':
|
2010-10-02 21:02:40 +00:00
|
|
|
context.area.header_text_set()
|
2010-05-20 07:49:41 +00:00
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
elif event.type in ('RIGHTMOUSE', 'ESC'):
|
|
|
|
rv3d.view_location = self._initial_location
|
2010-10-02 21:02:40 +00:00
|
|
|
context.area.header_text_set()
|
2010-05-20 07:49:41 +00:00
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
|
|
|
|
def invoke(self, context, event):
|
|
|
|
|
2010-05-23 05:34:45 +00:00
|
|
|
if context.space_data.type == 'VIEW_3D':
|
|
|
|
v3d = context.space_data
|
2010-05-20 07:49:41 +00:00
|
|
|
rv3d = v3d.region_3d
|
|
|
|
|
2010-09-02 04:53:05 +00:00
|
|
|
context.window_manager.add_modal_handler(self)
|
2010-05-20 07:49:41 +00:00
|
|
|
|
|
|
|
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'}
|