0% found this document useful (0 votes)
2 views

VPython notes .

VPython is a tool for creating web applications, animations, and simulations, enabling users to visualize and interact with 3D objects. It allows for variable and vector assignments, manipulation of scalar quantities, and the creation of a canvas for displaying graphics. The document outlines the syntax for using VPython, its applications, and the advantages and disadvantages of using frames in web design.

Uploaded by

Mr.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

VPython notes .

VPython is a tool for creating web applications, animations, and simulations, enabling users to visualize and interact with 3D objects. It allows for variable and vector assignments, manipulation of scalar quantities, and the creation of a canvas for displaying graphics. The document outlines the syntax for using VPython, its applications, and the advantages and disadvantages of using frames in web design.

Uploaded by

Mr.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Vpython

Introduction:
Vpython is used to create a web application, animation and simulation.
A Web application (Web app) is an application program that is stored on a remote server and
delivered over the Internet through a browser interface. Web services are Web apps by
definition and many, although not all, websites contain Web apps.
Computer animation is a visual digital display technology that processes the moving images
on screen. In simple words, it can be put or defined as the art or power of giving life,
energy and emotions etc. to any non-living or inanimate object via computers. It can be
presented in form of any video or movie. Computer animation has the ability to make any
dead image alive. The key/main concept behind computer animation is to play the defined
images at a faster rate to fool the viewer so that the viewer should interpret those images as
a continuous motion of images. This animation in programming is shown by changing and
displaying object’s position with time. This animation can be also shown by changing the
size and or colour of the objects or canvas. In the programming these animations can be
managed by incorporating different events in the program. This type of animation is called
as event driven animation.

Applications of animation:
1. Education
2. Demonstration
3. Movie / film, games, entertainment
4. Advertising and marketing
5. Architecture
6. Cartoon
A simulation is a model that mimics the operation of an existing or proposed system, providing
evidence for decision-making by being able to test different scenarios or process changes.
This can be coupled with virtual reality technologies for a more immersive experience.
A simulation is an imitation of the dynamics of a real-world process or system over time.
Although simulation could potentially still be done “by hand,” nowadays it almost always
implicitly requires the use of a computer to create an artificial history of a system to draw
inferences about its characteristics and workings.

The behavior of the system is studied by constructing a simulation model, which usually
takes the form of a set of assumptions about the workings of the system. Once developed, a
simulation model can be used for a variety of tasks, including:
• Investigate the behaviour of the system under a wide array of scenarios. This is also
often referred to as “what-if” analyses;

• Changes to the system can be simulated before implementation to predict their


impact in real-world;

• During the design stage of a system, meaning while it is being built, simulation can
be used to guide its construction.

Computer simulation has been used in a variety of domains, including manufacturing, health
care, transport system, defence and management science, among many others.

variable assignment (Same as python):


Variables are used to store data, they take memory space based on the type of value assigned
to them. Variable name is also called as identifier. Following rules must be followed while
naming the variables in Python.
1. The name of the variable must always start with either a letter or an underscore (a to z or A
to Z or 0 to 9 or _). For example: _str, str, num, _num are all valid name for the variables.
2. The name of the variable cannot start with a number. For example: 9num is not a valid
variable name.
3. The name of the variable cannot have special characters such as %, $, # etc, they can only
have alphanumeric characters and underscore (A to Z, a to z, 0-9 or _ ).
4. Variable name is case sensitive in VPython which means ball and Ball are two different
variables in python.
How to create variables in VPython?
1. Write variable name (identifier) by following above rules.
2. Identifier name is followed by ‘equal to’ symbol (=).
3. Assign a value to the variable by writing value to the right of equal to sign.
Note that in Python we need not to explicitly mention the type of the variable. VPython
infer the type based on the value we are assigning.

vector assignment, scalar assignment and manipulation:


In VPython to initialize scalar or vector quantities one have to import visual mode by
using statement ‘from visual import *’.
Scalar quantities, the name itself specifies that these are the quantities which has
magnitude but not the direction. On the other hand, vector quantities are the one which has
magnitude as well as direction.
To create or define scalar quantity, type the desired name of the quantity followed by
the equal to sign and then the value of the scalar. E.g. n=15 To define a vector quantity, type
the desired name of the quantity followed by an equal sign and then the vector(x,y,z) operator,
which lets the VPython to know that the quantity is a vector quantity. A vector quantity has
three components and one has to specify all these three components while initialising a vector
quantity. E.g. pos=vector(2,6,-0.8)
e.g.
ball=sphere(pos=vector(1,2,3), radius=1)
here we created an entity ball of type sphere having radius=1 at the position (1,2,3) i.e.
1,2,3 are the position of the ball i.e. x, y and z coordinates of the ball. Here radius is a scalar
quantity and pos is a vector quantity. If want to change or manipulate scalar quantity i.e. here
radius, the syntax is as below
radius=new_value or mathematical equation
if one wants to change or manipulate the vector quantity i.e. here pos there are two two types
of statements one can use and those are as follows:
first type of statement:
ball.pos=vector(new_value1 or mathematical_equation_1,new_value2 or
mathematical_equation_2, new_value3 or mathematical_equation_3)
second type of statements:
ball.pos.x=new_value1 or mathematical_equation_1
ball.pos.x=new_value2 or mathematical_equation_2
ball.pos.x=new_value3 or mathematical_equation_3
e.g
ball.pos=vector(0,0,0)
or
ball.pos.x=0
ball.pos.y=0
ball.pos.z=0

Canvas:
Basically, the Canvas is a rectangular area intended for drawing pictures or other complex
layouts. You can place graphics, text, widgets or frames on a Canvas. A widget is an element
of a graphical user interface that displays information or provides a specific way for a user to
interact with the operating system (OS) or an application. In the context of a web browser, a
frame is a part of a web page or browser window which displays content independent of its
container, with the ability to load content independently. The main advantage of frames is
that it allows the user to view multiple documents within a single Web page. It is possible to
load pages from different servers in a single frameset.

The major disadvantages of using frames are:

• Bookmarks only bookmark the top level pages (the framesets themselves). A user is
unable to bookmark any of the Web pages viewed within a frame.
• Frames can make the production of a website complicated, although current software
addresses this problem.
• It is easy to create badly constructed websites using frames. The most common
mistake is to include a link that creates duplicate Web pages displayed within a frame.
• Search engines that reference a Web page only give the address of that specific
document. This means that search engines might link directly to a page that was
intended to be displayed within a frameset.
• Users have become so familiar with normal navigation using tables, the back button,
and so on, that navigating through a site that uses frames can be a problem.
• The use of too many frames can put a high workload on the server. A request for, say,
ten files, each 1 Kilobyte in size, requires a greater workload than a request for a
single 10 Kilobyte file.
• Older browsers do not support frames.

The advantages of frames:

• The main advantage of frames is that it allows the user to view multiple documents
within a single Web page.
• It is possible to load pages from different servers in a single frameset.

When using VPython the canvas shows objects in 3D. (0,0,0) is in the center of the canvas.

(0,0,0) x
z

The +x axis runs to the right, the +y axis runs up, and the +z axis points out of the screen,
toward you. x, y, and z are measured in whatever units you choose; the canvas is automatically
scaled appropriately. (You could, for example, create a sphere with a radius of 1E-15 m to
represent a nucleus, or a sphere with a radius of 1E6 m to represent a planet, though it wouldn't
make sense to put both of these objects in the same canvas!)
Syntax for displaying canvas is
mycanvas=canvas(master, option =value,….)
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These
options can be used as key-value pairs separated by commas
Sr.No. Option & Description

1 title – for giving title to the animation e.g title= ‘My first Vpython program’. Double
inverting commas also can be used for giving title. You can use the label object to display
text on the canvas. You can also place a title just above the canvas and/or a caption just
below when you create a canvas:
canvas(title='Some title', caption='A caption')
After a canvas has been created you can change the title with scene.title = " " or the
caption with scene.caption = " "

2 background – to set background colour, default colour is black e.g


background=color.red

3 resizable - If true (the default), the canvas size can be changed and if false the canvas
size can not be changed. If C is a canvas that is resizable, then C.bind('resize', R) will
execute the function R when the user resizes that canvas. If R has the form def R(ev):,
then ev.event will be 'resize' and ev.canvas will be the canvas C.

4 width – to define width of the canvas in pixel. Default value is 640 pixel and can be
modified e.g. width =100

5 height - to define heigth of the canvas in pixel. Default value is 400 pixel and can be
modified e.g. height =100

6 align -Set to "left" (canvas forced to left side of window), "right" (canvas forced to right side of
window), or "none" (the default alignment). If you have a single canvas, setting align to "left"
causes the canvas caption to be displayed to the right of the canvas. If you want to place a graph
to the right of a canvas, set the canvas align attribute to the string "left" and the graph align
attribute to the string "right". If the window is too narrow, the object that is on the right will be
displayed below the other object. If you want to place a graph to the right of the canvas but keep
the canvas caption underneath the canvas, create the graph first with align set to "right" and
activate the graph by plotting something in it, then create the canvas without specifying its value
of align. Another option is to specify align='left' for all canvases and graphs, in which case they
will about each other.

7 ambient - Color of nondirectional ("ambient") lighting. Default is color.gray(0.2).


8 lights -List of light objects created for this canvas. By default, scene.lights is this list:
[distant_light(direction=vec(0.22, 0.44,0.88),color=color.gray(0.8)),
distant_light(direction=vec(-0.88,-0.22,-0.44),color=color.gray(0.3))]
These are equivalent to the default lights that are automatically created for you. For how to
create your own distant and local lights. The lights can be changed only after creating the
canvas. You can eliminate the lights by setting scene.lights = [], an empty list, then create your
own lights. To change the color of the first of the two default lights, do this, where 0 refers to the
first of the two lights:

9 objects -A list of all the visible objects in the canvas; invisible objects and lights are not
listed

10 visible- Setting visible = False means that no objects are displayed, until scene.visible is
set True again.

11 delete()- Deletes all the objects in this canvas and then deletes the canvas itself

12 capture(filename) -Sends to your Download folder a png screen shot of the canvas. If filename
is the string "boxes" or "boxes.png" the file will be named "boxes.png". If you execute
scene.capture("boxes") repeatedly, the additional files will be named "boxes(1).png",
"boxes(2).png", etc. If you do not want to capture "label" objects, execute
scene.capture(filename, False)

13 sleep(n)- wait for n seconds before executing next instruction

14 To obtain the current location of the camera, use scene.camera.pos. camera.follow If you
say scene.camera.follow(ball), the center of the scene will continually be reset to the current
position of the ball object. To stop following the object, execute scene.camera.follow(None).

pixel_to_world - Gives the width of a pixel in "world" coordinates (that is, the coordinates you use to
position objects). This is read-only; you cannot set it. It is determined from the current value of
scene.range. An example of its use is that if you want the radius of a curve object to be 5 pixels, set
the radius to 5*scene.pixel_to_world.
Here are canvas options to wait for canvas updates:
scene.waitfor("redraw") Wait for the start of the next update of the canvas by the web browser
scene.waitfor("draw_complete") Wait for the end of the next update of the canvas by the web browser
Controlling the view
Here is a diagram that shows the relationship among the quantities discussed below that affect how the
scene appears to the viewer:
center Location at which the camera continually looks, even as the user rotates the position of the
camera. If you change center, the camera moves to continue to look in the same direction toward the
new center, unless you also change forward (see next attribute). Default vector(0,0,0).
forward Vector pointing in the same direction as the camera looks (that is, from the current camera
location, given by scene.camera.pos, toward scene.center). When forward is changed, either by
program or by the user using the mouse to rotate the camera position, the camera position changes to
continue looking at center. Default vector(0,0,-1). Its magnitude is not significant.
fov Field of view of the camera in radians. This is defined as the maximum of the horizontal and vertical
fields of view. You can think of it as the angular size of an object of size range, or as the angular size
of the longer axis of the window as seen by the user. Default pi/3.0 radians (60 degrees).
range The extent of the region of interest to the left or right of center. Setting range to 10 means that
scene.center.x+scene.range will be at the right edge of a square window. A sphere of radius 10 will fill
the window. A cubical box whose half-width is 10 will overfill the window, because the front of the box
in 3D appears larger than the xy plane passing through scene.center, unless the field of view is very
small.
up A vector representing world-space up. This vector will always project to a vertical line on the screen
(think of the camera as having a "plumb bob" that keeps the top of the screen oriented toward up). The
camera also rotates around this axis when the user rotates "horizontally". By default the y axis is
the up vector.
There is an interaction between up and forward, the direction that the camera is pointing. By default,
the camera points in the -z direction vector(0,0,-1). In this case, you can make the x or y axes (or
anything between) be the up vector, but you cannot make the z axis be the up vector, because this is
the axis about which the camera rotates when you set the up attribute. If you want the z axis to point
up, first set forward to something other than the -z axis, for example vector(1,0,0).
autoscale = False no automatic scaling (set range explicitly); autoscale = True automatic scaling
(default). It is often useful to let VPython make an initial canvas with autoscaling, then turn autoscaling
off to prevent further automated changes.
userzoom = False user cannot zoom in and out of the scene
userzoom = True user can zoom (default)
userspin = False user cannot rotate the scene
userspin = True user can rotate (default)
userpan = False user cannot pan the scene
userpan = True user can pan (default)
Controlling the view using scene.camera
The mechanisms described above for controlling the view are designed to try to make sure that the
objects are visible no matter how the user rotates or zooms the view, because the camera direction is
always automatically adjusted to point toward scene.center, which by default is at the origin, and
scene.range is automatically adjusted to correspond to the user zoom.
However, if you want to "fly" through the scene, with scene.center necessarily varying but zoom held
constant, it is more convenient to move and point the camera directly. It may possible, when you take
direct control of the camera, there is increased risk of seeing nothing, due to unintentionally pointing
the camera away from the objects, or moving the camera far away from the objects.
An example of controlling the camera directly is the fly-through in the program Stonehenge, in which
changing scene.camera.pos (the location of the camera) and scene.camera.axis (the direction the
camera is pointing) is a convenient way to move through the scene. Using these statements means that
you are controlling the view, so autoscaling is turned off (scene.autoscale is set to False).
scene.camera.pos The current location of the camera, whether due to your own settings, autoscaling,
or as positioned by the user, is scene.camera.pos. For example, mag(scene.center -
scene.camera.pos) is the distance from the current position of the camera to the center of the
scene. The vectors scene.camera.pos and scene.mouse.ray together define all of the 3D points under
the mouse cursor.

You can place the camera where you want it by executing scene.camera.pos = vector(x,y,z). When you
set the camera position in this way, scene.center is reset to the location scene.camera.pos +
scene.camera.axis. The effect is that the camera continues to point in the same direction as before, but
from a new location.
scene.camera.axis The current direction that the camera is pointing, whether due to your own settings,
autoscaling, or as positioned by the user, is scene.camera.axis, which is equal to scene.center -
scene.camera.pos; it points from the camera to scene.center.

When you change scene.camera.axis, scene.camera.pos is unaffected, but scene.center is reset to the
location scene.camera.pos + scene.camera.axis, and scene.forward is reset to norm
(scene.camera.axis).
scene.up Changing scene.up rotates the display around the z axis and would be appropriate for
example if you imagine the camera is attached to an airplane that rotates around its own axis in a turn
or barrel roll.
scene.camera.rotate(angle=..., axis=..., origin=...) As with other VPython objects, you can rotate the
camera by an angle in radians, about a specified axis (which by default is scene.camera.axis), relative
to a specified origin (which by default is scene.camera.pos).

VPython entities names and attributes:


Attributes are elements of an object that help define it. For example, spheres have the attribute of radius,
which defines how big or small they are. Other examples of attributes are pos attribute, short for
position, which defines where the object is in space. Attributes are separated by commas. All objects
have certain attributes that help define them.
Following are the different 3D objects available in VPython:
i) Cylinder:

• 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.
• 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. 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. 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;
• 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.
• 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.
• rotate - for an easy way to change the orientation of an object.
• 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.
ii) Arrow:

The arrow object has a straight box-shaped shaft with an arrowhead at one end. The following
statement will display an arrow pointing parallel to the x axis:
pointer = arrow(pos=vector(0,2,1), axis=vector(5,0,0), shaftwidth=1)
The arrow object has the following attributes and default values, like those for
cylinders: pos (0,0,0), axis (1,0,0), length (1), color (1,1,1) which is
color.white, red (1), green (1), blue (1), opacity (1), shininess (0.6), emissive (False), texture,
and up (0,1,0). As with box, the up attribute is significant for arrow because the shaft and head
have square cross sections, and setting the up attribute rotates the arrow about its axis.
Additional arrow attributes:
round`- By default, round is False and the arrow has a square cross-section. If round is True, it
has a circular cross-section. The round attribute cannot be changed after creating the arrow.
shaftwidth By default, shaftwidth = 0.1*(length of arrow)
headwidth By default, headwidth = 2*shaftwidth
headlength By default, headlength = 3*shaftwidth
Assigning shaftwidth = 0 makes it be the default (0.1 times the length of the arrow). If you
don't explicitly set headwidth or headlength, its length is based as shown above
on shaftwidth (either the default or whatever value you specify for shaftwidth).
If headlength becomes larger than half the length of the arrow, or the shaft becomes thinner
than 1/50 the length, the entire arrow is scaled accordingly.
This default behavior makes the widths of very short arrows shrink, and the widths of very long
arrows grow (while displaying the correct total length). If you prefer
that shaftwidth and headwidth not change as the arrow gets very short or very long, specify the
desired shaftwidth. In this case the only adjustment that is made is that headlength is adjusted
so that it never gets longer than half the total length, so that the total length of the arrow is
correct. This means that very short, thick arrows look similar to a thumbtack, with a nearly flat
head.
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.
iii) box:
This is how to create a box object:
mybox = box(pos=vector(x0,y0,z0),length=L, height=H, width=W)

The given position is in the center of the box, at (x0, y0, z0).
This is different from cylinder, whose pos attribute is at one end
of the cylinder. Just as with a cylinder, we can refer to the
individual vector components of the box as mybox.x, mybox.y,
and mybox.z. The length (along the x axis) is L , the height
(along the y axis) is H, and the width is W (along the z axis).
For this box, we have mybox.axis = vector(L, 0, 0) . Note that
the axis of a box is just like the axis of a cylinder.
For a box that isn't aligned with the coordinate axes, additional
issues come into play. The orientation of the length of the box
is given by the axis (see the diagram):
mybox = box(pos=vector(x0,y0,z0), axis=vector(a,b,c), size=vector(L,H,W) )
The axis attribute gives a direction for the length of the box, and here the length, height, and
width of the box are given in terms of "size" (if the length or size is not specified when creating
a box, the length is set to the magnitude of the axis vector).
You can rotate the box around its own axis by changing which way is "up" for the box, by
specifying an up attribute for the box that is different from the up vector of the coordinate
system:
mybox = box(pos=vector(x0,y0,z0),axis=vector(a,b,c), length=L,
height=H, width=W, up=vector(q,r,s))
With this statement, the width of the box will lie in a plane perpendicular to the (q,r,s) vector,
and the height of the box will be perpendicular to the width and to the (a,b,c) vector.
The box object has the following attributes and default values, like those for
cylinders: pos vector(0,0,0), axis vector(1,0,0), up vector(0,1,0), length (1), color (1,1,1)
which is color.white, red (1), green (1), blue (1), opacity (1), shininess (0.6), emissive (False),
and texture. Additional box attributes and details:
height -In the y direction in the simple case, default is 1
width -In the z direction in the simple case, default is 1
size -(length, height, width), default is vector(1,1,1)

mybox.size=vector(20,10,12) sets length=20, height=10, width=12


axis- Setting the axis makes the length of the box be equal to the magnitude of the axis vector.
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. Similarly, changing the direction of up changes the direction
of axis so that the two directions always remain at right angles to each other.
Order of operations: If when you create an object you specify values for axis and size and up,
the operations are executed in that order. That is, first axis is set (which sets the x component
of size and may change up if necessary to ensure that axis and up are perpendicular to each
other), then size is set (which sets the magnitude of the axis), and then up is set (which may
change axis if necessary to ensure that axis and up are perpendicular to each other).
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.
iv)sphere:

Here is an example of how to make a sphere:


ball = sphere(pos=vector(1,2,1), radius=0.5)
This produces a sphere centered at location (1,2,1) with radius = 0.5, with the current
foreground color.
The sphere object has the following attributes and default values, similar to
cylinder: pos vector(0,0,0), axis vector(1,0,0), color vector(1,1,1) which is
color.white, red (1), green (1), blue (1), opacity (1), shininess (0.6), emissive (False), texture,
and up vector(0,1,0). As with cylinders, up and axis attributes affect the orientation of the
sphere but have only a subtle effect on appearance unless a non-smooth texture is specified or
the cross section is oval. The magnitude of the axis attribute is irrelevant. Additional sphere
attributes:
radius -The radius of the sphere, default = 1
size - Default is vector(2,2,2). Instead of specifying the radius, you can set
size=vector(length,height,width), which means that the cross section of the sphere can be
elliptical, making it like the ellipsoid object. Unlike other objects, changing size doesn't change
axis, changing axis doesn't change size.
Note that the pos attribute for a sphere 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.
A fast sphere object: simple_sphere
In order to look smooth, a sphere is made of a very large number of triangles. Sometimes speed
is more important than ultimate beauty. The simple_sphere object can be used just like a
sphere, but a large number of simple_sphere objects is displayed very much faster than the
same number of regular spheres. For speed, the simple_sphere object is used in the points object
and in trails that use points.

v)text:-

With the text object you can display 3D text. The green 3D text shown above was created with
the following statement:
T = text(text='My text is\ngreen', align='center', color=color.green)
Labels were added to the display above to illustrate some of the main attributes of the text
object. Whether you extrude into or out of the screen, the text is created so that it is readable
left to right from the normal viewing position, in the +z direction.
Limitations: It is important to know that this 3D text object is very complex and expensive to
display, nor can you modify the letters displayed once the object has been created. If for
example you want to display rapidly changing text, use the label object (displayed in front of
the canvas) or the wtext object (displayed above the canvas as part of the title or below the
canvas as part of the caption).
Currently this 3D text object does not handle HTML options, such as bold, italic, subscript, or
superscript. The label and wtext objects do however support these options.
3D text is made out of quads and triangles which are then made into a compound object for
speed of display. You can make additional copies of a text object by cloning it. You can move
the text by specifying a new pos, and you can change its axis, up, color, opacity, shininess,
and emissive, as with other objects, but note that color and size are "multiplicative".
The 3D text object does not have a size attribute. Instead, the size of the display is specified
by height, length and depth as discussed below.
Here is a list of text attributes, in addition to the usual attributes of canvas, color, shininess, and
opacity (but not texture or bumpmap, currently):
pos -The location of the baseline of the text, to the left, to the right, or at the center of the text,
as per align.
align -Specify 'left' (default), 'right', or 'center'.
text -The text to be displayed, such as "My text is\ngreen" in the example above ('\n' is a new-
line character). Unicode strings are supported. After creating the object, the text is read-only -
- it cannot be changed. The reason for this is that it takes a significant portion of a second to
create a 3D text object. If you need to change the text, make the current object invisible
(visible=False) and create a new object. The text need not be a string; it can be anything that
you could print.
height -Height of an uppercase letter (see above); default is 1. Specifying the height when
creating the text object sets the scale for the entire text display. After creating the 3D text, you
can change the height of the object. For example, if you say T.height = 0.5*T.height, the letters
become shorter but the length stays the same. Changing the height also changes descent and
vertical_spacing.
length - length of the displayed text. You cannot specify a length when creating the 3D text,
but you can change this later. For example, if you say T.length = 0.5*T.length, the letters
become narrower but the height stays the same.
depth - Depth of the text; the default is 0.2 times the height when creating the 3D text. A
positive number means extrude toward you, out of the screen; negative means extrude away
from you, into the screen. After creating the 3D text, you can change this. For example, if you
say T.depth = 2*T.depth, the letters become twice as thick. If the depth is zero, the thickness
is made to be 0.01 times the height, and the text looks like a thin sheet.
font- Name of the desired font, either "sans" (sans serif, the default) or "serif". This is read-
only.
billboard- In computer graphics "billboard" behavior means that the object always faces you,
no matter how you reorient the camera. Specifying billboard = True when creating the text
turns this on, and then is read-only. Because lighting changes as you rotate the camera, you
may wish to specify emissive = True, so that the text is always bright.
color- The color of the text.
start_face_color and end_face_color -By default,the starting and ending faces are given the
same color as the rest of the text, as specified by color, but you can specify that other colors be
used for the starting or ending face of the extruded text.
show_start_face and show_end_face -By default, these attributes are True and the start and end
faces of the extruded text are displayed. If you set one of these to False, that face is left open.
descender- Height of the descender on lower-case letters such as y (whether or not there is such
a letter in the text). This is typically about 0.3 times the height. This is read-only but is affected
by changes in height.
upper_left, upper_right, lower_right, lower_left- The bounding box of the displayed text; all of
these are read-only. For example, if you create title = text(text="My Text"), you can place a
sphere at the upper left corner with the statement sphere(pos=title.upper_left). See the screen
display above.
start, end -The left-most and right-most locations on the baseline. These are read-only. If align
is 'left', pos is the same as start. If align is 'right', pos is the same as end.
vertical_spacing -Vertical distance from one baseline to the next in a multiline text. This is
read-only.
Axis- The axis points along the baseline; changing the axis changes the orientation of text. The
default is vector(1,0,0), with text going toward the right. Changing the axis only affects the
direction of the line of text, not the length or size.
up -Controls the up attribute of the text; changing up makes the text tip away from the vertical.

vi) ring:

The ring object is by default circular, with a specified radius and thickness and with its center
given by the pos attribute:
ring(pos=vector(1,1,1), axis=vector(0,1,0), radius=0.5, thickness=0.1)
The ring object has the following attributes and default values, similar to those for
cylinders: pos vector(0,0,0), axis vector(1,0,0), length (0.2), color vector(1,1,1) which is
color.white, red (1), green (1), blue (1), opacity (1), shininess (0.6), emissive (False), texture,
and up (0,1,0). As with cylinders, up has a subtle effect on the 3D appearance of a ring unless
a non-smooth texture is specified or the ring is oval. The axis attribute only affects the
orientation of the ring; the magnitude of the axis attribute is irrelevant. Additional ring
attributes:
radius -Radius of the central part of the ring, default = 1, so
outer radius = radius+thickness
inner radius = radius-thickness
Setting radius overrides the size attribute. Note that size.x = 2*thickness and in a circular ring
size.y = 2*(radius + thickness).
thickness -The radius of the cross section of the ring (1/10th of radius if not specified), not the
full diameter as you might expect. Note that size.x = 2*thickness and in a circular ring size.y
= 2*(radius + thickness).
size -Instead of specifying radius and thickness, you can make the ring be oval by specifying
its size as size=vector(length,height,width), with different height and width. Setting thickness
overrides the size attribute.
Note that the pos attribute for cylinder, arrow, cone, and pyramid corresponds to one end of
the object, whereas for a ring, sphere, and box 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.

Note -similar explanation for ellipsoid, frame, point, extrusion, helix, label,pyramid, vertex,
triangle,quad.
In VPython we can define our own attribute by using following syntax
object_name.attribute=quantity
Loops: same as python

Each line of code gives the computer a task (for example: make a sphere, print are, etc.), and
it goes through the lines obne by one and follows those directions. If one wanted the
computer to do the same task over and over again until you tell it to stop, where one can use
a loop.

A while loop has following 4 parts:

1. Initialization of a variable
2. A test to see if some condition has been met
3. Calculations or other operations (like print) using the variable
4. A small addition (increment) to the variable

e.g.

num=0

while num<11:
print(“Number=”, num)

num=num+1

print(“Now loop is completed, let us move to the next part of the code”)

Above is a program containing a loop that counts from 0 to 10 in steps of 1.

• The line num = 0 initializes the variable t. Initializing a variable just means to create it
and give it a value so that it's ready for use on a later line of code.
• The line while num < 11: tells the computer do keep doing whatever you write in the
loop until t is no longer less than 11. Notice that whatever is inside the loop is
indented.
• The line print("Number=",num) is inside the loop, and therefore gets repeated every
time the computer runs through the loop.
• The line num = num +1 is also inside the loop and also gets repeated. This line is
important because it is changing t. We told the computer to only repeat the loop as
long as num < 11 was true. Eventually, by adding 1 every time, num will be greater
than 10, and the loop will end.

(Note: for loop - Similar description)

Displaying multiple objects:


When one initialise an object it will get displayed on the canvas. So if you initialize multiple
objects, those many objects get displayed on the canvas. In animation type of programming
one needs to display all the objects with respect to each other. And their animation must be
coordinated with each other.
Events:
Events are things that happen in the system you are programming, which the system tells you
about so your code can react to them. For example, if the user clicks a button on a webpage,
you might want to react to that action by displaying an information box. In this article, we
discuss some important concepts surrounding events, and look at how they work in browsers.
Events are basically of two types mouse events and keyboard events.
Mouse events:
the simplest mouse interaction, which displays an icon and waits for a mouse click (the left
mouse button going down and up)::
scene.pause() An icon is displayed in the canvas to indicate a pause, waiting for a mouse
click.
You can show a message to the user on the canvas: scene.pause('Click to proceed'). You can
also obtain detailed information about the event:
ev = scene.pause()
You can use the package of information contained in the variable "ev":
sphere(pos=ev.pos, radius=0.1)
The package of information about the event includes information of what kind of event it was
(see further event details below):
box()
while True:
ev = scene.waitfor('mousedown mouseup')
if ev.event == 'mousedown':
print('You pressed the mouse button')
else:
print('You released the mouse button')
print(ev.pos) # the position of the mouse
Here are additional options:
scene.waitfor('click') # wait for mouse button click
scene.waitfor('mousedown') # wait for mouse button press
scene.waitfor('mouseup') # wait for mouse button release
scene.waitfor('mousemove') # wait for mouse to be moved
scene.waitfor('mouseenter') # when move into canvas
scene.waitfor('mouseleave') # when leave canvas
scene.waitfor('mousedown mousemove') # either event
scene.waitfor('keydown') # wait for keyboard key press
scene.waitfor('keyup') # wait for keyboard key release
scene.waitfor('click keydown') # click or keyboard
You can set up a function to be called when a mouse or keyboard event occurs. The following
will display the type of event (mouse click or keydown) and, in the case of a keydown event,
which key is involved (see further details below):
def process(ev):
print(ev.event, ev.which)

scene.bind('click keydown', process)


The quantity ev.event will be 'keydown' if a key was pressed or 'mousedown' if the left mouse
button was pressed. The quantity ev.which is the numerical key code or mouse button indicator
(mouse button is always 1 for now). For example, ev.which is 65 for the 'a' key. The
quantity ev.key is the corresponding character string, such as 'a' or 'delete'.
The quantity ev.canvas is the canvas associated with the event. You can bind events on
different canvases to the same function and be able to tell in which canvas the event occurred.
Note that scene.mouse.shift is true if the shift key is down at the time of the keyboard event;
similarly for scene.mouse.ctrl and scene.mouse.alt.
It is possible to use "anonymous" (unnamed) functions in this situation. For examples, see
the mouse drag discussion. However, anonymous functions cannot be used in VPython 7.
The package of information about the event that caused the end of the wait includes the
information of whether it was a mouse or keyboard event:
box()
ev = scene.waitfor('mousedown keydown')
if ev.event == 'mousedown':
print('You pressed the mouse button at', ev.pos)
else:
print('You pressed the key', ev.key)
The object scene.mouse contains lots of information about the current state of the mouse,
which you can interrogate at any time:
pos The current 3D position of the mouse cursor; scene.mouse.pos. VPython always
chooses a point in the plane parallel to the screen and passing
through scene.center. (See Projecting mouse information onto a given plane for
other options.)
pick Execute obj = scene.mouse.pick- to obtain the object pointed to by the mouse. If you
have a box named B, you can determine whether the picked object is that box
by asking if (B == obj). If there is no object pointed to by the
mouse, obj is None. Also, obj will be None if the object has
the pickable attribute set to False. For example, the curves, spheres, and arrows
created by make_trail, attach_trail, and attach_arrow are not pickable, and you
may wish to specify that some of your own objects are not pickable. At present
label and helix cannot be picked. For curve objects, obj.segment is the number
of the picked segment along the curve, starting with 1 (representing the segment
from point number 0 to point number 1). You can test for a curve object with if
instanceof(obj, curve):. See the Web VPython example MousePicking.
ray A unit vector pointing from the camera in the direction of the mouse cursor.
project() - Projects position onto a plane. See Projecting mouse position onto a given plane.
alt = True if the ALT key is down, otherwise False
ctrl = True if the CTRL key is down, otherwise False
shift = True if the SHIFT key is down, otherwise False
Different kinds of mouse
The mouse routines in Web VPython currently handle only the left (or only) mouse button.
Projecting mouse position onto a given plane
Here is a way to get the mouse position relative to a particular plane in space:
temp = scene.mouse.project(
normal=vec(0,1,0),
point=vec(0,3,0) )
# None if no intersection with plane:
if temp != None) ball.pos = temp
This projects the mouse cursor onto a plane that is perpendicular to the specified normal. If the
second parameter is not specified, the plane passes through the origin. It returns a 3D position,
or None if the projection of the mouse misses the plane (scene.mouse.ray is parallel to the
plane).
In the example shown above, the user of your program will be able to use the mouse to place
balls in a plane parallel to the xy plane, a height of 3 above the xy plane, no matter how the
user has rotated the point of view.
You can instead specify a perpendicular distance from the origin to the plane that is
perpendicular to the specified normal. The example above is equivalent to
temp=scene.mouse.project(
normal=vec(0,1,0), d=3 )
Polling and callback
There are two different ways to get a mouse event, "polling" and "callback". In polling, you
continually check scene.mouse.events to see whether any events are waiting to be processed,
and you use scene.mouse.getevent() to get the next event to process. Prior to VPython 6, this
was the only way you could handle mouse or keyboard events, but this scheme is not available
in Web VPython
In the callback method, you specify a function to be executed when a specific type of event
occurs, and the function is sent the event information when the specified type of event occurs.
For many purposes this is a better way to handle mouse and keyboard events.

Callbacks
Here is a simple example of how to use callbacks to process click events:
s = sphere(color=color.cyan)

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

scene.bind('click', change)
We define a "function" named "change". Then we "bind" this function to click events occurring
in the canvas named "scene". Whenever VPython detects that a click event has occurred,
VPython calls the bound function, which in this case toggles the sphere's color between cyan
and red.
This operation is called a "callback" because with scene.bind you register that you want to be
called back any time there is a click event. Here are the built-in events that you can specify in
a bind operation:
Mouse: click, mousedown, mousemove, mouseup
Keyboard: keydown, keyup
Other: redraw, draw_complete
The event 'mousedown' or 'mouseup' occurs when you press or release the left button on the
mouse, and the 'mousemove' event occurs whenever the mouse moves, whether or not a button
is depressed. A 'redraw' event occurs just before the 3D scene is redrawn on the screen, and a
'draw_complete' event occurs just after the redrawing (these event have rather technical uses
such as timing how often redrawings occur, or how much time they take).
You can bind more than one event to a function. The following will cause the callback function
to be executed whether you press or release the mouse button:
scene.bind('mouseup mousedown', change)
Another use of callbacks is to drive a function periodically. See the example program Bounce-
Callbacks-VPython. Also see the related documentation for the
functions get_library() and read_local_file().
Details of the event
You can get detailed information about the event by writing the callback function like this (note
the variable 'evt' in parentheses):
def info(evt):
print(evt.event, evt.pos)
Here we specify an argument in the definition of the callback function ('evt' in this case). When
the function is called due to a specified event happening, VPython sends the function
information about the event. The name of the argument need not be 'evt'; use whatever name
you like. In addition to evt.event, evt.pos, and evt.pick, there is further event information in the
form of evt.press and evt.release which are 'left' for press or release events.
The quantity evt.event will be 'keydown' if a key was pressed. The quantity evt.which is the
numerical key code or mouse button indicator (mouse button is always 1 for now). For
example, evt.which is 65 for the 'a' key. Note that scene.mouse.shift is true if the shift key is
down at the time of the keyboard event; similarly for scene.mouse.ctrl and scene.mouse.alt.
In classic VPython you can optionally have VPython send the callback function an additional
argument, but this is not currently possible in Web VPython.
Right or middle button mouse events
There is currently no way in Web VPython to handle right button or middle button events.
Unbinding
Suppose you executed scene.bind('mousedown mousemove', Drag), but now you no longer
want to send mousemove events to that function. Do this:
scene.unbind('mousemove', Drag)
Custom events: triggers (works in Web VPython but currently not with installed Python)
It is possible to create your own event type, and trigger a function to do something. Consider
the following example, where the custom event type is 'color_the_ball':
def clickFunc():
s = sphere(pos=scene.mouse.pos, radius=0.1)
scene.trigger('color_the_ball', s)

def ballFunc(ev):
# ev.type is "color_the_ball"; ev.event is the new sphere
ev.event.color=color.cyan

scene.bind('click', clickFunc)
scene.bind('color_the_ball', ballFunc)

box(pos=vector(1,0,0))
We bind click events to the function clickFunc, and we bind our own special event type
'color_the_ball' to the function ballFunc. The function clickFunc is executed when the user
clicks the mouse. This function creates a small sphere at the location of the mouse click, then
triggers an event 'color_the_ball', with the effect of passing to the function ballFunc
information that includes the new sphere object. Finally ballFunc applies a color to the sphere.
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 = scene.mouse.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)
Drag Example
Here is the sequence of mouse events involved in dragging something:
1) Determine that the mouse button has been depressed (a mousedown event).
2) Continually watch for the mouse to move, and use scene.mouse.pos to update positions.
These are mousemove events.
3) Conclude the drag when the mouse button has been released (a mouseup event).
The way you detect these mouse events is by writing functions that are bound to the mouse
events using scene.bind, and Web VPython will execute these functions when these mouse
events occur.
Here is a complete routine for repeatedly creating and dragging a sphere, so that you can
arrange many spheres on the screen. While being dragged the sphere is red, but its color
changes to cyan when the mouse button is released.
scene.range = 5
box()

drag = False
s = None # declare s to be used below

def down():
global drag, s
s = sphere(pos=scene.mouse.pos,
color=color.red,
size=0.2*vec(1,1,1))
drag = True

def move():
global drag, s
if drag: # mouse button is down
s.pos = scene.mouse.pos

def up():
global drag, s
s.color = color.cyan
drag = False
scene.bind("mousedown", down)
scene.bind("mousemove", move)
scene.bind("mouseup", up)
It is also possible to use "anonymous" (unnamed) functions, an extended feature of the
RapydScript-NG Python-to-JavaScript compiler, as shown here (this will NOT work with
installed Python):
scene.range = 5
box()

drag = False
s = None # declare s to be used below

scene.bind("mousedown", def ():


global drag, s
s = sphere(pos=scene.mouse.pos,
color=color.red,
size=0.2*vec(1,1,1))
drag = True

scene.bind("mousemove", def ():


global drag, s
if drag: # mouse button is down
s.pos = scene.mouse.pos

scene.bind("mouseup", def ():


global drag, s
s.color = color.cyan
drag = False

Other mouse events: You can also watch for mouseenter (the mouse is moved from the
outside of the canvas to the inside), mouseleave (the mouse leaves the canvas), and click.
Multiple event types: You can bind a function to more than one type of event. Here is a
function bound to both mousedown and mouseup events, either of which will cause a sphere
to be created:
scene.bind("mousedown mouseup", def ():
sphere(pos=scene.mouse.pos)
Unbinding: After binding a function to a mouse event, you can unbind the function, in which
case Web VPython will no longer send events to your function. In the program shown above,
if you place scene.unbind("mousedown") in the mouseup event, you will be able to drag just
one sphere.
Just one: If you use scene.one instead of scene.bind, the binding occurs for just one event and
then is automatically unbound. In the program shown above, if you specify scene.one for the
mousedown event, you will be able to drag just one sphere.
Custom events: You can set up your own custom events using scene.trigger. In the following
sample program, first you see a box, then the while loop halts waiting for the custom "ball"
event to occur. When you click, the function t is executed, and in this function a sphere is
created and a new type of event, "ball", is triggered by scene.trigger, with the optional argument
b, representing the new sphere. With this triggering of a "ball" event, function f receives the
triggered arguments in ev and sets the sphere's color to blue. The triggering of a "ball" event
also breaks through the scene.waitfor in the while loop that was waiting for a "ball" event. The
process in the loop then repeats.
def t():
b = sphere(pos=scene.mouse.pos, radius=0.2)
scene.trigger("ball", b)
scene.bind("click", t)

def f(ev):
# ev.type is "ball"; ev.event is newball
ev.event.color = color.blue
scene.bind("ball", f)

cube = box(pos=vec(-2,0,0))
while True:
s = scene.waitfor("ball")
print(s.type, s.event.pos) # "ball" and the new sphere's pos
cube.rotate(angle=0.1, axis=vec(0,0,1))
Keyboard Interactions
As with mouse events, you can wait for various kinds of keyboard or mouse events:
scene.waitfor('keydown') # wait for keyboard key press
scene.waitfor('keyup') # wait for keyboard up key release
scene.waitfor('click keydown') # click or keyboard
scene.waitfor('click') # wait for a click
scene.waitfor('mousedown') # wait for mouse button press
scene.waitfor('mouseup') # wait for mouse button release
scene.waitfor('mousemove') # wait for mouse to be moved
scene.waitfor('mousedown mousemove') # either event
The event 'keydown' or 'keyup' occurs when you press or release a key on the keyboard (if you
hold down a key, you may get multiple 'keydown' events due to repeats). Keyboard events are
not actually associated with particular canvases, but mouse events are, and the capability of
waiting for both keyboard and mouse events makes it necessary to name a canvas, such as
"scene".

You can obtain a package of information about the event that caused the end of the wait:
box()
ev = scene.waitfor('click keydown')
if ev.event == 'click':
print('You clicked at', ev.pos)
else:
print('You pressed key '+ev.key)

Handling multiple simultaneously pressed keys


Sometimes you need to know the names of all the keys that are currently down.
The keysdown() function provides a list of the names of the keys that are currently pressed
down; for example, the list might be ['b', 'F', 'ctrl']. Keys with multiletter names are 'backspace',
'caps lock', 'tab', 'shift', 'ctrl', 'alt', 'pageup', 'pagedown', 'end', 'home', 'left', 'up', 'right', 'down',
'insert', 'delete', 'break', and the function keys 'f1' through 'f10'. If you hold down the right and
up arrow keys in the following program, the ball will accelerate toward the upper right of the
canvas.
scene.range = 20
ball = sphere(color=color.cyan)
v = vec(0,0,0)
dv = 0.2
dt = 0.01
while True:
rate(30)
k = keysdown() # a list of keys that are down
if 'left' in k: v.x -= dv
if 'right' in k: v.x += dv
if 'down' in k: v.y -= dv
if 'up' in k: v.y += dv
ball.pos += v*dt
If you hold down the 'm' key and the shift key, keysdown() will be ['shift', 'M']. That is, the 'm'
will be capitalized, and the 'shift' key will also be reported as down. In the list of down keys,
the entries are in time order: the first key in the list was pressed the earliest, and the last key in
the list was pressed most recently.

Handling events with callbacks


Here is a test routine using callbacks that lets you type text into a label.
prose = label() # initially blank text

def keyInput(evt):
s = evt.key
if len(s) == 1: # includes enter ('\n')
prose.text += s # append new character
elif s == 'delete' and len(prose.text) > 0:
prose.text = prose.text[:-1] # erase letter

scene.bind('keydown', keyInput)
We define a "function" named "keyInput". Then we "bind" this function to 'keydown' events
occurring in the canvas named "scene". Whenever VPython detects that a 'keydown' event has
occurred, VPython calls the bound function, which in this case adds the input to the text of the
label object.
Note that evt.ctrl, evt.alt, and evt.shift are True if the corresponding key is down at the time of
the event (evt.shift is also True if Caps Lock is in effect). Also, for a keyboard event evt.type is
'keydown' or 'keyup' and evt.which is the numerical code for the key.
This operation is called a "callback" because with scene.bind you register with VPython that
you want to be called back any time there is a 'keydown' event. Here are the built-in events that
you can specify in a bind operation:
Keyboard: keydown, keyup
Mouse: click, mousedown, mousemove, mouseup
Other: redraw, draw_complete
The event 'keydown' or 'keyup' occurs when you press or release a key on the keyboard. The
events 'click', 'mousedown', 'mousemove', and 'mouseup' are discussed in the mouse section.
A 'redraw' event occurs just before the 3D scene is redrawn on the screen, and a
'draw_complete' event occurs just after the redrawing (these event have rather technical uses
such as timing how often redrawings occur, or how much time they take).
You can bind more than one event to a function. The following will cause the callback function
to be executed whether you click with the mouse or press a key on the keyboard:
scene.bind('click keydown', myFunction)
With the following statement, click event will no longer be sent to myFunction:
scene.unbind('click', myFunction)

Pausing for keyboard or mouse input


Often you want to pause for either mouse or keyboard input which you can do by
executing scene.waitfor('click keydown').

Animation:
Computer animation is a visual digital display technology that processes the moving images
on screen. In simple words, it can be put or defined as the art or power of giving life,
energy and emotions etc. to any non-living or inanimate object via computers. It can be
presented in form of any video or movie. Computer animation has the ability to make any
dead image alive. The key/main concept behind computer animation is to play the defined
images at a faster rate to fool the viewer so that the viewer should interpret those images as
a continuous motion of images. This animation in programming is shown by changing and
displaying object’s position with time. This animation can be also shown by changing the
size and or colour of the objects or canvas. In the programming these animations can be
managed by incorporating different events in the program. This type of animation is called
as event driven animation.

One important method use in the animation loop is rate(frequency). It is used to halts
computations until 1.0/frequency seconds after the previous call to rate().
For example, rate(50) will halt computations long enough to make sure that at least 1.0/50.0 =
0.02 second has elapsed. If this much time has already elapsed, no halt is performed. If you
place rate(50) inside a computational loop, the loop will execute at a maximum of 50 times per
second, even if the computer can run faster than this. This makes animations look about the
same on computers of different speeds, as long as the computers are capable of carrying out 50
computations per second.

It is obligatory to place a rate statement in an animation loop. Without it, no screen updates or
processing of mouse or keyboard events are possible.

Another option is to use the function sleep: sleep(0.02) means "don't do anything for 0.02
seconds" and is equivalent to rate(50). The sleep function periodically renders the scene and
processes mouse events, making it possible to continue using zoom and rotate, whereas the
standard Python function time.sleep(t) does not do this.

Applications of animation:
1. Education
2. Demonstration
3. Movie / film, games, entertainment
4. Advertising and marketing
5. Architecture
6. Cartoon

scaling objects:
In the animation, when the object is moving along the x or y axis there is no or in xy plane
there is no question of changing the size of the object. But if there is motion in Z-direction i.e.
the object is moving either away from you where the object size must be decrease
proportionally or the object is moving towards the positive z-axis direction, it means that the
object is coming towards you. In the later case the size of the object must be increase in
proportion. The proportionality constant between the original size and the final size is called
as a scaling factor. Another example is when one wants to show the direction and the magnitude
of velocity of projectile motion. The direction is shown by an arrow pointing to different
directions at every point (tangent to the curve at that point). Also the length of the arrow gives
the magnitude. In other words, the length of the arrow reflects magnitude of the velocity. Thus,
the length of the arrow must change in proportional to the velocity of the object. This
proportionality constant i.e. scaling factor I given by
scale factor=desired object size / present size of the object

You might also like