update templates for registration changes
[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
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"
10
11     offset = FloatVectorProperty(name="Offset", size=3)
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.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.offset = (self._initial_mouse - Vector((event.mouse_x, event.mouse_y, 0.0))) * 0.02
25             self.execute(context)
26             context.area.header_text_set("Offset %.4f %.4f %.4f" % tuple(self.offset))
27
28         elif event.type == 'LEFTMOUSE':
29             context.area.header_text_set()
30             return {'FINISHED'}
31
32         elif event.type in ('RIGHTMOUSE', 'ESC'):
33             rv3d.view_location = self._initial_location
34             context.area.header_text_set()
35             return {'CANCELLED'}
36
37         return {'RUNNING_MODAL'}
38
39     def invoke(self, context, event):
40
41         if context.space_data.type == 'VIEW_3D':
42             v3d = context.space_data
43             rv3d = v3d.region_3d
44
45             context.window_manager.modal_handler_add(self)
46
47             if rv3d.view_perspective == 'CAMERA':
48                 rv3d.view_perspective = 'PERSP'
49
50             self._initial_mouse = Vector((event.mouse_x, event.mouse_y, 0.0))
51             self._initial_location = rv3d.view_location.copy()
52
53             return {'RUNNING_MODAL'}
54         else:
55             self.report({'WARNING'}, "Active space must be a View3d")
56             return {'CANCELLED'}
57
58
59 def register():
60     bpy.utils.register_class(ViewOperator)
61
62
63 def unregister():
64     bpy.utils.unregister_class(ViewOperator)
65
66
67 if __name__ == "__main__":
68     register()