Edit properties of a material that is assigned to an object [C#]

Hi,

I want to change properties of a material (default material or other rhino material) that is assigned to an object. Especially Reflectivity, Transparency and IndexOfRefraction.

I tried some code but there are not visible changes when rendering:

// First version
Rhino.DocObjects.RhinoObject object = SomeObjRef.Object();
int materialIndex = object.Attributes.MaterialIndex;
Rhino.DocObjects.Material material = MyDocument.Materials[materialIndex];

material.IndexOfRefraction = someIndexOfRefraction;
material.Reflectivity = someReflectivity;
material.Transparency = someTransparency;
material.CommitChanges();

// Second version
Rhino.DocObjects.RhinoObject object = SomeObjRef.Object();
Rhino.DocObjects.Material material = object.GetMaterial();

material.IndexOfRefraction = someIndexOfRefraction;
material.Reflectivity = someReflectivity;
material.Transparency = someTransparency;
material.CommitChanges();

Maybe someone can point me in the right direction, as I found nothing in the forum or in code examples (just how to create and assign a *new material).

Thank you!

I solved the problem by creating a new material, adding a copy of the geometry and deleting the old object. (Just like here http://developer.rhino3d.com/samples/rhinocommon/addmaterial/)

But this seems… wrong as I’m flooding the document with new materials and objects. I have to do this from one to a few hundret times.

Is there a more efficient way?

Edit:
I now create my material and attribute once and then change the properties of this material:

// create once
var mat = doc.Materials.Add();
var attribute = new Rhino.DocObjects.ObjectAttributes();

...
// change properties of material, commit and create new object
Rhino.DocObjects.Material innerMat = Doc.Doc.Materials[InnerGlasMaterialIndex];
mat.Reflectivity = 1.0;
mat.IndexOfRefraction = IndexOfRefraction;
mat.CommitChanges();

var guid = Doc.Doc.Objects.Add(MyObject.Geometry, attributes);
doc.Objects.Delete(MyObject.Attributes.ObjectId, true);

Still, it seems wrong to create a new object whenever I want to change the material. So if theres another way I gladly adjust my code :wink: