python – Is there a way to export an stl file from a matplotlib surface plot?

I am analyzing the shape changes of a region of the developing heart in mouse embryos. For that, I study how the spatial coordinates of cells within that tissue change over time.

I am using the function matplotlib.plot_trisurf to obtain the triangulated surface between the coordinates of these cells:

tissue surface

Now I want to calculate the area of this triangulated surface and extract the surface as a .stl file so I can use in other 3D browsers like MeshLab. I don’t know how to do it.

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

##Data, spatial coordinates for each of the cells forming the tissue.

X = np.array([157.68627725, 103.11761198, 157.51873667, 165.81563644,
        77.9816598 , 126.77531671,  90.24263806, 162.27734804,
       122.62725478,  83.2503042 , 162.59702484,  88.3921336 ])

Y = np.array([-174.21831735, -144.38094418, -144.87819434, -181.84569162,
       -116.19711147, -133.53935007, -139.02422794, -141.49550572,
       -137.70714927, -144.36804192, -174.05904052, -120.61181162])

Z = np.array([-40.21972608,  19.97958051, -49.49361177, -69.6049367 ,
       -10.5853926 , -22.06279801,  23.51722221, -72.24126518,
         8.24172533,  42.0251029 , -49.31600354, -16.93026202])

##Plot triangulated surface

fig = plt.figure(figsize = (10, 4.5))
ax = fig.add_subplot(121, projection = '3d')

ax.plot_trisurf(X,Y,Z) 
surface.save('mysurface.stl', ax.plot_trisurf(X,Y,Z))
surface.calculate_area(ax.plot_trisurf(X,Y,Z))

Read more here: Source link