Home

If you're new to Python
and VPython: Introduction

A VPython tutorial

Pictures of 3D objects

Choose a 3D object:

Work with 3D objects:

Windows, Events, & Files:

Vector operations

Graphs

factorial/combin

What's new in Visual 5

VPython web site
Visual license
Python web site
Math module (sqrt etc.)
Numpy module (arrays)

# Click Example

This program displays a box (which automatically creates a window referred to as scene), then repeatedly waits for a mouse left click, prints the mouse position in the Python shell window, and displays a cyan sphere. A mouse click is defined as pressing and releasing the left mouse button with no motion of the mouse, so the sphere appears when you release the mouse button.

from visual import *
scene.range = 4
box() # display a box for context
while True:
if scene.mouse.clicked:
m = scene.mouse.getclick()
loc = m.pos
print loc
sphere(pos=loc, radius=0.2, color=color.cyan)

Copy this program into an IDLE window and run the program. Click outside the box and a cyan sphere appears where you click. If you click inside the box, nothing seems to happen. This is because the mouse click is in the xy plane, and the sphere is buried inside the box. If you rotate the scene and then click, you'll see that the spheres go into the new plane parallel to the screen and passing through scene.center. If you want all of the spheres to go into the xy plane, perpendicular to the z axis, change the latter part of the program like this:

loc = m.project(normal=(0,0,1))
# loc is None if no intersection with plane
if loc:
print loc
sphere(pos=loc, radius=0.2, color=color.cyan)

Here is general mouse documentation.