# How to get mouse position in a window with Python?

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

• 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 Jun 24 '14 at 14:31

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):
self.layout.operator("wm.mouse_position")

#
#   Registration
#   Not really necessary to register the class, because this happens
#   automatically when the module is registered. OTOH, it does not hurt either.
bpy.utils.register_class(SimpleMouseOperator)
bpy.utils.register_module(__name__)

# Automatically display mouse position on startup
bpy.ops.wm.mouse_position('INVOKE_DEFAULT')

# 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
WIDTH=area.width
HEIGHT=area.height

print(X,Y,WIDTH,HEIGHT)

• 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. Jun 24 '14 at 13:47
• I've edited my answer. At the end you will find how to determine the size and position of the 3D View. Jun 24 '14 at 13:54
• 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. Jun 24 '14 at 14:15
• I don't know how to determinate that. Ask it at an other question, you will find better help :) Jun 24 '14 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:

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


Just want to add:

• Do you mean poll for the operator or the panel? Jan 10 '19 at 4:59