Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

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

$
0
0

@mhop wrote:

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))

Posts: 2

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles