Let say we want to trigger something after we finish drag a Select Box. How?
Each approach i try will trigger only after i stop to drag select box, release the mouse and make a new single click anywhere.
One of the code examples.
import bpy
class LMBListenerOperator(bpy.types.Operator):
"""Operator to listen for Left Mouse Button events in 3D View, including during dragging."""
bl_idname = "view3d.lmb_listener_operator"
bl_label = "LMB Listener Operator"
is_lmb_pressed = False # Class variable to track LMB state
is_dragging = False # Track if the LMB is in drag state
def modal(self, context, event):
# Check for Left Mouse Button (LMB) events
if event.type == 'LEFTMOUSE':
if event.value == 'PRESS':
# LMB Pressed
if not self.is_lmb_pressed:
self.is_lmb_pressed = True
self.report({'INFO'}, "LMB Pressed")
print("LMB Pressed")
# Start tracking drag if not already dragging
if not self.is_dragging:
self.is_dragging = True
self.report({'INFO'}, "Started Dragging")
print("Started Dragging")
elif event.value == 'RELEASE':
# LMB Released
if self.is_lmb_pressed:
self.is_lmb_pressed = False
self.is_dragging = False # End dragging
self.report({'INFO'}, "LMB Released")
print("LMB Released")
# Detect if dragging continues (mouse move during press)
if self.is_dragging and event.type == 'MOUSEMOVE':
# Here you can add any additional handling for the drag if needed
pass
# Exit the modal operator on ESC key press
if event.type == 'ESC':
return {'CANCELLED'}
return {'PASS_THROUGH'} # Allow other operators to function normally
def execute(self, context):
# Start the modal operator
self.report({'INFO'}, "LMB Listener Started")
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def menu_func(self, context):
self.layout.operator(LMBListenerOperator.bl_idname)
def register():
bpy.utils.register_class(LMBListenerOperator)
bpy.types.VIEW3D_MT_view.append(menu_func)
def unregister():
bpy.utils.unregister_class(LMBListenerOperator)
bpy.types.VIEW3D_MT_view.remove(menu_func)
if __name__ == "__main__":
register()