pythonplot3dplotlywavefront

Plotly Mesh3d plot from a Wavefront OBJ file


I am trying to plot a 3D scan of a leg using Plotly's Mesh3D.

I have used scatter_3d with the XYZ points to show this concept using:

fig = px.scatter_3d(df, x='x', y='y', z='z', opacity = 0.8)

Plotly 3D Scatter Plot of XYZ points

However, it does not look like a surface. Therefore, I tried Mesh3d, using:

fig = go.Figure(data=[go.Mesh3d(x=x, y=y, z=z, color='lightpink', opacity=0.50)])

Plotly 3D Mesh of XYZ points

Obviously, this plot is not smooth. I've tried to sort the df before rendering the plots but it did not help.

To reiterate, I am looking for a smooth surface plot of this XYZ data.

Here is the scan's XYZ data.

Edit: Continued Information on Introduction of Surface Plot

I implemented the Surface plot with the code below. Unfortunately, no plot is rendered (no error is accompanied, either).

colnames = ['x', 'y', 'z']
df = pd.read_csv('sandbox\leg.txt', sep = ' ', header = None, names = colnames)
x, y = np.array(df['x'].tolist()), np.array(df['y'].tolist())
df2 = df.pivot(index = 'x', columns = 'y', values = 'z')
z = df2.values
fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])
fig.show()

Solution

  • I found a fantastic answer here: https://plot.ly/~empet/15040/plotly-mesh3d-from-a-wavefront-obj-f/#/

    The author used go.Mesh3d. But, perhaps the more important breakthrough was their function:

    def obj_data_to_mesh3d(odata):
        # odata is the string read from an obj file
        vertices = []
        faces = []
        lines = odata.splitlines()
    
        for line in lines:
            slist = line.split()
            if slist:
                if slist[0] == 'v':
                    vertex = np.array(slist[1:], dtype=float)
                    vertices.append(vertex)
                elif slist[0] == 'f':
                    face = []
                    for k in range(1, len(slist)):
                        face.append([int(s) for s in slist[k].replace('//','/').split('/')])
                    if len(face) > 3: # triangulate the n-polyonal face, n>3
                        faces.extend([[face[0][0]-1, face[k][0]-1, face[k+1][0]-1] for k in range(1, len(face)-1)])
                    else:
                        faces.append([face[j][0]-1 for j in range(len(face))])
                else: pass
    
    
        return np.array(vertices), np.array(faces)
    

    Here is the final plot:

    enter image description here