2011-02-18 09:47:37 +01:00
|
|
|
"""
|
2022-09-01 15:01:51 +02:00
|
|
|
.. _modal_operator:
|
|
|
|
|
2011-02-18 09:47:37 +01:00
|
|
|
Modal Execution
|
|
|
|
+++++++++++++++
|
2015-09-08 06:30:05 +02:00
|
|
|
|
2017-06-02 03:59:51 +02:00
|
|
|
This operator defines a :class:`Operator.modal` function that will keep being
|
|
|
|
run to handle events until it returns ``{'FINISHED'}`` or ``{'CANCELLED'}``.
|
2011-02-18 09:47:37 +01:00
|
|
|
|
2017-06-02 03:59:51 +02:00
|
|
|
Modal operators run every time a new event is detected, such as a mouse click
|
|
|
|
or key press. Conversely, when no new events are detected, the modal operator
|
|
|
|
will not run. Modal operators are especially useful for interactive tools, an
|
|
|
|
operator can have its own state where keys toggle options as the operator runs.
|
|
|
|
Grab, Rotate, Scale, and Fly-Mode are examples of modal operators.
|
2011-02-18 09:47:37 +01:00
|
|
|
|
2019-06-21 01:50:23 +02:00
|
|
|
:class:`Operator.invoke` is used to initialize the operator as being active
|
|
|
|
by returning ``{'RUNNING_MODAL'}``, initializing the modal loop.
|
2011-02-18 09:47:37 +01:00
|
|
|
|
2015-09-08 06:30:05 +02:00
|
|
|
Notice ``__init__()`` and ``__del__()`` are declared.
|
2011-02-18 09:47:37 +01:00
|
|
|
For other operator types they are not useful but for modal operators they will
|
|
|
|
be called before the :class:`Operator.invoke` and after the operator finishes.
|
|
|
|
"""
|
|
|
|
import bpy
|
|
|
|
|
|
|
|
|
|
|
|
class ModalOperator(bpy.types.Operator):
|
|
|
|
bl_idname = "object.modal_operator"
|
|
|
|
bl_label = "Simple Modal Operator"
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
print("Start")
|
2011-02-25 17:19:50 +01:00
|
|
|
|
2011-02-18 09:47:37 +01:00
|
|
|
def __del__(self):
|
|
|
|
print("End")
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
context.object.location.x = self.value / 100.0
|
2012-01-05 06:43:35 +01:00
|
|
|
return {'FINISHED'}
|
2011-02-18 09:47:37 +01:00
|
|
|
|
|
|
|
def modal(self, context, event):
|
2011-02-25 17:19:50 +01:00
|
|
|
if event.type == 'MOUSEMOVE': # Apply
|
2011-02-18 09:47:37 +01:00
|
|
|
self.value = event.mouse_x
|
|
|
|
self.execute(context)
|
|
|
|
elif event.type == 'LEFTMOUSE': # Confirm
|
|
|
|
return {'FINISHED'}
|
2014-09-18 09:45:31 +02:00
|
|
|
elif event.type in {'RIGHTMOUSE', 'ESC'}: # Cancel
|
2022-12-26 13:42:01 +01:00
|
|
|
# Revert all changes that have been made
|
2012-10-12 20:19:39 +02:00
|
|
|
context.object.location.x = self.init_loc_x
|
2011-02-18 09:47:37 +01:00
|
|
|
return {'CANCELLED'}
|
|
|
|
|
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
|
|
|
|
def invoke(self, context, event):
|
2012-10-12 20:19:39 +02:00
|
|
|
self.init_loc_x = context.object.location.x
|
2011-02-18 09:47:37 +01:00
|
|
|
self.value = event.mouse_x
|
|
|
|
self.execute(context)
|
|
|
|
|
2012-10-12 20:19:39 +02:00
|
|
|
context.window_manager.modal_handler_add(self)
|
2011-02-18 09:47:37 +01:00
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
|
2022-04-20 07:10:14 +02:00
|
|
|
|
|
|
|
# Only needed if you want to add into a dynamic menu.
|
2021-11-16 10:45:23 +01:00
|
|
|
def menu_func(self, context):
|
|
|
|
self.layout.operator(ModalOperator.bl_idname, text="Modal Operator")
|
2011-02-18 09:47:37 +01:00
|
|
|
|
2022-04-20 07:10:14 +02:00
|
|
|
|
|
|
|
# Register and add to the object menu (required to also use F3 search "Modal Operator" for quick access).
|
2011-02-18 09:47:37 +01:00
|
|
|
bpy.utils.register_class(ModalOperator)
|
2021-11-16 10:45:23 +01:00
|
|
|
bpy.types.VIEW3D_MT_object.append(menu_func)
|
2011-02-18 09:47:37 +01:00
|
|
|
|
|
|
|
# test call
|
|
|
|
bpy.ops.object.modal_operator('INVOKE_DEFAULT')
|