Skip to content
generate 3D wireframes as vector art
Branch: master
Clone or download
Latest commit 75e9195 Jul 17, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Add pycache to gitignore. Jul 15, 2019
LICENSE Add MIT license etc. Jul 7, 2019
Pipfile Lots of fixups. Jul 8, 2019
Pipfile.lock Lots of fixups. Jul 8, 2019
README.md Fix Z clipping and example code. Jul 15, 2019
example.py Fix Z clipping and example code. Jul 15, 2019
gallery.html Add overlapping triangles test. Jul 13, 2019
svg3d.py Minor cleanup. Jul 17, 2019
test.py Fix Z clipping and example code. Jul 15, 2019

README.md

This is a single-file Python library for generating 3D wireframes in SVG format.

For a description of how the library was designed and implemented, check out this blog post.

Usage example

import numpy, svg3d, pyrr, math

def get_octahedron_faces():
    f = math.sqrt(2.0) / 2.0
    verts = numpy.float32([ ( 0, -1,  0), (-f,  0,  f), ( f,  0,  f), ( f,  0, -f), (-f,  0, -f), ( 0,  1,  0) ])
    triangles = numpy.int32([ (0, 2, 1), (0, 3, 2), (0, 4, 3), (0, 1, 4), (5, 1, 2), (5, 2, 3), (5, 3, 4), (5, 4, 1) ])
    return 15.0 * verts[triangles]

def generate_svg(filename):
    view = pyrr.matrix44.create_look_at(eye=[50, 40, 120], target=[0, 0, 0], up=[0, 1, 0])
    projection = pyrr.matrix44.create_perspective_projection(fovy=15, aspect=1, near=10, far=200)
    camera = svg3d.Camera(view, projection)

    style = dict(
        fill="white", fill_opacity="0.75",
        stroke="black", stroke_linejoin="round", stroke_width="0.005")

    mesh = svg3d.Mesh(get_octahedron_faces(), style=style)
    view = svg3d.View(camera, svg3d.Scene([mesh]))
    svg3d.Engine([view]).render(filename)

generate_svg("octahedron.svg")

The above code snippet generates an image like this:

Running the test script

pipenv shell
pipenv install
./test.py
open gallery.html
You can’t perform that action at this time.