3
$\begingroup$

In the clip editor, I need to find out the position of the mouse relative to the current movie clip. I know I can get the mouse position with event.mouse_x and event.mouse_y in my modal operator. I can also use context.region.view2d.region_to_view to get the mouse position relative to the view.

However, none of these tell me where the mouse is relative to the movie clip. Ideally (0,0) would be one corner of the movie clip and (1,1) the opposite corner. How can I find out the mouse position?

$\endgroup$
1
$\begingroup$

If you run this script in Blender's text editor, you can press L with your mouse in the preview window to get the position of the mouse in pixels relative to the bottom left corner of the video clip.

import bpy

bl_info = {"name": "Mouse Pos", "category": "Sequencer"}

class GetMousePos(bpy.types.Operator):
    bl_idname = "mouse_pos.get_mouse_pos"
    bl_label = "Get Mouse Pos"

    def invoke(self, context, event):
        # Size of the preview region
        width = context.region.width
        height = context.region.height

        fac = 1.0

        prs = bpy.context.space_data.proxy_render_size
        res_perc = bpy.context.scene.render.resolution_percentage

        if prs == 'SCENE':
            fac = res_perc / 100

        rv1 = context.region.view2d.region_to_view(0, 0)
        rv2 = context.region.view2d.region_to_view(width, height)

        preview_zoom = (width / (rv2[0] - rv1[0]))

        # Resolution of the scene
        res_x = context.scene.render.resolution_x
        res_y = context.scene.render.resolution_y

        # Distance of video preview from bottom left corner
        offset_x = -int((rv1[0] + ((res_x * fac) / 2)) * preview_zoom)
        offset_y = -int((rv1[1] + ((res_y * fac) / 2)) * preview_zoom)

        print(event.mouse_region_x - offset_x, event.mouse_region_y - offset_y)

        return {'FINISHED'}

def register():
    bpy.utils.register_class(GetMousePos)

    keyconfig = bpy.context.window_manager.keyconfigs['Blender Addon']
    km = keyconfig.keymaps["SequencerPreview"]

    kmi = km.keymap_items.new("mouse_pos.get_mouse_pos", 'L', 'PRESS')

def unregister():
    bpy.utils.unregister_class(GetMousePos)

    keyconfig = bpy.context.window_manager.keyconfigs['Blender Addon']
    km = keyconfig.keymaps["SequencerPreview"]
    for kmi in km.keymap_items:
        if kmi.idname == "mouse_pos.get_mouse_pos":
            km.keymap_items.remove(kmi)

    bpy.utils.unregister_module(__name__)

register()
$\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.