Black Lives Matter. Please consider donating to Black Girls Code today.

Faulty Functionality or User Error? Changing Color on a Mesh3d plot

I took the code from this community.plotly post. The code translates a 3d object and plots it as a Mesh3d plot.

Although I specify the Mesh3d plot to be lightpink, the plot remains black.

import numpy as np
import plotly.graph_objects as go

# https://plot.ly/~empet/15040/plotly-mesh3d-from-a-wavefront-obj-f/#/
#load objdata
import urllib.request
req = urllib.request.Request('https://raw.githubusercontent.com/MichaelHopwood/asaps-dev-utils/master/Leg_Scan.obj')
try:
    response=urllib.request.urlopen(req)
    obj_data = response.read().decode('utf-8')
except urllib.error.URLError as e:
    print(e.reason)

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)


vertices, faces = obj_data_to_mesh3d(obj_data)

x, y, z = vertices[:,:3].T
I, J, K = faces.T

mesh = go.Mesh3d(
            x=-x,
            y=-y,
            z=z,
            vertexcolor=vertices[:, 3:], #the color codes must be triplets of floats  in [0,1]!!
            i=I,
            j=J,
            k=K,
            name='',
            showscale=False,
            opacity = 0.75,
            color = 'lightpink')

# Lighting festivities
'''
mesh.update(lighting=dict(ambient= 0.18,
                          diffuse= 1,
                          fresnel=  .1,
                          specular= 1,
                          roughness= .1),

            lightposition=dict(x=100,
                               y=200,
                               z=150));
'''

layout = go.Layout(#title='Mesh3d from a Wavefront obj file',
                   #font=dict(size=14, color='black'),
                   #width=900,
                   #height=800,
                   scene=dict(xaxis=dict(visible=False),
                              yaxis=dict(visible=False),
                              zaxis=dict(visible=False),
                              #aspectratio=dict(x=1.5,
                                #               y=0.9,
                                #               z=0.5
                                #         ),
                              camera=dict(eye=dict(x=1., y=1., z=0.5)),
                        ),
                  paper_bgcolor='rgb(235,235,235)',
                  margin=dict(t=175))

Of course, the moment I post, I recognized that vertexcolor was utilized.

That’s the power of stupidity when not writing the code yourself, haha!

For those who may have had the same issue, here’s an excerpt of Mesh3d's vertexcolor documentation.

Sets the color of each vertex Overrides “color”. While Red, green and blue colors are in the range of 0 and 255; in the case of having vertex color data in RGBA format, the alpha color should be normalized to be between 0 and 1.