blender/scripts/templates_py/operator_modal.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

59 lines
1.7 KiB
Python
Raw Normal View History

2010-07-08 16:24:24 +00:00
import bpy
from bpy.props import IntProperty, FloatProperty
2010-02-21 16:20:32 +00:00
2010-02-21 16:20:32 +00:00
class ModalOperator(bpy.types.Operator):
"""Move an object with the mouse, example"""
2010-02-21 16:20:32 +00:00
bl_idname = "object.modal_operator"
bl_label = "Simple Modal Operator"
first_mouse_x: IntProperty()
first_value: FloatProperty()
2010-02-21 16:20:32 +00:00
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:
self.first_mouse_x = event.mouse_x
self.first_value = context.object.location.x
context.window_manager.modal_handler_add(self)
2010-02-21 16:20:32 +00:00
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "No active object, could not finish")
return {'CANCELLED'}
def menu_func(self, context):
self.layout.operator(ModalOperator.bl_idname, text=ModalOperator.bl_label)
2010-02-21 16:20:32 +00:00
# Register and add to the "view" menu (required to also use F3 search "Simple Modal Operator" for quick access).
def register():
bpy.utils.register_class(ModalOperator)
bpy.types.VIEW3D_MT_object.append(menu_func)
def unregister():
bpy.utils.unregister_class(ModalOperator)
bpy.types.VIEW3D_MT_object.remove(menu_func)
2010-02-21 16:20:32 +00:00
if __name__ == "__main__":
register()
# test call
2011-08-10 09:16:35 +00:00
bpy.ops.object.modal_operator('INVOKE_DEFAULT')