Friday, February 16, 2007

How to draw 3D objects using python and VPython

I found another cool python package-- VPython, "3D Programming for Ordinary Mortals". It is very easy to use. Here is a simple example that creates a green sphere and a red box.

Steps:
1. Download and install VPython. For Windows users who already have Python 2.4.3 installed, go to http://www.vpython.org/win_download24.html, and install "VPython-Win-Py2.4-3.2.9.exe" only. Other users can select the proper version from here http://www.vpython.org/download.html
2. Open IDLE ("Start" -> "All Programs" -> "Python 2.4" -> "IDLE (Python GUI)")
3. "File" -> "New Window"
4. Type this inside:

from visual import *
redbox=box(pos=vector(4,2,3), size=(8.,4.,6.),color=color.red)
greenball=sphere(pos=vector(4,7,3), radius=2, color=color.green)
5. Hit "F5"
6. To change the camera angle, right-click and drag.

Scientific equations can also be used to create more complex diagrams and animations. Here are a couple more links to get started:
VPython applications for Teaching Physics
Simple 3D Programming Using VPython

No comments: