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$
1
  • $\begingroup$ See the Text Editor > Templates > Python > Operator Modal Draw template. $\endgroup$
    – batFINGER
    Dec 15, 2018 at 14:24

1 Answer 1

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$
1
  • $\begingroup$ What context do I need to obtain 'context.space_data.region_3d'. My console output: AttributeError: 'NoneType' object has no attribute 'region_3d' $\endgroup$
    – james_t
    May 1, 2021 at 20:10

You must log in to answer this question.

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