From: Daniel Brooks (danielbrooks20_at_gmail.com)
Date: Mon Mar 13 2017 - 16:05:15 CDT

Thanks Joshua, that worked like a charm.

For reference, here's a python script that can generate and export surfaces
to vmd in the .stl format.

#!/usr/bin/env python
import numpy as np
from skimage import measure
from mpl_toolkits.mplot3d import Axes3D
import stl
import matplotlib.pyplot as plt

#Generates isosurfaces from a 3d array of points
#Creates an ascii stl output that can be read by vmd

#Load a 3D array of points
sum_gauss = np.load('array.txt.npy')
print "Loaded sum_gauss from file"

#Now, compute an iso-surface
level = 0.02
print "level =", level
verts, faces = measure.marching_cubes(sum_gauss, level)

#Load the surface in STL format
cube = stl.mesh.Mesh(np.zeros(faces.shape[0], dtype=stl.mesh.Mesh.dtype))
for i, f in enumerate(faces):
  for j in range(3):
    cube.vectors[i][j] = verts[f[j], : ]

#Write the mesh to file in stl ASCII format. This can be read by VMD
cube.save('my_surface.stl', mode=stl.Mode.ASCII)

#Plot the isosurface in python
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(verts[:, 0], verts[:,1], faces, verts[:, 2],
cmap='Spectral', lw=0.1)
plt.show()
plt.savefig('gaussian_load_002.png')

On Mon, Mar 13, 2017 at 9:25 AM, Vermaas, Joshua <Joshua.Vermaas_at_nrel.gov>
wrote:

> Hi Daniel,
>
> The way I'd do it is to go via the STL interface. Its been a while since I
> tried this, but files with the stl extension are automatically picked up to
> be loaded via VMD's STL reader. Note that VMD cannot read binary ascii
> files (http://www.ks.uiuc.edu/Research/vmd/plugins/molfile/stlplugin.html),
> so if you use a library to write out the files, make sure you write out the
> ascii version (https://pypi.python.org/pypi/numpy-stl).
>
> -Josh
>
> On 03/13/2017 02:14 AM, Daniel Brooks wrote:
> Hi all,
>
> I am generating surface meshes that I would like to import into VMD. These
> meshes are stored in python as lists of verts and faces.
>
> It looks like MSMS surface meshes and polygon meshes are supported for
> file import - but I haven't seen documentation on the structure of these
> files. Anyone know how to import surfaces into VMD?
>
> Best,
> -Dan
>
> Daniel Brooks
> PhD Candidate in Applied Physics
> California Institute of Technology
>
>