Get mouse position

Hello, I want to get the position of the mouse, but I don’t want to get the mouse in the ‘event’ of ‘bpy. Types. Operator’. I want to get the mouse without entering the mode. Is there any better way

ps:你好,我想获取鼠标的位置,但是不想在 'bpy.types.Operator’的’event’里面,我想不进入模态的情况下获取鼠标,请问有什么好一些的方法吗

1 Like

What exactly do you want to do with this? What’s the bigger picture?

as far as I’m aware it’s not possible to get the mouse position without an event. knowing that it’s just python you’re running on- you can use pretty much any method you like- you’ll just have to convert the screen coordinates into relevant data you can use in Blender (relative to region coordinates, etc).

@sybren I’m not sure what OP is trying to do, but one example I’ve used this for in the past is knowing what the precise mouse position was at the time a menu was invoked (by hijacking the poll function), so operators can raycast from the ‘position of intent’ rather than wherever the menu button happened to appear on the screen.

Sorry, I didn’t make it clear. I want to draw the grid in the 3D view and obtain the mouse position to locate the mouse position in the 3D view. Just like the tool ‘add Cube’, I can draw the grid in the mouse position, but it doesn’t enter the mode

ps:抱歉是我没表达清楚,我是想在3D视图绘制网格,获取鼠标位置,来定位鼠标在3D视图的位置,就像 ‘Add Cube’ 这个工具一样,可以在鼠标的位置绘制网格,但是不进入模态

I tried to use the external library ‘pyautogui’ to get the position of the mouse in the window, but the obtained value is based on the screen, not on the 3D view


This is done using ray casting
See this example

For what you want to do
i’d suggest taking a look at grid modeller source code

Thank you. I’ll study how to achieve it