Export .stl or .obj from 3D spectrogram using python

Not that easy. You need to connect your vertices to a triangle mesh (shape n, 3, 3)
Then you can use mk_stl_mesh to generate and export_stl_mesh to save:

def export_stl_mesh(*args, fileName:str, **kwargs):
        exports geoms mesh as stl file
        call like:  im_ex_port.export_stl_mesh( np.flip(d.vtx[0][d.geoms[0].mesh], -1 ), 
    content = mk_stl_mesh(*args, **kwargs)
    exportPath = os.path.join(sts.mediaPath, 'stls', f"{fileName}.stl")
    with open(exportPath, 'wb') as f:
    print(f"stl saved to exportPath: \n{exportPath}")

def mk_stl_mesh(mesh, *args, **kwargs):
    bounds = f"solid Exported from geometries.py" + '\n'
    content = bounds
    for triangle in mesh.reshape(-1, 3, 3):
        content += ('facet normal 0.000000 0.000000 1.000000' + '\n')
        content += ('outer loop' + '\n')
        for point in triangle:
            content += (f"vertex {' '.join([f'{t:6f}' for t in point])}" + '\n')
        content += ('endloop' + '\n')
        content += ('endfacet' + '\n')
    content += ('end' + bounds)
    return content

I forgot to mention a couple of important points to complete the answer:

  1. There are some variables in the functions like mediaPath which you must change to whatever you want.
  2. The most critical part is not obviously to create the mesh. There is a coding challenge 25 from “the coding train” that can help you there. It’s JavaScript but the idea is the same.
  3. You should use numpy to do this. There is also a numpy.stl library which might have what you need.

Have fun

Read more here: Source link