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