2
from bpy.props import IntProperty, FloatProperty
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"
10
first_mouse_x = IntProperty()
11
first_value = FloatProperty()
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
18
elif event.type == 'LEFTMOUSE':
21
elif event.type in {'RIGHTMOUSE', 'ESC'}:
22
context.object.location.x = self.first_value
25
return {'RUNNING_MODAL'}
27
def invoke(self, context, event):
29
self.first_mouse_x = event.mouse_x
30
self.first_value = context.object.location.x
32
context.window_manager.modal_handler_add(self)
33
return {'RUNNING_MODAL'}
35
self.report({'WARNING'}, "No active object, could not finish")
40
bpy.utils.register_class(ModalOperator)
44
bpy.utils.unregister_class(ModalOperator)
47
if __name__ == "__main__":
51
bpy.ops.object.modal_operator('INVOKE_DEFAULT')