How to import and assign rmtl to object

Hi all. Rhino 7 . I would like to use my rmtl files. How to import and assign to object with common c# ?
I tried with :
Rhino.DocObjects.Material myMaterial = new
myMaterial .Reflectivity = Reflectivity;
myMaterial .Shine = Shine;
myMaterial .Name = Name;
var texture = new Rhino.DocObjects.Texture();
texture.FileName = FileName;
texture.TextureType = Rhino.DocObjects.TextureType.Bitmap;
texture.Enabled = true;
var index= Rhino.RhinoDoc.ActiveDoc.Materials.Add(myMaterial);
attribute.MaterialIndex = index;

I saw that some methods are deprecated but he first issue is that i cannot set the file for environement ( It is ok for color and bump)

I’d use a macro to import the material, then continue from there.

In Python you would do something like this:

import Rhino
import scriptcontext as sc

matfile = r"E:\Downloads\woodything.rmtl"
matname = "woodything"

""".format(matfile), False)

mats =[m.Name for m in sc.doc.RenderMaterials]
filtered_mats = [m for m in sc.doc.RenderMaterials if m.Name==matname]

if len(filtered_mats) > 0:
    the_mat = filtered_mats[0]

    for o in sc.doc.Objects:
        o.Attributes.MaterialSource = Rhino.DocObjects.ObjectMaterialSource.MaterialFromObject
        o.RenderMaterial = filtered_mats[0]

It shouldn’t be too hard to translate the Python code to C#

1 Like

thank you very much for that, i will try it and give my feedback

Hi Nathan, Hi all. I added a control to get existing materials, so if not existing i import it and assign to objects. Thank you very much. It works fine