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
Rhino.DocObjects.Material(Rhino.DocObjects.Material.DefaultMaterial);
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;
myMaterial.SetBumpTexture(texture);
myMaterial.SetTexture(texture,Rhino.DocObjects.TextureType.Bitmap);
myMaterial.SetEnvironmentTexture(texture);
myMaterial.CommitChanges();
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"

Rhino.RhinoApp.RunScript("""-_Materials
_Options
_LoadFromFile
{0}
_Enter
_Enter
""".format(matfile), False)

mats =[m.Name for m in sc.doc.RenderMaterials]
print(mats)
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]
        o.CommitChanges()
    
    sc.doc.Views.Redraw()

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