Rendering 2D images from STL file
Asked Answered
B

2

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 ?

Blocking answered 24/7, 2018 at 22:39 Comment(4)
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.Abdominous
Thanks @Abdominous - 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!Blocking
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).Abdominous
Very true; in my shame :-) I ended up doing this in c# using Open-GL based on this sample code - github.com/batu92k/STL-ViewerBlocking
I
11

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")
Impact answered 14/8, 2019 at 20:18 Comment(0)
O
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.

Odyl answered 18/6, 2019 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.