Accessing local Rhino materials library with C#

I’m a beginner trying to make a custom analysis/display component in C#. I’m feeding in a list of breps and assigning them different display materials based on some analysis factors. So far I’m generating the display materials manually with the DisplayMaterial class and giving them different colors. Would there be a simple way to access the default material library present in Rhino? I would like to be able to add simple textures etc. without having to create them from scratch each time.

You mean that you want to inquire what Materials are in the ActiveDoc (and then modify some attributes and create some new ones?).

Like:

Yes! This was exactly what i was looking for. I guess you can use your method for making a DisplayMaterial and feed the table straight into a RenderMaterial constructor to make those.

However when I try to call the material table like in your example it doesn’t return any values. When I check the properties with matTable.Count, it’s always zero. Is there a silly mistake I might be making?

` public DataTree<object> matInfo;

  public void GetMaterials(){
    matInfo = new DataTree<object>();

    Rhino.DocObjects.Tables.MaterialTable matTable = RhinoDoc.ActiveDoc.Materials;
    Print("Materials in Table: {0}", matTable.Count);
    if(!matTable.Any())return;

    for(int i = 0; i < matTable.Count;i++){
      Material mat = matTable[i];
      Color matAC = mat.AmbientColor;
      Color matDC = mat.DiffuseColor;
      string name = mat.Name;

      matInfo.Add(name, new GH_Path(i, 0));
      matInfo.Add(matAC, new GH_Path(i, 0));
      matInfo.Add(matDC, new GH_Path(i, 0));

      Texture[] textures = mat.GetTextures();
      if(textures != null && textures.Length > 0){
        int count = 1;
        foreach(Texture t in textures){
          matInfo.Add(t.FileName, new GH_Path(i, count));
          matInfo.Add(t.TextureType, new GH_Path(i, count));
          count++;
        }
      }
      else Print("Mat : {0} has no texture", name);
    }
  }`

If you run a similar thing and reports 0 Materials … this means that you must define some since Table has none.

Say:

`static Random rnd = new Random();

  public void MakeRndMaterials(int N){
    Rhino.DocObjects.Tables.MaterialTable matTable = RhinoDoc.ActiveDoc.Materials;

    for(int i = 0; i < N;i++){
      Material mat = new Material();
      mat.Name = string.Format("Rnd_Mat_{0}", i);
      mat.AmbientColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
      mat.DiffuseColor = Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256));
      mat.CommitChanges();
      matTable.Add(mat);
    }
  }`

I tried to do the same thing a few weeks ago, and my disappointing conclusion was that I either had to create the materials like shown above, or load them in an empty Rhino document, so the MaterialTable wouldn’t be empty.

There is no way to access the NativeRenderMaterial objects that come with Rhino.

Okay, this is what I was afraid of. The only example I found coming close is accessing them via a file path to the installation directory. Thanks for the deep-dive so I don’t lose any more time over the issue!

Yes, still returns zero. I was making my library same way as in your bottom example. As @magicteddy mentioned, I guess there is no way to access the NativeRenderMaterial library in Rhino?