Cleanup: use staticmethod where appropriate
[blender.git] / release / scripts / templates_py / 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(
12         name="Offset",
13         size=3,
14     )
15
16     def execute(self, context):
17         v3d = context.space_data
18         rv3d = v3d.region_3d
19
20         rv3d.view_location = self._initial_location + Vector(self.offset)
21
22     def modal(self, context, event):
23         v3d = context.space_data
24         rv3d = v3d.region_3d
25
26         if event.type == 'MOUSEMOVE':
27             self.offset = (self._initial_mouse - Vector((event.mouse_x, event.mouse_y, 0.0))) * 0.02
28             self.execute(context)
29             context.area.header_text_set("Offset %.4f %.4f %.4f" % tuple(self.offset))
30
31         elif event.type == 'LEFTMOUSE':
32             context.area.header_text_set(None)
33             return {'FINISHED'}
34
35         elif event.type in {'RIGHTMOUSE', 'ESC'}:
36             rv3d.view_location = self._initial_location
37             context.area.header_text_set(None)
38             return {'CANCELLED'}
39
40         return {'RUNNING_MODAL'}
41
42     def invoke(self, context, event):
43
44         if context.space_data.type == 'VIEW_3D':
45             v3d = context.space_data
46             rv3d = v3d.region_3d
47
48             if rv3d.view_perspective == 'CAMERA':
49                 rv3d.view_perspective = 'PERSP'
50
51             self._initial_mouse = Vector((event.mouse_x, event.mouse_y, 0.0))
52             self._initial_location = rv3d.view_location.copy()
53
54             context.window_manager.modal_handler_add(self)
55             return {'RUNNING_MODAL'}
56         else:
57             self.report({'WARNING'}, "Active space must be a View3d")
58             return {'CANCELLED'}
59
60
61 def register():
62     bpy.utils.register_class(ViewOperator)
63
64
65 def unregister():
66     bpy.utils.unregister_class(ViewOperator)
67
68
69 if __name__ == "__main__":
70     register()