Assigning RenderMaterial to Layers with a Python script

Hello,

I am trying to edit the RenderMaterial property of the Layer Class, to automate the creation of layers with a preset material, color, linetype, etc.

I got Layer.Color to work but for some reason I can’t really understand Layer.RenderMaterial() seems to not work.

Not sure what is happening under the hood or where my code is not working. Happy to hear some advices.

doc = Rhino.RhinoDoc.ActiveDoc

render_mat= Rhino.Render.RenderMaterial.CreateBasicMaterial(Rhino.DocObjects.Material())
render_mat.Name="Wood"

existing_mat = [mat.Name for mat in doc.RenderMaterials]

if render_mat.Name not in existing_mat:
    doc.RenderMaterials.Add(render_mat)

def create_layer(df):

    existing_layer = [layer.Name for layer in doc.Layers if layer.IsDeleted == False]

    for i in df:
        if i[1]['LAYER_NAME'] not in existing_layer:

            newlayer=Rhino.DocObjects.Layer()
            newlayer.Name=i[1]['LAYER_NAME']
            newlayer.Id=System.Guid.NewGuid()
            newlayer.RenderMaterial=render_mat

            print(newlayer.RenderMaterial)

            try:
                newlayer.Color= i[1]['RHINO_PEN']
            except:
                newlayer.Color= rs.CreateColor(255, 0, 0)

            doc.Layers.Add(newlayer)

Hi @Tobia_Davanzo,

Please review the following and let me know if you have any questions.

import Rhino
import System
import scriptcontext as sc

def test_layer_with_material():
    
    """
    The properties of a Rhino material (Rhino.DocObjects.Material) define
    what is required to draw an object using OpenGL. Most rendering plug-ins
    require a much richer definition than this. And, not all rendering
    plug-ins support the same material properties. This is where Render
    materials (Rhino.Render.RenderMaterial) come in. A Render material
    is an abstract material - one that is defined by the owning
    rendering plug-in. But a basic Render material does share some
     properties with a Rhino material.
    """
    
    """
    This example demonstrates how to create a basic Render material.
    This material will appear in Rhino's Materials panel. It it called a
    basic material because it does not target any particular rendering plug-in.
    """
    
    # 1. Create a Rhino material
    rhino_material = Rhino.DocObjects.Material()
    rhino_material.Name = "Burly"
    rhino_material.DiffuseColor = System.Drawing.Color.BurlyWood
    
    # 2. Create a basic Render material from the Rhino material
    render_material = Rhino.Render.RenderMaterial.CreateBasicMaterial(rhino_material, sc.doc)
    
    # 3. Add the basic Render material to the document
    sc.doc.RenderMaterials.Add(render_material)
    
    """
    At this point, you will see a Render material appear in Rhino's
    Materials panel. Note, RhinoDoc.Materials.Count will equal 0. This is
    because we have not added a Rhino material. We've only added a
    Render material. When you assign a Render material to an object
    or layer, a compatible Rhino material will be added to RhinoDoc.Materials,
    and this material will be referenced by the Render material.
    """
    
    # 4. Now create a layer with the Render material assigned to it
    layer_index = sc.doc.Layers.Add("Tobia", System.Drawing.Color.BurlyWood)
    if layer_index > 0:
        sc.doc.Layers[layer_index].RenderMaterial = render_material

if __name__ == "__main__":
    test_layer_with_material()

test_layer_with_material.py (2.1 KB)

– Dale

Hi Dale,
thank you very much for your answer.

I tested it and it is all clear.

From what I can see, my code didn’t work because I was treating the property Layer.RenderMaterial as Layer.Name or Layer.Color, so I was trying to apply it before pushing the layer in the document, am I right?

Thanks again!

Correct.

– Dale