Add layer with new material with Rhinocommon

I’m trying to add a new layer with a new material via the Rhinocommon code below, but it isn’t working. Anyone know what I might be missing?

Also, I’m hoping to make the material plastic, the same as in Rhino where you can change the type of the material to plastic.

Thanks,
Sam

 Dim MyLayer = New Rhino.DocObjects.Layer
        Dim index As Integer = doc.Materials.Add()
        Dim mat As Rhino.DocObjects.Material = New Rhino.DocObjects.Material
        mat.AmbientColor = System.Drawing.Color.Blue
        Dim rm As Rhino.Render.RenderMaterial = Rhino.Render.RenderMaterial.CreateBasicMaterial(mat, doc)
        doc.RenderMaterials.Add(rm)
        MyLayer.RenderMaterialIndex = mat.Index
        MyLayer.Name = "MyLayer"
        doc.Layers.Add(MyLayer)

Hi @samlochner,

Perhaps you can translate this to VB:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var color = Color.BurlyWood;

  // Create the layer
  var layer = new Layer
  {
    Name = color.Name,
    Color = color
  };

  // Add the layer to the document
  var layerIndex = doc.Layers.Add(layer);
  if (layerIndex < 0)
    return Result.Nothing;

  // Create a Rhino material
  var rhino_material = new Material
  {
    Name = color.Name,
    DiffuseColor = color
  };

  // Create a basic Render material from the Rhino material
  var render_material = RenderMaterial.CreateBasicMaterial(rhino_material, doc);

  // Add the basic Render material to the document
  doc.RenderMaterials.Add(render_material);

  // Assign the render material to the layer
  doc.Layers[layerIndex].RenderMaterial = render_material;

  return Result.Success;
}

– Dale

3 Likes

Hi Dale,

That worked, thanks.

In Rhino, I often switch the material type to plastic and use the bump texture leather at XXS. How would I go about achieving this or the equivalent in Rhinocommon?

Thanks,
Sam

On my examples site I have an article on how to create a material of a specific type: https://jesterking.github.io/rhipy/create_specific_rendermaterial.html . Essentially use the proper overload of RenderContent.Create Method with RenderMaterial.PlasticMaterialGuid Property.

Thanks Nathan,

Form this page I learnt how to set the material to plastic, thanks.

Next I was trying to implement the script on this page as a first step to getting the leather texture, but I run into a problem saying SimulatedTexture is not a member of RenderContent:

Basically, I’m trying to apply the standard leather with XXS scale as done in Rhino in below image. I’m thinking the leather texture file is somewhere in Rhino and I have to figure out where to access it. If there is a quick way to set this up, would really like to know:

Thanks,
Sam

RenderContent is the base class for RenderTexture. The method NewContentFromTypeId gives you a RenderContent, so you have to cast to RenderTexture.

I’ll check the other bits later, probably tomorrow, it is getting soon bed time (:

I recorded a short video on how to find what you can change in Rhino materials. In short: save out the material to file, open it to inspect. Take note of the parameters you are interested in and then use RenderContent.SetParameter Method (String, Object) on your material instance, properly bracketed by RenderContent.BeginChange Method and RenderContent.EndChange Method

I hope this helps.

Thanks Nathan! A video is worth a thousand pictures, really learnt some problem solving skills from your approach. And of course got my answer to this question too. Enjoyed the beats too and that is one funky keyboard you are using :slight_smile:

Thanks again,
Sam

1 Like

I’m glad the video was useful. Keep asking questions, because that is how we know where people want or need to learn!

Showing my own approach is the reason why I started doing my Rhino live coding streams. Naturally it is not the only way, but one that helps me a lot. I also found that having calm musical background also helps watching this type of videos and streams.

And naturally I love my keyboard, that is why I have posted about it in Meta and the Gallery (: