Mouse events in VPython

VPython (Visual Python) is a 3D graphics library that allows us to create and visualize three-dimensional objects on the screen. It is primarily used to visualize the impact of physics equations on the objects' motion.

Note: Read more about VPython.

Mouse events

VPython provides two mouse events that allow us to interact with objects in the 3D scene:

Click Event

Usually, in VPython, the left click event is set to rotate the object keeping the position same. We can apply the necessary changes to the right-click event by using the bind() method as follows:

scene.bind('click', function)
Syntax for click event

where the 'click' event corresponds to right-clicking on an object in the 3D scene and the function is our defined function, which includes the changes we want on the right-click event. This function will be executed automatically whenever we right-click on the object.

Drag Event

The drag event includes moving an object on the screen by clicking and holding the mouse button while moving the pointer. The dragged object follows the mouse cursor's movement until we release the mouse button.

VPython drag event typically involves the following three main phases:

  1. MouseDown: The drag action begins when the user clicks on the object, indicating the intention to start dragging.

  2. MouseMove: While holding the mouse button down, the user moves the mouse cursor, causing the object to follow the mouse's movement. This phase is responsible for updating the position or other properties of the object in real time.

  3. MouseUp: The drag action concludes when the user releases the mouse button, signifying the end of the dragging.

Syntaxes for the dragging events are as follows:

scene.bind('mousedown', function1)
scene.bind('mouseup', function2)
scene.bind('mousemove', function3)
Syntax for the drag events

where 'mousedown', 'mousemove', and 'mouseup' are the events for holding the click, dragging, and dropping the click, respectively. function1, function2, and function3 are the functions we want to execute on the previously mentioned events.

Example code

To execute the above below and view its output, we have to follow the following process:

Once you click the "Run" button, follow the encircled link.
1 of 4

The following code explains the events mentioned above:

from vpython import *

# Create a 3D scene
scene = canvas()

# Create a sphere object
s = sphere(pos=vector(0, 0, 0), radius=1, color=color.red)

# Track the state of dragging
is_dragging = False

def change():
    if s.color.equals(color.green):
        s.color = color.red
    else:
        s.color = color.green

def on_mousedown(event):
    global is_dragging
    is_dragging = True

def on_mouseup(event):
    global is_dragging
    is_dragging = False

def on_mousemove(event):
    global is_dragging
    if is_dragging:
        s.pos = event.pos

scene.bind('click', change)
scene.bind('mousedown', on_mousedown)
scene.bind('mouseup', on_mouseup)
scene.bind('mousemove', on_mousemove)

# Run the VPython animation loop
while True:
    rate(30)
Code for VPython mouse events

Code explanation

  • Line 1: Importing VPython library.

  • Line 4: Using canvas() method to create a scene.

  • Line 7: Creating a red-colored sphere object.

  • Line 10: Global boolean variable is_dragging which is False at the moment. It will become True once we click on the object and False once we drop the click.

  • Lines 12–16: The function change() changes the object's color to red if it's green and vice versa.

  • Lines 18–20: The function on_mousedown() makes the is_dragging variable True, because this function is called when the dragging event starts.

  • Lines 22–24: The function on_mouseup() makes the is_dragging variable False, because this function gets called when the drag event stops.

  • Lines 26–29: The function on_mousemove() changes the object's position according to the cursor's position.

  • Lines 37–38: Sets the frames per second for the animation.

Note: The mouse scroll ball is set to zoom in or out the scene.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved