scallop dome pyformex logo

Previous topic

66. vascularsweepingmesher — Vascular Sweeping Mesher

Next topic

68. olist — Some convenient shortcuts for common list operations.

[FSF Associate Member]

Valid XHTML 1.0 Transitional

67. webgl — View and manipulate 3D models in your browser.

This module defines some classes and function to help with the creation of WebGL models. A WebGL model can be viewed directly from a compatible browser (see

A WebGL model typically consists out of an HTML file and a Javascript file, possibly also some geometry data files. The HTML file is loaded in the browser and starts the Javascript program, responsible for rendering the WebGL scene.

Classes defined in module webgl

class webgl.WebGL(name='Scene1')

A 3D geometry model for export to WebGL.

The WebGL class provides a limited model to be easily exported as a complete WebGL model, including the required HTML, Javascript and data files.

Currently the following features are included:

  • create a new WebGL model
  • add the current scene to the model
  • add Geometry to the model (including color and transparency)
  • set the camera position
  • export the model

An example of its usage can be found in the WebGL example.

The created model uses the XTK toolkit from or the modified version of FEops.


Return a dict with the objects in this model.

Returns a dict with the name:object pairs in the model. Objects that have no name are disregarded.


Add the current OpenGL scene to the WebGL model.

This method add all the geometry in the current viewport to the WebGL model.


Add a geometry object to the model.

Currently, two types of objects can be added: pyFormex Geometry objects and file names. Geometry objects should be convertible to TriSurface (using their toSurface method). Geometry files should be in STL format.

The following keyword parameters are available and all optional:

  • obj=: specify a pyFormex Geometry object
  • file=: specify a geometry data file (STL). If no obj is specified, the file should exist. If an obj file is specified, this is the name that will be used to export the object.
  • name=: specify a name for the object. The name will be used as a variable in the Javascript script and as filename for for export if an obj was specified but no file was given. It should only contain alphanumeric characters and not start with a digit.
  • caption=: specify a caption to be used as a tooltip when the mouse hovers over the object.
  • color=: specify a color to be sued for the object. The color should be a list of 3 values in the range 0..1 (OpenGL color).
  • opacity=: specify a value for the opacity of the object (the ‘alpha’ value in pyFormex terms).
  • magicmode=: specify True or False. If magicmode is True, colors will be set from the normals of the object. This is incompatible with color=.
  • control=: a list of attributes that get a gui controller

Add an actor to the model.

The actor’s drawable objects are added to the WebGL model as a list. The actor’s controller attributes are added to the controller gui.


Set the camera position and direction.

This takes two (optional) keyword parameters:

  • position=: specify a list of 3 coordinates. The camera will be positioned at that place, and be looking at the origin. This should be set to a proper distance from the scene to get a decent result on first display.
  • upvector=: specify a list of 3 components of a vector indicating the upwards direction of the camera. The default is [0.,1.,0.].

Export an object in XTK Javascript format

format_gui_controller(name, attr)

Format a single controller


Create the controller GUI script

exportPGF(fn, sep='')

Export the current scene to a pgf file

export(name=None, title=None, description=None, keywords=None, author=None, createdby=False)

Export the WebGL scene.


  • name: a string that will be used for the filenames of the HTML, JS and STL files.
  • title: an optional title to be set in the .html file. If not specified, the name is used.

You can also set the meta tags ‘description’, ‘keywords’ and ‘author’ to be included in the .html file. The first two have defaults if not specified.

Returns the name of the exported htmlfile.

Functions defined in module webgl


Sanitize sloppy settings for JavaScript output

Return properties of an object

properties are public attributes (not starting with an ‘_’) that are not callable.

webgl.surface2webgl(S, name, caption=None)

Create a WebGL model of a surface

  • S: TriSurface
  • name: basename of the output files
  • caption: text to use as caption