5

I would like to load an STL file and produce a set of 2D images in different rotations.

I got the basics working with numpy-stl based on this example, ended up with this code -

from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot

filename = '3001.stl'

# Create a new plot
figure = pyplot.figure()
axes = figure.gca(projection='3d')

# Load the STL files and add the vectors to the plot
mesh = mesh.Mesh.from_file(filename)

axes.add_collection3d(mplot3d.art3d.Poly3DCollection(mesh.vectors, color='lightgrey'))
#axes.plot_surface(mesh.x,mesh.y,mesh.z)
# Auto scale to the mesh size
scale = mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)

#turn off grid and axis from display      
pyplot.axis('off')

#set viewing angle
axes.view_init(azim=120)

# Show the plot to the screen
pyplot.show()

This works well only that I end up with a silhouette of the component, lacking a lot of the detail. the picture below is a lego brick... Lego brick

I tried to highlight the edges. but that is sensitive to how the model was created, which is not great for me.

Lego brick with edges

I was hoping that by adding lighting, the shadows could help add the missing detail but I can't find a way to do that.

Any idea how to add lightsource to the code below to create shadows ?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Yossi Dahan
  • 5,389
  • 2
  • 28
  • 50
  • 2
    Note that matplotlib is not a 3D raytracer. In fact it is often described as 2.5D, because it provides flat projected 3D output. For your application I could imagine that mayavi is much better suited. – ImportanceOfBeingErnest Jul 25 '18 at 11:26
  • Thanks @ImportanceOfBeingErnest - I have read that elsewhere and have tried to install mayavi but could not get it to install with pip; whilst trying i spotteed the LightSource object and a bunch of samples that showed some rather nice 3d rendering so was hoping! – Yossi Dahan Jul 25 '18 at 14:04
  • 1
    Agreed, installing mayavi is a bit of a pain. But I could imagine it to be more productive spending some more time on getting that to run, rather than finding workarounds in a software which is simply limited (with respect to true 3D plotting). – ImportanceOfBeingErnest Jul 25 '18 at 23:22
  • Very true; in my shame :-) I ended up doing this in c# using Open-GL based on this sample code - https://github.com/batu92k/STL-Viewer – Yossi Dahan Jul 25 '18 at 23:25

2 Answers2

10

After getting tired with Mayavi's install disasters I ended up writing my own library for this. https://github.com/bwoodsend/vtkplotlib

Your code would be something like

import vtkplotlib as vpl
from stl.mesh import Mesh

path = "your path here.stl"

# Read the STL using numpy-stl
mesh = Mesh.from_file(path)

# Plot the mesh
vpl.mesh_plot(mesh)

# Show the figure
vpl.show()

If you want the brick to be blue you can replace the mesh_plot with

vpl.mesh_plot(mesh, color="blue")
pullmyteeth
  • 462
  • 5
  • 12
1

If you don't find Mayavi helpful, you could try Panda3D which is intended for graphics/3D rendering applications. I find it quite straightforward for doing simple stuff like this.

L0tad
  • 574
  • 3
  • 15