How do I get the material index of the material that is being used by a mesh?

Hi !

You see, I have an ON_Mesh object, and was wondering on how to know what material it is using.

A simple thought would be that it would have a material index value, which when used in the Material Table, could give me the material being used.

But, I have been unable to get any methods or fields that denote the material being used by the mesh.

How would I do that?

Thank you!

Hi @alexian007,

The material index is not stored on the geometry, but rather the Rhino object’s attributes:

CRhinoCommand::result CCommandTest::RunCommand(const CRhinoCommandContext& context)
  CRhinoGetObject go;
  go.SetCommandPrompt(L"Select object");
  go.GetObjects(1, 1);
  if (go.CommandResult() != CRhinoCommand::success)
    return go.CommandResult();

  const CRhinoObject* obj = go.Object(0).Object();
  if (nullptr == obj)
    return CRhinoCommand::failure;

  int material_index = -1;

  ON::object_color_source color_source = obj->Attributes().ColorSource();
  if (color_source == ON::color_from_layer)
    const CRhinoLayer& layer = obj->ObjectLayer();
    material_index = layer.RenderMaterialIndex();
  else //if (color_source == ON::color_from_object)
    material_index = obj->Attributes().m_material_index;

  const CRhinoMaterial& material = (material_index < 0)
    ? context.m_doc.m_material_table.DefaultMaterial
    : context.m_doc.m_material_table[material_index];

  // TODO...

  return CRhinoCommand::success;

– Dale

1 Like

Thanks @dale,

I ended up using this, to get the material index:

int materialIndex = currentRhinoMesh.m_parent_object->Attributes().m_material_index;