From top view(orthographic), I'd like to be able to click in the 3D view and move an object to where I clicked. I know I have to do this in a modal operator, what I need to know is how do I convert the mouse position coordinates into xy coordinates in the 3D view?


1 Answer 1


Can do this using bpy.extras.region_2d_to_location_3d

Here is a sample script using the Modal Draw Template script. As well as drawing a line, it moves the context object to the mouse cursor. The depth is set to (0, 0, 0)

import bpy
import bgl
import blf
import gpu
from gpu_extras.batch import batch_for_shader
from bpy_extras.view3d_utils import region_2d_to_location_3d

def draw_callback_px(self, context):
    print("mouse points", len(self.mouse_path))

    font_id = 0  # XXX, need to find out how best to get this.

    # draw some text
    blf.position(font_id, 15, 30, 0)
    blf.size(font_id, 20, 72)
    blf.draw(font_id, "Hello Word " + str(len(self.mouse_path)))

    # 50% alpha, 2 pixel width line
    shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
    batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": self.mouse_path})
    shader.uniform_float("color", (0.0, 0.0, 0.0, 0.5))

    # restore opengl defaults

class ModalDrawOperator(bpy.types.Operator):
    """Draw a line with the mouse"""
    bl_idname = "view3d.modal_operator"
    bl_label = "Simple Modal View3D Operator"

    def modal(self, context, event):

        if event.type == 'MOUSEMOVE':
            x, y = event.mouse_region_x, event.mouse_region_y
            self.mouse_path.append((x, y))
            obj = context.object
            if obj:
                loc = region_2d_to_location_3d(context.region, context.space_data.region_3d, (x, y), (0, 0, 0))
                obj.location = loc

        elif event.type == 'LEFTMOUSE':
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
            return {'FINISHED'}

        elif event.type in {'RIGHTMOUSE', 'ESC'}:
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
            return {'CANCELLED'}

        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        if context.area.type == 'VIEW_3D':
            # the arguments we pass the the callback
            args = (self, context)
            # Add the region OpenGL drawing callback
            # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
            self._handle = bpy.types.SpaceView3D.draw_handler_add(draw_callback_px, args, 'WINDOW', 'POST_PIXEL')

            self.mouse_path = []

            return {'RUNNING_MODAL'}
            self.report({'WARNING'}, "View3D not found, cannot run operator")
            return {'CANCELLED'}

def register():

def unregister():

if __name__ == "__main__":
  • $\begingroup$ Sorry for my ignorance, but how do you actually run this? Does this create a UI item? Or do you need to call the Python file in block logic? Thanks :) $\endgroup$ Jun 12, 2017 at 20:14
  • 1
    $\begingroup$ Press the space bar in the 3d view, then search for "Simple Modal View3d Operator". $\endgroup$
    – batFINGER
    Jun 13, 2017 at 9:29

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.