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):

        return {"PASS_THROUGH"}

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

def register():

if __name__ == "__main__":

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!


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.