PyZine
 


Article Finder
People
Issue 6 - Revision 7  /   October 18, 2004 


 
  Py Links:
Latest Issue
Issue 08
Issue 07
Issue 06
Issue 05
Issue 04
Issue 02
Issue 01
 
 
Downloads
     
  Articles:
Throughout the quarter we cover topics of interest to Python developers.

  Python MIDI

  Python in Bioinformatics

  Getting Twisted

  optparse

  jEdit

  Python RFID

  3d Graphics in Python

 
 
 
     


3D Graphics in Python

- - - - - - - - - - - -

By Tamer Fahmy  | October 18, 2004

print
Abstract

A broad range of 3D modelling tools and 3D graphics libraries exist that take advantage of the power of Python. However, the sheer number of applications available makes it hard to choose the appropriate tool for the particular job at hand. This article presents a taxonomy of 3D applications for Python based on the type of application, strengths, weaknesses and different programming paradigms.

Overview

Computer graphics is the field of synthesizing or augmenting imagery through digital means and integrating or altering visual and spatial information sampled from the real world. Apart from its applications in science, computer graphics is also used for artistic, engineering and recreational purposes.

Starting originally with the output of text and numbers on electronic displays, computer graphics today typically refers to creating images, not text. The computer graphics field can be divided into several areas: real-time 3D rendering as used, for example, in video games; video capture and video creation rendering; special effects editing as used, for example, in movies and television; image editing;, and modeling as used, for example, in the engineering and medical fields.

Two-dimensional computer graphics can be broadly categorized as consisting of vector graphics, which are represented and stored as properties of shapes such as colored lines or circles, and raster graphics, which are represented and stored as a set of colored points. They are used in many graphical user interfaces (GUIs), paint and drawing programs such as "The GIMP", most early video games and vector drawing programs.

On the other hand, three-dimensional computer graphics are based on vector or "wire-frame" representations of virtual objects which involve the need for some kind of three-dimensional internal representation of objects and of lighting characteristics.

As computers became more powerful in terms of processing power some major advances in 3D computer graphics were developed. These include:

  • Flat shading - a lightning technique where each polygon of an object is shaded based on the angle between the polygon's surface "normal" and the direction of the light source.
  • Gouraud shading - a fast and resource-concious technique used to simulate smoothly shaded surfaces by interpolating vertex colors across a polygon's surface.
  • Texture mapping - a technique for simulating surface detail by mapping images (textures) onto polygons.
  • Phong shading - a smooth shading technique that approximates curved-surface lighting by interpolating the vertex normals of a polygon across the surface; the lighting model includes glossy reflection with a controllable level of gloss.
  • Bump mapping - a normal-perturbation technique used to simulate bumpy or wrinkled surfaces.
  • Ray Tracing - a method based on the physical principles of geometric optics which can simulate multiple reflections and transparency.
  • Radiosity - a technique for global illumination that uses radiative transfer theory to simulate indirect (reflected) illumination.
Interactive Computer Graphics System Paradigms

There are 2 different paradigms for the programming of interactive computer graphics.

Immediate mode

An example of an immediate mode system is the widely used cross-platform 2D and 3D graphics library OpenGL. The graphics operations are taken out immediately. It is the application itself that maintains the data that describe a model and the model data is not duplicated by the graphics system.

Immediate mode graphics libraries require the application programmer to constantly handle all details, such as when to draw what on the screen and how to react to events. Sooner or later the need will arise to organize your data in one way or another depending on the complexity of your application, which then also is facilitated by code reuse. Most importantly, you are constantly calling functions that could have a serious impact on your application's performance if done inappropriately. Nevertheless, this low-level paradigm gives you the greatest control over the graphics, which is an absolute necessity for certain types of applications.

Retained mode

An example of a retained mode system is Coin3D or Java3D. The graphics system retains a copy of all the data describing a model by using a scene graph that gets traversed. It is necessary to specify the complete model in advance by using predefined data structures.

These libraries offer you a high-level view of your application and come with a set of predefined data structures and a way to organize them - usually in a scene graph. A scene graph is made up of an ordered collection of nodes. Each node holds a piece of information, such as surface material, shape description, geometric transformation, light or a camera.

After you have described and defined a scene graph, you can apply operations to the scene such as rendering, picking, searching, computing a bounding box and writing it to a file. Apart from this, other special nodes exist such as engines which are used to animate parts of the scene or constrain certain parts of the scene in relation to other parts. Additionally, sensors exist that detect when something in the scene graph changes and then call a function supplied by the application.

These mechanisms allow you to build applications or prototype them quickly by making use of a rich collection of predefined data structures.

Application-driven vs. data-driven scene-graph APIs

Another important distinction is made in this context between data-driven and application-driven scene-graph APIs. Data-driven toolkits only re-render when something changes in the scene - for example, when the user changes the perspective of the scene. Application-driven toolkits re-render the scene continuously in an application loop, using up all the CPU resources available. The latter case is used for games and simulation software such as flight simulators where high and constant frame rates are desirable. In general a data-driven approach fits better for a general purpose 3D API where a constant frame rate is not the main concern – and, more importantly, when resources have to be available for other computational tasks. Typical examples benefiting from this approach are applications that visualize results of numerical simulations or 3D editors (level editor for games). Examples of application-driven APIs are Performer or OpenSG, whereas Open Inventor or Coin3D offer a data-driven API.

Performance is a key problem, hence these libraries are usually implemented in a compiled language such as C++. However, the use of C++, a statically typed language with a heavy and complicated syntax, tends to be error-prone and cumbersome. A dynamically typed and bound language with an intuitive syntax such as Python provides a more natural interface.

Consequently, bindings have been created to interface with these libraries in order to make them accessible from within the Python interpreter, allowing true Rapid Application Development.

So what are the pros and cons of each paradigm with regard to Python?

From my personal point of view retained mode systems suit Python's characteristics much better than immediate mode systems. Firstly, continually calling functions as is required by immediate mode libraries creates an additional performance load by adding the calling overhead of the wrapped functions or methods. Further, the high-level view that retained mode libraries offer allows better usage of Python's key strength as a controlling language. The application is created by describing the model in advance and upon interaction certain actions are carried out, such as callbacks being called, which modify data in the scene graph. The rendering, i.e. scene graph traversals, is done by calling C/C++ code without constantly running code in the Python interpreter, which therefore doesn't affect performance as much as when immediate mode libraries are used.

Application types with regard to clustering

Different application types have different limitations and demand diverse setups in a cluster.

Compute-limited

Applications generate data more slowly than the graphics system can accept it. A suitable setup for such an application would be to split the computing task over multiple clients that feed a few rendering servers.

Graphics-limited

Applications make intensive use of the graphics hardware. For this type a single client is usually sufficient to feed multiple rendering servers. A special distinction in graphics-limited applications is made for pixel fill-limited where, e.g., volume rendering with 3D textures requires high fill rates while using few primitives.

Interface-limited

Applications are limited by the rate at which they can issue geometry to the graphics system, e.g. visualizations of large geometric data sets. In this context we also speak of transformation-limited or geometry-limited applications. To satisfy the needs of this kind of application the best approach is to use display lists or a retained mode interface. Usually the bottleneck in a cluster is handled by an equal number of application clients and rendering servers.

Resolution-limited

Applications are limited in their ability to visualize data due to a lack of display resolution. This is typical in many scientific applications where it is important to view all the data at a macroscopic level to get an overview of a data-set. Such applications require the combined resolution of multiple graphics accelerators and a high-resolution tiled display.

Apart from Python's being widely used in the distributed systems and networking field, the taxonomy presented above comes in handy when used with systems such as Chromium, which will be discussed in the next section of this article.

Some Python 3D graphics libraries and tools

PyOpenGL

URL: http://pyopengl.sourceforge.net/

Mike C. Fletcher's PyOpenGL is an excellent Python binding for OpenGL.

PyOpenGL includes support for OpenGL v1.0, OpenGL v1.1, GLU, GLUT v3.7, GLE 3, WGL 4, and Togl (Tk OpenGL widget). It also includes support for dozens of extensions (where they are supported in the underlying implementation).Additionally, it offers a retained mode library called OpenGLContext which features a partial implementation of VRML97.

PyOpenGL is perfectly suited for Rapid Application Development of OpenGL applications in Python. Apart from this, it is used by a variety of Python projects to add OpenGL support.

VPython

URL: http://www.vpython.org/

VPython is a project that combines Python, Numerical Python, Tcl/Tk, and an OpenGL-based graphics package called "visual" into a really easy system for real-time 3D graphics programming. It was developed by David Scherer.

VPython is very well suited for educational purposes and scientific graphics programs.

Pivy

URL: http://pivy.tammura.at/

Pivy is a Coin3D binding for Python developed by me. Coin3D is a high-level 3D graphics library with a C++ Application Programming Interface. Coin uses scene-graph data structures to render real-time graphics suitable for most kinds of scientific and engineering visualization applications.

Coin3D is fully compatible with SGI Open Inventor 2.1, the de facto standard for 3D visualization and visual simulation software in the scientific and engineering community. Additional features in Coin3D include VRML97 support, 3D sound through OpenAL, 3D textures, and parallel rendering on multiple processors.

Add-on libraries such as SIM Voleon or SIM Scenery complement Coin3D, making volume and terrain rendering possible while using the same consistent API.Pivy incorporates a Scripting Node for the scene graph which is capable of executing Python code and callback functions. Already existing C++ applications or frameworks based upon Coin3D can, therefore, also benefit from Python's power.Pivy, a data-driven retained mode library, is highly suited as a general purpose 3D library. Thanks to Open Inventor's API conformance it is very well documented through books, and online C++ API documentation which is easily translated to Python due to its high-level nature and sample code.

Pivy's biggest strength - which many other libraries lack - is that it supports the standard and well-known file formats Open Inventor and VRML97. This allows the user to describe his whole scene in a text file through a simple and straightforward syntax without having to write a single line of code. By using a Scripting Node one can embed Python code in these text files, which allows the user to do more interesting stuff similar to the Javascript facility in VRML97.

Panda3D

URL: http://www.etc.cmu.edu/panda3d/

Panda3D was developed by Disney VR Studio for its massively multiplayer online game Toontown Online. The engine contains a strong networking architecture that allowed them to create shared experiences quickly and easily. The studio then released the engine as Open Source and began to coordinate with universities on preparing it for the Open Source community. In 2003, Panda3D made its debut through Carnegie Mellon University.

Panda3D operates with a combination of C++ and Python. Developers may script in Python, and Panda3D's special interrogate function converts the C++ code to Python. Panda3D is extensible, so developers may use additional Python modules and C++.Being an application-driven library, Panda3D is very well suited for games and simulations.

Don't miss Airblade, a game written using Panda3D which can be downloaded at http://www.etc.cmu.edu/panda3d/projects/Airblade/.

OGRE

URL: http://www.ogre3d.org/

OGRE (Object-Oriented Graphics Rendering Engine) is a scene-oriented, flexible 3D engine written in C++, designed to make it easier and more intuitive for developers to produce games and demos utilizing 3D hardware. The class library abstracts all the details of using the underlying system libraries such as Direct3D and OpenGL and provides an interface based on world objects and other intuitive classes.It comes with Python bindings and is focused on game development.

Soya 3D

URL: http://oomadness.nekeme.net/en/soya/

Soya 3D is a high-level fully object-oriented 3D engine for Python, designed with games in mind. Soya is written partly in Python and partly in C.

VTK

URL: http://www.vtk.org/

The Visualization ToolKit (VTK) is a software system for 3D computer graphics, image processing, and visualization used for scientific purposes. It also features a variety of visualization algorithms.

In contrast with the scene-graph based APIs VTK uses a pipeline approach.A scientific data visualizer called MayaVi for VTK which can be downloaded from http://mayavi.sourceforge.net/. MayaVi was developed by Prabhu Ramachandran.

PyFX

URL: http://graphics.cs.lth.se/pyfx/

PyFX is an interesting Python framework which makes it really easy to use graphics effects based on Cg shaders.

PyFX is implemented on top of OpenGL and allows the user to write shader code in Python which is executed on graphics processors (GPUs).

Crystal Space

URL: http://crystal.sourceforge.net/

Crystal Space is a portable 3D Game Development Kit written in C++ using OpenGL for which a Python binding exists. It offers a lot of features, among them shader support (CG, vertex programs, fragment programs, ...), 3D sprites (frame-based or with skeletal animation using the cal3d animation library), procedural textures, scripting (using Python) and a physics plug-in based on ODE.

Open Scene Graph

URL: http://openscenegraph.sourceforge.net/

The Open Scene Graph is an Open Source high-performance 3D graphics toolkit, used by application developers in fields such as visual simulation, games, virtual reality, scientific visualization and modelling.

SGI Performer

URL: http://www.sgi.com/products/software/performer/

SGI's OpenGL Performer is a an application-driven library used in real-time visual simulation and other performance-oriented 3D graphics applications for which a Python binding exists.

Vision Egg

URL: http://www.visionegg.org/

The Vision Egg is a high-level interface between Python and OpenGL. In addition to methods for automatic generation of traditional visual stimuli such as sinusoidal gratings and random dot patterns, it has a number of functions for moving numeric data, images, movies, text, and 3D objects to and from the video card and enabling use of some of its features such as perspective distortion. Therefore, it is also useful for anyone wishing to make use of the features of today's graphics cards.

Jython3D

URL: http://www.smallshire.org.uk/jython3d.htm

Rob Smallshire has put up a page providing examples and descriptions of how to make use of Java3D from Jython.

cgKit

URL: http://cgkit.sourceforge.net/

The Python Computer Graphics Kit is a collection of Python modules that contain the basic types and functions for creating 3D computer graphics images. The kit mainly focuses on Pixar's RenderMan interface, but some modules can also be used for OpenGL programs or non-RenderMan-compliant renderers such as POV-Ray..

PyPov

URL: http://arrowtheory.com/software/python/pypov/

PyPov is a relatively simple framework for making Povray (a popular raytracer) files from Python. It's good for creating structured/mathematical scenes and animations.

Blender

URL: http://www.blender3d.org/

Blender is a very mature and professional cross-platform 3D modeller which has a very large selection of features that comprise a full-featured gaming engine. Blender exposes all its internals through a Python API which allows Python to be used as a scripting facility.

Blender can natively export and import DXF, Inventor and VRML files, but additional exporters and importers already exist as Python scripts or can easily be written in Python and called from within Blender.

Chromium

URL: http://chromium.sourceforge.net/

Chromium is a system for interactive rendering on clusters of graphics workstations. It allows one to implement various parallel rendering techniques such as sort-first and sort-last. Furthermore, Chromium allows filtering and manipulation of OpenGL command streams for non-invasive rendering algorithms.

The configs are created through Python scripts which allow the user to specify different SPUs (stream processing unit). An SPU is the part of a node that intercepts OpenGL calls and performs actions on the OpenGL stream.

Chromium can be used for clustering solutions and connecting multiple displays to form a single large one. Most importantly, it allows the user to intercept OpenGL calls which can be used together with PyOpenGL and lets one exploit its capabilities from within Python.

SciPy

URL: http://www.scipy.com

SciPy is a library of scientific tools for Python. SciPy supplements the popular Numeric module, gathering a variety of high-level science and engineering modules together into a single package.

SciPy includes modules for graphics and plotting, optimization, integration, special functions, signal and image processing, genetic algorithms, ODE solvers, and others.SciPy is not 3D-related in itself. But because 3D graphics programming involves a lot of math Scipy very often comes in handy.

A community site focusing on Python in 3D graphics can be found at http://www.py3d.org/ which provides links to many more projects and bindings.

As you can see, Python is widely used in the 3D graphics domain. Its usage is gradually expanding, which is not surprising as Python is an excellent choice as an integration language.

Hopefully this article has whetted your appetite to explore some of the libraries and tools presented, or at least given you a good overview of the different paradigms available, which will make your decision easier as to which tool to use for your next project.


Tamer Fahmy

shim
shim

 Py is committed to bringing you great Python Articles.

shim
shim


Home   Subscribe   Migration FAQ   Contact PyZine   Write for PyZine   ZopeMag   opensourcexperts.com  

Reproduction of material from any of PyZine's pages without prior written permission is strictly prohibited. Copyright 2003 - 2005 PyZine Zope/Plone hosting by Nidelven IT