Skip to main content

Loads SVG files into pyglet Batch objects for OpenGL rendering.

Project description

svgbatch

The latest version of this documentation is online at http://pypi.python.org/pypi/svgbatch

SvgBatch is a pure Python package to load SVG vector graphic files, and convert them into pyglet Batch objects, for OpenGL rendering.

The polygons from the SVG file are tessellated using GLU functions, and used to create a pyglet Batch object of indexed vertex arrays of GL_TRIANGLES. The Batch will aggregate all paths from an SVG file into a single OpenGL primitive for rendering. Each path is also exposed in its untessellated form, so the application could use them for things other than rendering, for example collision detection.

Currently only a subset of SVG is handled - paths forming closed polygons, filled with solid color. Each path may comprise multiple loops (disjoint areas or holes), but must be made up from straight line edges. Arcs, beziers, gradient fills and other SVG entities such as rectangles or text are not currently handled.

Dependencies

Only tested on Windows, but should be cross-platform. Only tested on Python 2.5 and 2.6. Requires the fabulous pyglet.

Status

Nominally complete, and works with some simple SVG files that were generated by Inkscape, but has not yet been used in earnest.

Download

You can install svgbatch to your site-packages directory, making it available for import by any Python programs on your computer. The automated way to do this is to install setuptools, then use the command-line:

easy_install svgbatch

The manual way to do the same thing is to download the source distribution from the bottom of the project PyPI page and install manually, using the command-line:

python setup.py install

Alternatively, the svgbatch package from the above source distribution can be copied directly into your own project, which might be useful if you intend to modify it.

Finally, to get the very latest (possibly broken) version, you might want to check it out from Subversion on Google Code.

Usage

Very straightforward:

svg = SvgBatch('data/logo.svg')
batch = svg.create_batch()

create_batch() returns a pyglet Batch object, which can be rendered in a pyglet program using batch.draw() (see demo.py.)

Your application can access the untessellated geometry of each path, indexed by id, using:

path = svg.path_by_id['pathid']

where pathid is the string ID of the path tag in the SVG file. A path’s ID can be set from within Inkscape by editing its object properties. The returned Path object has the following attributes:

  • id: string, copied from the svg tag’s id attribute

  • color: triple of unsigned bytes, (r, g, b)

  • loops: a list of loops. A loop is a list of vertices representing a closed polygonal boundary (which may be convex.) A vertex is a pair of floats or ints.

  • bounds: an object which provides the path’s axis-aligned extents, as xmin, xmax, ymin and ymax.

  • offset(x, y): adds the given offset to all vertices in the path

Known Issues

  • It generally chokes on real-world SVG files other than the small ones I’m saving from Inkscape, due to unhandled SVG entities such as rectangles or text. Avoid this by just using closed, straight-edged paths filled with plain color.

  • I’ve only tested it on Windows. My lappy graphics chipset doesn’t work for OpenGL stuff on Ubuntu since Jaunty. Thanks ATI.

Plans

See TODO.txt

Acknowledgements

Many thanks to Martin O’Leary of supereffective.org, whose Squirtle module formed a major inspiration for this project, and in particular for his sublime tesselation code, which I have copied wholesale under the terms of the BSD.

This project’s tests use a slightly-modified embedded copy of voidspace’s superbly useful mock library.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

svgbatch-0.1.8.zip (32.5 kB view details)

Uploaded Source

svgbatch-0.1.8.tar.gz (19.7 kB view details)

Uploaded Source

File details

Details for the file svgbatch-0.1.8.zip.

File metadata

  • Download URL: svgbatch-0.1.8.zip
  • Upload date:
  • Size: 32.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for svgbatch-0.1.8.zip
Algorithm Hash digest
SHA256 f2b33748d46f7ca0233363714694fb84f8c872ca60214acd3fb2cb7beb3caf43
MD5 0922fcf63776e2c9991d597e6be00791
BLAKE2b-256 48f3d3bce4c92446c3c5ab64420618616cba5b8d5e8649be86409260a3be5d7f

See more details on using hashes here.

File details

Details for the file svgbatch-0.1.8.tar.gz.

File metadata

  • Download URL: svgbatch-0.1.8.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for svgbatch-0.1.8.tar.gz
Algorithm Hash digest
SHA256 01d5217c4d9c0d3a65b87f0f6247796df526b2a82feba724abe207fb9de98c27
MD5 2f813f36c4edca7bc0fddeec8221879b
BLAKE2b-256 4dff8a89d7134f4845e7b6327c85eefbf186091c2463daa8f7e4457af7024891

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page