Mesh face color maping

Hi there,

I am trying to find out whether Rhino.python has any functionality for different mesh face color mapping. Specifically, I have several triangulated surfaces and I would like to calculate gap distances from a reference triangulated surface. For each input surface I project the central point of each mesh triangle to the reference surface along the normal direction and I save the euclidean distance in a numpy array. Is there any way to map a color range according to any calculated distance on each input triangulated surface?

Thank you very much in advance.

Hi namesake,

The way you color the mesh is by setting color for each mesh vertex. The rs.MeshVertexColors() function is what you need.
I do not have numpy module installed, but this example might help you with your issue. It calculates the distance from a point to all mesh vertices, then determines the red color based on that distance:

import rhinoscriptsyntax as rs

mesh_id = rs.GetObject("Pick up mesh", 32, True)
point_id = rs.GetObject("Pick up the point", 1, True)

meshVertices = rs.MeshVertices(mesh_id)
distances = []
for mVert in meshVertices:
    distances.append(rs.Distance(mVert, point_id))

oldMin = distances[0]
oldMax = distances[1]
for dist in distances:
    if dist > oldMax:
        oldMax = dist
    elif dist < oldMin:
        oldMin = dist
oldDomain = (oldMax - oldMin)
newDomain = 255

colors = []
for dist in distances: 
    colorR =  ( ((dist - oldMin) * newDomain) / oldDomain )
    colorG = 200
    colorB = 0
    colors.append([colorR, colorG, colorB])

rs.MeshVertexColors(mesh_id, colors)

1 Like

Hi sakename,

Thanks a lot for your instant response.

I used the vertices instead and I now have what I need.

By the way, for better interpretation, is there any way for displaying next to my colored meshes the color range according to the distance value? Moreover, is there any tutorial that you could suggest to understand how should I edit the colors? Since I have very small distance differences between the sampled vertices and the projected points I would like the color mapping to be more sensitive to small changes.

Anyway, thanks a lot for you help.

There’s no Python tutorial, at least not to my knowledge.
Do not worry about the small distance differences - values get remapped according to the full spectrum of a single color (from its 0 to 255 value).

Check the attached function.
It will allow you to choose three types of gradient colorings, and the origin of your color range (legend).

mesh_pt_distance_gradient.py (4.7 KB)
mesh_pt_distance_gradient.3dm (128.9 KB)

1 Like

That looks fantastic, djordje. Thanks a lot for your help.

Hi Djordje.
Your explanation is fantastic. Though I still have a doubt.
You measured distances between points, and therefore each vertex had a value to remap to colors, but…
what happens when I try to evaluate face normals, for example. There is a different number of faces than of vertices and I evaluating the faces in each vertex (if possible) would give a different value…

Hi,

Check the attached script.
mesh_normals_angle_gradient.py (5.4 KB)
It will measure the angle between the mesh face normal and its projection to the XY plane.

Another one will convert the mesh face normal to a color, but in that case a legend can not be created, as there is no criteria for it.
mesh_normals_gradient.py (885 Bytes)

1 Like