I'm listening to the right click in my model operator. I wish to get its 3D coordinates in the 3D_view. I've tried the following code from here:
from bpy_extras.view3d_utils import region_2d_to_vector_3d, region_2d_to_location_3d
def mouse_coo_to_3d_loc(event, context):
coord = (event.mouse_region_x, event.mouse_region_y)
region = context.region
rv3d = context.space_data.region_3d
vec = region_2d_to_vector_3d(region, rv3d, coord)
return region_2d_to_location_3d(region, rv3d, coord, vec)
But I got different coordinates from bpy.context.scene.cursor_location. I thought that maybe the context is wrong, so I used this code instead:
def mouse_coo_to_3d_loc(event):
area, region = get_3d_area_region()
coord = (event.mouse_x - area.x, event.mouse_y - area.y)
rv3d = area.spaces.active.region_3d
vec = region_2d_to_vector_3d(region, rv3d, coord)
return region_2d_to_location_3d(region, rv3d, coord, vec)
def get_3d_area_region():
for screen in bpy.data.screens:
for area in screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
return area, region
But still wrong results. Am I m missing something here?
**** EDIT ****
I should have mentioned that my addon is running in the graph_editor.
get_3d_area_region()above? will always return theVIEW_3Darea ofbpy.data.screens[0].. (or first with 3d view, eg "3D Full VIew" screen in default file.) – batFINGER Nov 08 '17 at 03:26