Home

If you're new to Python
and VPython: Introduction

A VPython tutorial

Pictures of 3D objects

What's new in VPython 6

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

 

This is documentation for Classic VPython (VPython 6), which continues to be available but is no longer supported. See vpython.org for information on installing VPython 7 or using GlowScript VPython. Documentation is available at glowscript.org by clicking Help.

Additional Attributes

The following attributes apply to all VPython objects:

visible If False, object is not displayed; e.g. ball.visible = False
Use ball.visible = True to make the ball visible again.

frame Place this object into a specified frame, as in ball = sphere(frame = f1)

display When you start a VPython program, for convenience VPython creates a display window and names it scene. By default, objects you create go into that display window. You can choose to put an object in a different display like this:

make_trail You can specify that a trail be left behind a moving arrow, box, cone, cylinder, ellipsoid, pyramid, ring, or sphere object. For details, see Leaving a Trail.

scene2 = display( title = "Act IV, Scene 2" )
rod = cylinder( display = scene2 )

Executing myscene = display.get_selected() returns a reference to the display in which objects are currently being created. Given a specific display named scene2, scene2.select() makes scene2 be the "selected display", so that objects will be drawn into scene2 by default.

There is a rotate function for all objects other than the "array objects" curve, convex, extrusion, faces, text, and points (which can be put into a frame and the frame rotated).

__class__ Name of the class of object. For example, ball.__class__ is sphere is true if ball is a sphere object. There are two underscores before and after the word class. In a list of visible objects provided by scene.objects, if obj is in this list you can determine the class of the object with obj.__class__. You can check for a specific kind of object by using a standard Python function: isinstance(obj, sphere) is true if "obj" is a sphere object.

__copy__() Makes a copy of an object. There are two underscores before and after copy. Without any arguments, this results in creating a second object in the exact same position as the first, which is probably not what you want. The __copy__() function takes a list of keyword=value argument pairs which are applied to the new object before making it visible. For example, to clone an object from one display to another, you would execute: new_object = old_object.__copy__( display=new_display). Restriction: If the original object is within a frame, and the new object is on a different display, you must supply both a new display and a new frame for the new object (the new frame may be None). This is due to the restriction that an object may not be located within a frame that is in a separate display.

Here is an example that uses the __copy__() function. The following routine copies all of the VPython objects currently existing in one display into a previously defined second display, as long as there are no nested frames (frames within frames):

def clone_universe( new_display, old_display):
    # Create a dictionary of frames in old display to
    # the corresponding frames in the new display.
    frames = {} # create empty dictionary
    for obj in old_display.objects:
        if obj.__class__ == frame:
            frames[obj] = obj.__copy__( frame=None,
                display=new_display)
    # For each old frame within another reference frame,
    # place the new frame in appropriate frame in new
    # display. Here old is an object and new is its
    # frame in the new display.
    for old, new in frames.iteritems():
        if old.frame:
            new.frame = frames[old.frame]
    # Copy over the universe.
    for obj in old_display.objects:
        if obj.__class__ == frame:
            # Already taken care of above.
            pass
        elif obj.frame:
            # Initialize with the corresponding frame
            # in the new display:
            obj.__copy__( display=new_display,
                   frame=frames[obj.frame])
        else:
            # No frame issue;
            obj.__copy__( display=new_display)

See Controlling One or More VPython Display Windows for more information on creating and manipulating display objects.