remove register/unregister from templates
[blender.git] / release / scripts / templates / operator_modal.py
1 import bpy
2 from bpy.props import *
3
4 class ModalOperator(bpy.types.Operator):
5     '''Move an object with the mouse, example.'''
6     bl_idname = "object.modal_operator"
7     bl_label = "Simple Modal Operator"
8
9     first_mouse_x = IntProperty()
10     first_value = FloatProperty()
11
12     def modal(self, context, event):
13         if event.type == 'MOUSEMOVE':
14             delta = self.properties.first_mouse_x - event.mouse_x
15             context.object.location.x = self.properties.first_value + delta * 0.01
16
17         elif event.type == 'LEFTMOUSE':
18             return {'FINISHED'}
19
20         elif event.type in ('RIGHTMOUSE', 'ESC'):
21             context.object.location.x = self.properties.first_value
22             return {'CANCELLED'}
23
24         return {'RUNNING_MODAL'}
25
26     def invoke(self, context, event):
27         if context.object:
28             context.manager.add_modal_handler(self)
29             self.properties.first_mouse_x = event.mouse_x
30             self.properties.first_value = context.object.location.x
31             return {'RUNNING_MODAL'}
32         else:
33             self.report({'WARNING'}, "No active object, could not finish")
34             return {'CANCELLED'}
35
36
37 if __name__ == "__main__":
38     bpy.ops.object.modal_operator()