Layer name to material name?

Hi. I have 1 question to answer.

I have a lot of “layers” how can I convert “layer” name to “material” name

If you want to rename current materials:

import Rhino
import scriptcontext as sc
def renameLayerMaterial():    
    for layer in sc.doc.Layers:
        if layer.IsDeleted: continue
        material = sc.doc.Materials[layer.RenderMaterialIndex]
        material.Name = layer.Name
        material.CommitChanges()
    sc.doc.Views.Redraw()
if __name__ == '__main__':
    renameLayerMaterial()

If you want to create new materials:

import Rhino
import scriptcontext as sc
def createAndSetLayerMaterial():    
    for layer in sc.doc.Layers:
        if layer.IsDeleted: continue
        material = Rhino.DocObjects.Material()
        material.Default()
        material.Name = layer.Name
        layer.RenderMaterial = material.RenderMaterial
    sc.doc.Views.Redraw()
if __name__ == '__main__':
    createAndSetLayerMaterial()
2 Likes

Oh pretty good. Thank you
And get the sublayer name

image

import Rhino
import scriptcontext as sc
from random import choice
from System import Drawing, Enum
def createAndSetLayerMaterial():    
    for layer in sc.doc.Layers:
        if layer.IsDeleted: continue
        material = Rhino.DocObjects.Material()
        material.Default()
        color_name = choice(Enum.GetValues(Drawing.KnownColor))
        material.DiffuseColor = Drawing.Color.FromKnownColor(color_name)
        material.Name = layer.FullPath
        layer.RenderMaterial = material.RenderMaterial
    sc.doc.Views.Redraw()
if __name__ == '__main__':
    createAndSetLayerMaterial()
1 Like