Material Index -1

I still get index -1 when the material has not yet been assigned to an object but exists in the table, is this intentional?

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).

Index 6 still persists without materials, same result changing ignoreDeletedMaterials argument.

I need to solve this problem (today) somehow to assign my materials to objects, what other (working) method should I use?


EDIT: I have Version 6 SR18 (6.18.19266.14201, 09/23/2019)

@dale can you please help?

Hi @Dani_Abalde,

Is it posslble for you to post your 3dm file?


– Dale

Yes, because the Materials table shows only those that are actually in use.

To iterate over the materials that you can see in the Materials panel you should iterate over the RenderMaterials table of the RhinoDoc.

// 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.