blender/release/scripts/templates/operator_modal.py

39 lines
1.2 KiB
Python
Raw Normal View History

2010-07-08 16:24:24 +00:00
import bpy
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-21 16:20:32 +00:00
first_mouse_x = IntProperty()
first_value = FloatProperty()
def modal(self, context, event):
if event.type == 'MOUSEMOVE':
delta = self.first_mouse_x - event.mouse_x
context.object.location.x = self.first_value + delta * 0.01
2010-02-21 16:20:32 +00:00
elif event.type == 'LEFTMOUSE':
return {'FINISHED'}
elif event.type in ('RIGHTMOUSE', 'ESC'):
context.object.location.x = self.first_value
2010-02-21 16:20:32 +00:00
return {'CANCELLED'}
2010-02-21 16:20:32 +00:00
return {'RUNNING_MODAL'}
def invoke(self, context, event):
if context.object:
context.window_manager.add_modal_handler(self)
self.first_mouse_x = event.mouse_x
self.first_value = context.object.location.x
2010-02-21 16:20:32 +00:00
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "No active object, could not finish")
return {'CANCELLED'}
if __name__ == "__main__":
bpy.ops.object.modal_operator()