Interesting: 3D Wireframes in SVG (via Python)

Discussions about the development of the TechDraw workbench
User avatar
Posts: 7501
Joined: Thu Jan 05, 2017 9:03 pm

Interesting: 3D Wireframes in SVG (via Python)

Postby Kunda1 » Mon Jul 08, 2019 11:31 pm

SVG is great for line art. It scales nicely for high DPI displays without using much bandwidth. However SVG was not designed for 3D, so it does not provide mechanisms for applying perspective transformation or hidden surface elimination.

These limitations can be overcome for simple meshes by baking the perspective transformation, carefully ordering the paths within the SVG document, and paying attention to the winding direction of projected polygons.

In this post I will show how to use Python to generate vector art as seen at the top of the page, including the fully lit 3D Möbius tube.

To see the complete code that I used to generate all the SVG images on this page, go to this GitHub repo.
One example that really caught my eye:
The above scene culls away some of the faces to reveal the inside of the mesh. We draw the sphere in two passes: first backfacing triangles, then frontfacing triangles.

Code: Select all

def backface_shader(face_index, winding):
    if winding >= 0: return None
    return dict(
        fill='#7f7fff', fill_opacity='1.0',
        stroke='black', stroke_linejoin='round',        
        stroke_width='0.001', stroke_dasharray='0.01')

def frontface_shader(face_index, winding):
    if winding < 0 or faces[face_index][0][2] > 0.9: return None
    return dict(
        fill='#7fff7f', fill_opacity='0.6',
        stroke='black', stroke_linejoin='round',

scene = svg3d.Scene()
scene.add_mesh(svg3d.Mesh(faces, backface_shader))
scene.add_mesh(svg3d.Mesh(faces, frontface_shader))
svg3d.Engine([svg3d.View(camera, scene)]).render('sphere_shell.svg')

Article source:
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features