That is a useful command. Only drawbacks are that it makes all materials called Custom, Custom (1) Custom (2), etc, So no clear idea as to which layer they below to.
An alternative Rhino script (below) written by a friend recolors all the layer (maybe not ideal) and adds a material based on the layer color and layer name. I may try rewriting it to preserve the layer color (important if you have specific standards), but keep the material named after the layer name.
’ Assign unique layer colors and create/match materials to each layer
Option Explicit
Call Main()
Sub Main()
Dim arrLayers, i, strLayer, clr, matIndex, matName, arrObjects
arrLayers = Rhino.LayerNames()
If IsNull(arrLayers) Then
Rhino.Print "No layers found."
Exit Sub
End If
Rhino.EnableRedraw False
For i = 0 To UBound(arrLayers)
strLayer = arrLayers(i)
' generate a hue around the color wheel (golden-step)
clr = HSVtoRGB((i * 137) Mod 360, 0.75, 0.95)
' set the display color of the layer
Call Rhino.LayerColor(strLayer, clr)
' get existing material index for the layer (-1 means none)
matIndex = Rhino.LayerMaterialIndex(strLayer)
If matIndex = -1 Then
matIndex = Rhino.AddMaterialToLayer(strLayer) ' adds material and assigns it to the layer
End If
If Not IsNull(matIndex) Then
matName = strLayer
Call Rhino.MaterialName(matIndex, matName)
Call Rhino.MaterialColor(matIndex, clr)
Else
Rhino.Print "Failed to add/get material for layer: " & strLayer
End If
' --- OPTIONAL: force all objects on the layer to use the layer material ---
' Uncomment the next lines if you want objects on the layer to be set to "Material from layer"
' arrObjects = Rhino.ObjectsByLayer(strLayer)
' If IsArray(arrObjects) Then
' Call Rhino.ObjectMaterialSource(arrObjects, 0) ' 0 = material from layer
' End If
' -------------------------------------------------------------------------
Next
Rhino.EnableRedraw True
Rhino.Print "Done: layer colors and materials updated."
End Sub
'-----------------------------
’ HSV to RGB helper (h:0-360, s:0-1, v:0-1) → returns VB RGB long
Function HSVtoRGB(h, s, v)
Dim r, g, b, i, f, p, q, t
h = h Mod 360
If s = 0 Then
r = v: g = v: b = v
Else
i = Int(h / 60)
f = (h / 60) - Int(h / 60)
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
Select Case i
Case 0: r = v: g = t: b = p
Case 1: r = q: g = v: b = p
Case 2: r = p: g = v: b = t
Case 3: r = p: g = q: b = v
Case 4: r = t: g = p: b = v
Case 5: r = v: g = p: b = q
Case Else: r = v: g = v: b = v
End Select
End If
HSVtoRGB = RGB(CInt(r * 255), CInt(g * 255), CInt(b * 255))
End Function