Skip to main content

Loads SVG files into pyglet Batch objects for OpenGL rendering.

Project description

The latest version of this documentation is online at http://code.google.com/p/svgload

Description

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.

Requires 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 import this package in your own Python projects by installing it to your site-packages directory. If you have setuptools installed, do this automatically with the command-line:

easy_install svgbatch

Alternatively, download the source distribution from the bottom of the project PyPI page:

The svgbatch directory from this can be incorporated as a package directly within your own project’s source, or else installed to your site-packages using:

python setup.py install

Finally, you could 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. A vertex is a pair of floats or ints.

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

  • offset(x, y): a method which will add 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, whos 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 voidspace’s superbly useful mock library.

This project would be naught without the fabulous pyglet.

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.5.zip (21.1 kB view details)

Uploaded Source

svgbatch-0.1.5.tar.gz (14.9 kB view details)

Uploaded Source

File details

Details for the file svgbatch-0.1.5.zip.

File metadata

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

File hashes

Hashes for svgbatch-0.1.5.zip
Algorithm Hash digest
SHA256 c9c51c2fedd5e93eca9635a934a3e781413baf488097a6c99b79a00859450d0c
MD5 eae651ab117d9b7470009dae46f1ca6f
BLAKE2b-256 7bb3084c374cbb9299543a1fee23d6df20c4661d4ba7927a5babc32fee276030

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for svgbatch-0.1.5.tar.gz
Algorithm Hash digest
SHA256 57ec40a29c5beec04ed8865246aa0298dc4c887af038ede0a419f9d766dd71d3
MD5 125ab2e8dcd3ea149f96050c9a33a284
BLAKE2b-256 7dd7e54cb3517996089cafb6e0b33a2b4dcaeb160ed85c7f9856995a90ff9000

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