remove register/unregister from templates
[blender.git] / release / scripts / templates / operator_modal_view3d.py
1 import bpy
2 from mathutils import Vector
3 from bpy.props import FloatVectorProperty
4
5 class ViewOperator(bpy.types.Operator):
6     '''Translate the view using mouse events.'''
7     bl_idname = "view3d.modal_operator"
8     bl_label = "Simple View Operator"
9
10     offset = FloatVectorProperty(name="Offset", size=3)
11
12
13     def execute(self, context):
14         v3d = context.space_data
15         rv3d = v3d.region_3d
16
17         rv3d.view_location = self._initial_location + Vector(self.properties.offset)
18
19     def modal(self, context, event):
20         v3d = context.space_data
21         rv3d = v3d.region_3d
22
23         if event.type == 'MOUSEMOVE':
24             self.properties.offset = (self._initial_mouse - Vector((event.mouse_x, event.mouse_y, 0.0))) * 0.02
25             self.execute(context)
26
27         elif event.type == 'LEFTMOUSE':
28             return {'FINISHED'}
29
30         elif event.type in ('RIGHTMOUSE', 'ESC'):
31             rv3d.view_location = self._initial_location
32             return {'CANCELLED'}
33
34         return {'RUNNING_MODAL'}
35
36     def invoke(self, context, event):
37
38         if context.space_data.type == 'VIEW_3D':
39             v3d = context.space_data
40             rv3d = v3d.region_3d
41
42             context.manager.add_modal_handler(self)
43
44             if rv3d.view_perspective == 'CAMERA':
45                 rv3d.view_perspective = 'PERSP'
46
47             self._initial_mouse = Vector((event.mouse_x, event.mouse_y, 0.0))
48             self._initial_location = rv3d.view_location.copy()
49
50             return {'RUNNING_MODAL'}
51         else:
52             self.report({'WARNING'}, "Active space must be a View3d")
53             return {'CANCELLED'}