Workspace: Move engines to workspace and Properties Editor cleanup
[blender.git] / release / scripts / templates_py / operator_modal.py
1 import bpy
2 from bpy.props import IntProperty, FloatProperty
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             self.first_mouse_x = event.mouse_x
30             self.first_value = context.object.location.x
31
32             context.window_manager.modal_handler_add(self)
33             return {'RUNNING_MODAL'}
34         else:
35             self.report({'WARNING'}, "No active object, could not finish")
36             return {'CANCELLED'}
37
38
39 def register():
40     bpy.utils.register_class(ModalOperator)
41
42
43 def unregister():
44     bpy.utils.unregister_class(ModalOperator)
45
46
47 if __name__ == "__main__":
48     register()
49
50     # test call
51     bpy.ops.object.modal_operator('INVOKE_DEFAULT')