2
$\begingroup$

In the context of a modal operator, I would like to perform an operation when the left mouse button is clicked in the VIEW_3D area, WINDOW region. (similar to setting the cursor location with a mouse left click, assuming 'right click select' is enabled)

So far I have the following code:

import bpy
from mathutils import Vector

def mouse_in_view3d_window(event):
    regions = dict()
    for region in bpy.context.area.regions:
        regions[region.type] = region
    mouse_pos = Vector((event.mouse_x, event.mouse_y))
    window_dimensions = Vector((
        regions["WINDOW"].width - regions["UI"].width,
        regions["WINDOW"].height - regions["HEADER"].height
    ))
    return (
        regions["TOOLS"].width < mouse_pos.x < window_dimensions.x and
        mouse_pos.y < window_dimensions.y
    )

class SCENE_OT_set_cursor_in_view3d_window(bpy.types.Operator):
    """ Test operator for 'mouse_in_view3d_window' function """
    bl_idname = "scene.set_cursor_in_view3d_window"
    bl_label = "Set Cursor"
    bl_options = {"REGISTER", "UNDO"}

    def modal(self, context, event):
        if bpy.context.area is None:
            return {"PASS_THROUGH"}
        if mouse_in_view3d_window(event):
            bpy.context.window.cursor_set("PAINT_BRUSH")
        else:
            bpy.context.window.cursor_set("DEFAULT")

        return {"PASS_THROUGH"}

    def execute(self, context):
        # run modal operator
        context.window_manager.modal_handler_add(self)
        return {"RUNNING_MODAL"}

def register():
    bpy.utils.register_class(SCENE_OT_set_cursor_in_view3d_window)

if __name__ == "__main__":
    register()

This gets me close, but still returns False when the mouse is in the dead space below the UI and Tools regions, as well as the dead space between header buttons:

3D View regions ignored

Is there a way to improve my mouse_in_view3d_window function to account for these additional areas? Blender seems to accomplish this internally, as mouse clicks in these areas are passed through to the viewport (e.g. clicking in 3D space to set the cursor location).

Thanks for your help!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.