Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Plotting many mesh3ds while still retaining ability to modify color

Hi,

I’m using the plotly (plotly_4.7.1) within R 3.2.2. I have many .obj files that I would like to plot all within the same plot using the mesh3d type. I would also like to color each of the different mesh surfaces in colors I assign via a colorbar. All vertices of a given obj file turned into a plotted mesh should be the same color.

It seems that when doing this, I have to first hack in the full range of colors into a given mesh (just picking the first two vertices) and then choose my color of interest for the rest of the vertices of the mesh. If I don’t do this, then it seems plotly gets confused and does not use the appropriate color for the vertices. However, when I do this hack then I get two vertices next to each other that do not have the color that I want.

Any ideas on how to fix this? I would like all vertices within a given mesh to be colored according to a value I assign in the intensity field and that value to match what the colorbar should hold.

Thanks,
Jason

for (i in 1:length(objs)) {
obj = objs[[i]];
##If it is the first plot, set up the mesh
if (i==1) {
p = plot_ly(type = ‘mesh3d’,
x = obj$shapes[[1]]$positions[1,],
y = obj$shapes[[1]]$positions[2,],
z = obj$shapes[[1]]$positions[3,],
i = obj$shapes[[1]]$indices[1,],
j = obj$shapes[[1]]$indices[2,],
k = obj$shapes[[1]]$indices[3,],
##for two of the vertices, do the whole range of colors (you can’t see the in the plots, but without it I couldn’t get the color ranges to work)
intensity = c(minZ,maxZ,rep(Zforthisobj[i],length(obj$shapes[[1]]$positions[1,])-2)),
color = seq(minZ,maxZ,length=50),
colors = colorRampPalette(c(“darkred”,“grey”,“midnightblue”))(50),
showscale=TRUE)
} else {
p = add_mesh(p = p,
x = obj$shapes[[1]]$positions[1,],
y = obj$shapes[[1]]$positions[2,],
z = obj$shapes[[1]]$positions[3,],
i = obj$shapes[[1]]$indices[1,],
j = obj$shapes[[1]]$indices[2,],
k = obj$shapes[[1]]$indices[3,],
intensity = c(minZ,maxZ,rep(Zforthisobj[i],length(obj$shapes[[1]]$positions[1,])-2)),
color = seq(minZ,maxZ,length=50),
showscale=FALSE)
}
}
p

Ended up figuring it out based on a previous post: Plotly - different color scales for multiple surfaces

I think the basic is that you can’t use the color argument and instead use these 3

                    cauto=FALSE,
                    cmax=maxZ,
                    cmin=minZ,