2
$\begingroup$

I have a question on getting the mouse coordinates (in 3d space) when I click in the 3d view.

I have a draw handler in a modal operator (POST_VIEW) and I want to get the mouse cursor x, y vertex and the 3rd vertex should be on the current viewplane.

Do you have a Python example?

$\endgroup$
  • $\begingroup$ See the Text Editor > Templates > Python > Operator Modal Draw template. $\endgroup$ – batFINGER Dec 15 '18 at 14:24
1
$\begingroup$

bpy_extras.view3d_utils.region_2d_to_location_3d

Return a 3d location from the region relative 2d coords, aligned with depth_location.

Here is the code:

from bpy_extras.view3d_utils import region_2d_to_location_3d
...
x, y = event.mouse_region_x, event.mouse_region_y
region = context.region
rv3d = context.space_data.region_3d

# this is the vector
loc = region_2d_to_location_3d(region, rv3d, (x, y), (0, 0, 0))

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.