The document had 7 materials (created from Matrix) that I deleted, then I add from code many other materials (that’s why the index 361 appears) but I don’t understand why the index 6 persists (it should be 360).
// to use this snippet ensure you `use System.Linq;` at the top of your script as well...
private void RunScript(string mname, ref object index) {
var mlist = (from x in RhinoDoc.ActiveDoc.RenderMaterials select x).ToList();
int idx = mlist.FindIndex(mat => mat.Name.Equals(mname));
index = idx;
}
Note that the indices in the Materials table can be awkward, as well as the count of entries. Say you have an empty document and add one material in the Materials panel. RhinoDoc.ActiveDoc.Materials will have a count of 0 (RenderMaterials count will be 1). Now add box and assign the material to it. Materials table will have now count 1. Add another box and assign the one material to it. Materials will now have a count of 2.
The Materials table is essentially a list of all material instances in use.
The RenderMaterials table lists all RenderMaterials in the document.