Drag example

You can program dragging with the mouse simply by continually reading the current value of scene.mouse.pos. Here is a complete routine for dragging a sphere with the left button down. Note the statements for making the cursor invisible during the drag.

scene.range = 10 # fixed size, no autoscaling

ball = sphere(pos=(-5,0,0), radius=1., color=color.cyan)

cube = box(pos=(+5,0,0), size=(2,2,2), color=color.red)

pick = None # no object picked out of the scene yet

while 1:

    if scene.mouse.events:

        m1 = scene.mouse.getevent() # obtain drag or drop event

        if m1.drag and m1.pick == ball:

            drag_pos = m1.pickpos

            pick = m1.pick

            scene.cursor.visible = 0 # make cursor invisible

        elif m1.drop:

            pick = None # end dragging

            scene.cursor.visible = 1 # cursor visible

    if pick:

        new_pos = scene.mouse.project(normal=(0,0,1))

        if new_pos != drag_pos:

            pick.pos += new_pos - drag_pos

            drag_pos = new_pos

If you do a lot of processing of each mouse movement, or you are leaving a trail behind the moving object, you may need to check whether the "new" mouse position is in fact different from the previous position before processing the "move", as is done in the example above. For example, a trail drawn with a curve object that contains a huge number of points all at the same location may not display properly.

Choose Back, or Go to top of mouse documentation