VPython in GlowScript

84 views
Skip to first unread message

Bruce Sherwood

unread,
Nov 6, 2014, 12:38:02 AM11/6/14
to rapyd...@googlegroups.com
Take a look at this program, and run it:

http://www.glowscript.org/#/user/Bruce_Sherwood/folder/Examples/program/Bounce-VPython/edit

The program looks exactly like a VPython program and behaves the same. The goal is to be able to run the same (or very nearly the same) code either in the classic VPython environment, the IPython environment that John Coady has built (the ivisual module), or in the GlowScript environment.

You can help in this endeavor by writing test routines at glowscript.org or by copying an existing VPython program to glowscript.org and reporting problems you may see at this experimental stage. There are some caveats:

As you'll see in the program whose link is shown above, the developmental header line must be 

     GlowScript 1.2dev VPython

If you copy this code (minus the header line) to a classic VPython file, with the header "from visual import *", the program will run with no changes.

At the moment, the GlowScript assumption is that your program is designed for "from visual import *", which is implicit. It is intended also to support other flavors ("from visual import box, cylinder", "import visual", "from vis import box, cylinder", and "import vis"). Standard in GlowScript (due to the nature of the underlying JavaScript) is that 1/2 is 0.5, not 0.

The curve object is that of GlowScript/RapydScript, which is somewhat different from the classic VPython curve. See the GlowScript Help for how to deal with curve.

make_trail has not been implemented.

I intend to provide a conversion program to deal with the following differences:

In creating an object such as a box, it is obligatory to specify pos, axis, size, color, and up as vectors, which can be written either as "vector" or as "vec". It is intended to update classic VPython to accept "vec", but you can achieve this effect in classic VPython by starting a program with the statement "vec = vector".

If you say b = box() and want to change its x position you must refer to b.pos.x, not b.x.

Change "scene.mouse.getclick()" to "scene.waitfor('click')", which works in either environment.

"scene.title(...)" must be changed to "scene.title.text(....)" or "scene.title.html(...)".

Some things are likely to require manual intervention for a while, including curve, materials (GlowScript textures), and testing two vectors for equality. In classic VPython you can say "if A == B:", where A and B are vectors, but in GlowScript you must say "if A.equals(B)", and VPython should be changed to accept this form. There is pick but no 3D pickpos functionality in GlowScript. GlowScript still doesn't handle keyboard events, except to type into a text area. Graphing compatibility has not really been addressed yet, but both environments ought to accept either g.plot(x,y) as in current GlowScript or g.plot(pos=(x,y)) as in current classic VPython.

The key point of this new GlowScript option is that although the basic GlowScript syntax is very similar to that of classic VPython, the details of the semantics are rather different. In particular, in classic VPython changing axis affects the length attribute of the size, and changing the length attributed affects the magnitude of the axis. The new GlowScript/VPython option mimics the behavior/semantics of classic VPython, in recognition that a large amount of VPython code exists that is worth porting to the browser. For GlowScript users who want to write in a Python-like language but prefer the original GlowScript semantics, they can choose RapydScript.

This development has been made possible by the Python-to-JavaScript compiler, RapydScript, and by the help of the RapydScript community in the persons of Alexander Tsepkov, Charles Law, and Salvatore di Dio.

Bruce Sherwood

unread,
Nov 6, 2014, 9:15:49 AM11/6/14
to rapyd...@googlegroups.com
Some features of VPython are not yet available in GlowScript, or not in the same form, including the objects faces, extrusion (and the shapes and paths library), 3D text, convex, and points. The frame capability is not available, but the GlowScript "compound" object can be used for many of the same purposes. Events must be handled with "callbacks" as in VPython 6 but not "polling" using scene.events and scene.getevent(). Buttons and other widgets, available in VPython through wxPython, are available in the browser using jquery. The factorial and combin functions have not been implemented.

Instead of faces, which expresses arbitrary shapes in terms of a list of triangles, GlowScript offers the vertex object and triangles or quads defined by vertex objects. This is a more powerful and flexible scheme than faces and is a candidate for adding to classic VPython.

I will attempt to implement the make_trail capability, but GlowScript offers "attach_trail" and "attach_arrow" which are significantly more powerful and are candidates for adding to classic VPython.

Bruce Sherwood

unread,
Nov 18, 2014, 4:23:42 PM11/18/14
to rapyd...@googlegroups.com
Run VPython programs in a browser!

Now in GlowScript (glowscript.org) you can write and run VPython programs. Although there are some restrictions, many existing VPython programs can run in a browser, which means that you can send a link to a colleague who can immediately run your program without installing Python and VPython. It also means that students can write and run VPython programs using existing programming instructions and without installing anything, though one must use a modern browser and a modern graphics capability (GPU). I'll mention that the 4th edition of the textbook "Matter & Interactions", available in January, includes explicit instruction in VPython.

You cannot import Python modules such as numpy, nor create your own modules to import.

Here are examples of VPython programs that run in either classic VPython or in GlowScript (when reading the program you can click "Run this program" to execute the program):



More demo programs in VPython will be added to the examples at glowscript.org.

In addition to classic VPython and this new browser VPython at glowscript.org, a third way to run a VPython program is in an IPython notebook, in a browser, thanks to the ivisual module created by John Coady.

A few VPython objects are not currently available in GlowScript: convex, extrusion (and the paths and shapes libraries), faces, frame, and the 3D text object. For details, see http://www.glowscript.org/docs/VPythonDocs/index.html.

Please report bugs!

Bruce

Salvatore Di Dio

unread,
Nov 20, 2014, 3:04:33 AM11/20/14
to rapyd...@googlegroups.com
Great Bruce
Reply all
Reply to author
Forward
0 new messages