2010-02-21 16:20:32 +00:00
|
|
|
from bpy.props import *
|
|
|
|
|
|
|
|
class ModalOperator(bpy.types.Operator):
|
|
|
|
'''Move an object with the mouse, example.'''
|
|
|
|
bl_idname = "object.modal_operator"
|
|
|
|
bl_label = "Simple Modal Operator"
|
2010-02-22 23:32:58 +00:00
|
|
|
|
2010-02-21 16:20:32 +00:00
|
|
|
first_mouse_x = IntProperty()
|
|
|
|
first_value = FloatProperty()
|
|
|
|
|
|
|
|
def modal(self, context, event):
|
|
|
|
if event.type == 'MOUSEMOVE':
|
|
|
|
delta = self.properties.first_mouse_x - event.mouse_x
|
|
|
|
context.object.location.x = self.properties.first_value + delta * 0.01
|
|
|
|
|
|
|
|
elif event.type == 'LEFTMOUSE':
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
2010-02-24 15:56:27 +00:00
|
|
|
elif event.type in ('RIGHTMOUSE', 'ESC'):
|
2010-02-21 16:20:32 +00:00
|
|
|
context.object.location.x = self.properties.first_value
|
|
|
|
return {'CANCELLED'}
|
2010-02-22 23:32:58 +00:00
|
|
|
|
2010-02-21 16:20:32 +00:00
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
|
|
|
|
def invoke(self, context, event):
|
|
|
|
if context.object:
|
|
|
|
context.manager.add_modal_handler(self)
|
|
|
|
self.properties.first_mouse_x = event.mouse_x
|
|
|
|
self.properties.first_value = context.object.location.x
|
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
else:
|
|
|
|
self.report({'WARNING'}, "No active object, could not finish")
|
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
|
|
|
|
bpy.types.register(ModalOperator)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2010-02-22 23:32:58 +00:00
|
|
|
bpy.ops.object.modal_operator()
|