Mouse Event

i’m new to Python and scripting with Python in Blender. I want to create a simple script which adds a cube to the scene when i leftclick. Adding a cube to the scene is very simple and no problem for me, but how can i trigger it with a mouse event?
I don’t need a finished source for my problem, but some hints would be great.

With regards,


Left click in the 3d viewport moves the 3D cursor. Here is a simple play on the modal timer op that utilises this to add a cube.

import bpy
from mathutils import Vector

class ModalTimerOperator(bpy.types.Operator):
    """Operator which runs its self from a timer"""
    bl_idname = "wm.modal_timer_operator"
    bl_label = "Modal Timer Operator"

    _timer = None
    _loc = Vector((0,0,0))

    def modal(self, context, event):
        if event.type == 'ESC':
            return self.cancel(context)

        if event.type == 'TIMER':
            if context.scene.cursor_location != self._loc:
                # move cursor to far away 
                self._loc = context.scene.cursor_location.copy()

        return {'PASS_THROUGH'}

    def execute(self, context):
        self._timer = context.window_manager.event_timer_add(0.1, context.window)
        ModalTimerOperator._loc = context.scene.cursor_location.copy()
        return {'RUNNING_MODAL'}

    def cancel(self, context):
        return {'CANCELLED'}

def register():

def unregister():

if __name__ == "__main__":

    # test call

Wow, i thought my problem was easy to solve…
But this script works great and is exactly what i need, now i try to understand how this script works:).
Thank you!

you can also bind the cube-add operator to left mouse:

Double-click and Release work, no scripting required. Don’t forget to save the config if you wanna keep it (but make sure you don’t overwrite your startup.blend by accident)