0:00
/
0:00
Transcript

Parthenon in Plotly

#30DayMapChallenge - Day 27 - Micromapping

import trimesh
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np

# Data source - 3D model purchased on CGTrade
# https://www.cgtrader.com/3d-models/exterior/historic-exterior/parthenon-12a94286-160a-446a-9bee-8a2c560ca7ab


# Load the STL file
your_file_path = "2022-parthenon-ruins-athens-by-miniworld3d.stl"
mesh = trimesh.load_mesh(your_file_path)  # Use the correct variable here

# Extract the vertices and faces
vertices = mesh.vertices
faces = mesh.faces

# Choose a scalar property for coloring (e.g., height (z-axis) of vertices)
z_values = vertices[:, 2]

# Create a custom colormap
cmap = mcolors.LinearSegmentedColormap.from_list("blue_pink", ["#4DFFFD", "#ff6ec7"])

# Normalize z_values for colormap mapping
norm = plt.Normalize(vmin=z_values.min(), vmax=z_values.max())

# Map normalized values to the colormap
colors = (cmap(norm(z_values))[:, :3] * 255).astype(int)  # RGB in 0-255

# Create a Plotly Mesh3d plot with vertex colors
fig = go.Figure(data=[go.Mesh3d(
    x=vertices[:, 0],
    y=vertices[:, 1],
    z=vertices[:, 2],
    i=faces[:, 0],
    j=faces[:, 1],
    k=faces[:, 2],
    vertexcolor=[
        f"rgb({r},{g},{b})" for r, g, b in colors  # Convert RGB to Plotly format
    ],
    opacity=0.95
)])

# Update layout for better visuals
fig.update_layout(
    scene=dict(
        xaxis=dict(title="X Axis", showbackground=True, backgroundcolor="black"),
        yaxis=dict(title="Y Axis", showbackground=True, backgroundcolor="black"),
        zaxis=dict(title="Z Axis", showbackground=True, backgroundcolor="black"),
        aspectmode="data"
    ),
    title="3D Mesh Colored with Blue-Pink Colormap",
    title_font=dict(color="white"),
    margin=dict(l=0, r=0, t=50, b=0),
    paper_bgcolor="black",
    plot_bgcolor="black",
    font=dict(color="white")
)

# Show the plot
output_html = "DAY27.html"
fig.write_html(output_html)
fig.show()

Discussion about this video

User's avatar