Hello from the future,
If we have us some properties on our operator, like so:
from bpy.types import Operator;
from bpy.props import IntProperty;
class MYADDON_OT_my_operator(Operator):
bl_idname = "myaddon.myoperator";
bl_label = "Pop!";
bl_description = "Spawns a pop-up";
# using annotations for 2.8+
# change to equal signs on previous versions
first_mouse_x : IntProperty(default = 0);
first_mouse_y : IntProperty(default = 0);
target_mouse_x : IntProperty(default = 0);
target_mouse_y : IntProperty(default = 0);
mouse_snap : BoolProperty(default = False);
Then we can save the original position of the mouse during invoke
.
def invoke(self, context, event):
# assign position of the cursor at the time the operator is called
self.first_mouse_x = event.mouse_x;
self.first_mouse_y = event.mouse_y;
# probably redundant but i can't recall if props actually reset to default
self.mouse_snap = False;
# now warp the mouse to some xy coords
# here i center on the window and adjust y to approx. the height of my popup
context.window.cursor_warp(context.window.width/2, (context.window.height/2) + 60);
# now invoke
wm = context.window_manager;
return wm.invoke_props_dialog(self, width = 600);
Next thing on the list is resetting the cursor to the original coordinates. I do this on the first run of draw
and haven't really noticed any jumpy motion on the cursor.
def draw(self, context):
if not self.mouse_snap:
self.mouse_snap = True;
context.window.cursor_warp(self.first_mouse_x, self.first_mouse_y);
That gets you back where you started.
But wait, there's more! As a bonus, you can also pass the xy coords to the operator from your panel's draw
.
def draw(self, context):
layout = self.layout;
row = layout.row();
# this gets you a handle to the operator properties
props = row.operator("myaddon.myoperator");
# now just assign them values as you would
props.target_mouse_x = some_x_coord;
props.target_mouse_y = some_y_coord;
It's a barebones example, but hopefully a good starting point for anyone else that google-wanders in here like I did. ;)
bpy.context.window.cursor_warp(x, y)
. $\endgroup$