I want to detect if the mouse is in certain area of the 3d View, so I can display a custom menu when it will be in the left top corner, over the view name ("User Persp" etc).

  • 1
    $\begingroup$ Take a look at the python templates, some of them use the mouse position, such as the Modal Draw template that draws the path of mouse: i.imgur.com/55rYdAc.png $\endgroup$
    – Greg Zaal
    Commented Jun 24, 2014 at 14:31

2 Answers 2


Here is a script from the blender wiki.

Go to the Invoke versus execute section for finding this code:

# File invoke.py
# from API documentation
import bpy
class SimpleMouseOperator(bpy.types.Operator):
    """ This operator shows the mouse location,
        this string is used for the tooltip and API docs
    bl_idname = "wm.mouse_position"
    bl_label = "Mouse location"
    x: bpy.props.IntProperty()
    y: bpy.props.IntProperty()
    def execute(self, context):
        # rather then printing, use the report function,
        # this way the message appears in the header,
        self.report({'INFO'}, "Mouse coords are %d %d" % (self.x, self.y))
        return {'FINISHED'}
    def invoke(self, context, event):
        self.x = event.mouse_x
        self.y = event.mouse_y
        return self.execute(context)
#    Panel in tools region
class MousePanel(bpy.types.Panel):
    bl_label = "Mouse"
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOL_PROPS"
    def draw(self, context):
#   Registration
#   Not really necessary to register the class, because this happens
#   automatically when the module is registered. OTOH, it does not hurt either.
# Automatically display mouse position on startup
# Another test call, this time call execute() directly with pre-defined settings.
#bpy.ops.wm.mouse_position('EXEC_DEFAULT', x=20, y=66)

Now if you want to know the position of your 3D view, I made a quick research. With this you can find the area of your 3D View:

import bpy

for area in bpy.context.screen.areas:
    if area.type=='VIEW_3D':
        X= area.x
        Y= area.y
  • $\begingroup$ Thanks, now I guess I need to get the position and size of the 3d View, to determine if the mouse is in the specified location or not. The sidebar also adds trouble. $\endgroup$ Commented Jun 24, 2014 at 13:47
  • $\begingroup$ I've edited my answer. At the end you will find how to determine the size and position of the 3D View. $\endgroup$
    – lucblender
    Commented Jun 24, 2014 at 13:54
  • $\begingroup$ That's great, works, thanks! Any way to determine the sidebar current width? If I can get that, I will be able to do what I want. $\endgroup$ Commented Jun 24, 2014 at 14:15
  • $\begingroup$ I don't know how to determinate that. Ask it at an other question, you will find better help :) $\endgroup$
    – lucblender
    Commented Jun 24, 2014 at 14:17

You get the area and the region from the context argument (passed to the invoke / execute / poll method of the operator).

Use the operator's poll method to check for the right context. I.e:

def poll(cls, context):
    return context.area.type   == 'VIEW_3D' and \
           context.region.type == 'WINDOW'

Just want to add:

  • $\begingroup$ Do you mean poll for the operator or the panel? $\endgroup$
    – yarun can
    Commented Jan 10, 2019 at 4:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .