Overview of Python & VPython

  "Welcome to VPython" tutorial

  Introductory Videos

  Pictures of 3D objects




  VPython 7 web site
  VPython license


Studying this description of the cylinder object provides an overview of important aspects common to all of the VPython 3D objects, box, sphere, pyramid, etc. Additional details about how to orient an object using "axis" and "up" are found in the description of the box object.

This documentation explains how to use Web VPython. If you prefer to write programs in JavaScript, see this document.

cylinder Here is an example of how to make a cylinder, naming it "rod" for future reference:

rod = cylinder(pos=vector(0,2,1),         axis=vector(5,0,0), radius=1)

The center of one end of this cylinder is at x=0, y=2, and z=1. Its axis lies along the x axis, with length 5, so that the other end of the cylinder is at (5,2,1), as shown in the accompanying diagram.

You can modify the position of the cylinder after it has been created, which has the effect of moving it immediately to the new position:

rod.pos = vector(15,11,9)
rod.pos.x = 15 # only change pos.x

If you create an object such as a cylinder but without giving it a name such as rod, you can't refer to it later. This doesn't matter if you never intend to modify the object.

Since we didn't specify a color, the cylinder will be the current "foreground" color (see Controlling One or More VPython Canvases). The default foreground color is white. After creating the cylinder, you can change its color:

rod.color = vector(0,0,1) # make rod be blue

This will make the cylinder suddenly turn blue, using the so-called RGB system for specifying colors in terms of fractions of red, green, and blue. (For details on choosing colors, see Specifying Colors.) You can set individual amounts of red, green, and blue like this:

rod.red = 0.4
rod.green = 0.7
rod.blue = 0.8

The cylinder object can be created with other, optional attributes, which can be listed in any order. Here is a full list of attributes, most of which also apply to other objects:

pos Position: the center of one end of the cylinder; default = vector(0,0,0)

axis The axis points from pos to the other end of the cylinder, default = vector(1,0,0). Setting the axis makes length and size.x equal to the magnitude of the axis. An object's axis and up attributes are always perpendicular to each other. Changing the direction of axis also changes the direction of up so that the two directions always remain at right angles to each other.

up Which side of the cylinder is "up"; this has only a subtle effect on the 3D appearance of the cylinder unless a non-smooth texture is specified or the cross section is oval; the default is vector(0,1,0). An object's axis and up attributes are always perpendicular to each other. Changing the direction of up also changes the direction of axis so that the two directions always remain at right angles to each other.

length Length of axis and x component of size; default is 1. Setting the length makes the magnitude of axis equal to the length. The following feature is not yet available in VPython 7, in which you should update axis, not length: If you set the length to zero, the current value of axis is saved, the object becomes invisible, and axis is set to vector(0,0,0). If you later set length to nonzero, axis is set to the direction of the saved value, multiplied by the new length.

radius The radius of the cylinder, default = 1

size Instead of specifying length and radius, you can set size=vector(length,height,width), which means that the cross section of the cylinder can be elliptical. Setting size makes axis be equal to size.x (the length) times the direction of axis. or (except in VPython 7 currently) if axis is vector(0,0,0), the direction of the saved value of axis.

color Color of object, as a red-green-blue (RGB) triple: vector(1,0,0) is pure red, default = vector(1,1,1), which is color.white

red, green, blue (can set these color attributes individually), defaults are all 1

opacity Opacity of object, default = 1; 0 is completely transparent

shininess 0 to 1, default 0.6; governs the amount of specular reflections.

emissive If True, local and distant lights are ignored, and the brightness is governed by the object's own color. An example of its use is to put an emissive sphere at the location of a local_light, which looks like a glowing lamp. The default for emissive is False.

texture Texture of object; see Textures for currently available options (there are no materials in as there were in Classic VPython, but textures are similar)

canvas By default, an object such as a cylinder will be displayed in the most recently created 3D canvas, which will be the default canvas named "scene" unless you create a canvas yourself (see the related discussion at the start of the canvas documentation).

Note that the pos attribute for cylinder, arrow, cone, and pyramid corresponds to one end of the object, whereas for a box, sphere, or ring it corresponds to the center of the object.

If you include make_trail=True when you create the object, a trail will be left behind the object as you move it. For related options, see Leaving a Trail.

See Rotating an Object for an easy way to change the orientation of an object.

See description of Additional Attributes available for all 3D display objects.

When you start a VPython program, for convenience VPython creates a canvas and names it scene. By default, objects that you create go into that canvas. See Controlling One or More VPython Display Windows later in this reference for how you can create additional canvases and place objects in them.