Home

If you're new to Python
and VPython see more
Help at glowscript.org

Pictures of 3D objects

 

Click Example

This program displays a box, then repeatedly waits for a mouse left click and displays a cyan sphere at the mouse location. 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.

scene.range = 4
box() # display a box for context

def showSphere(evt):
    loc = evt.pos
    sphere(pos=loc, radius=0.2, color=color.cyan)

scene.bind('click', showSphere)

Copy this program into an edit 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 = evt.project(normal=vector(0,0,1))
    # loc is None if no intersection with plane
    if loc:
        sphere(pos=loc,radius=0.2,color=color.cyan)