How to set material to the face of the Brep by api?

In rhino we can set material to every face of the Brep easily,but how to do this by api?

I want to set material not a color

Oops, sorry. I don’t know how to do that

Hi @duzhengjie1,

Just set BrepFace.MaterialChannelIndex with the rendering material channel index of your choice.

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var rc = RhinoGet.GetOneObject("Select Brep", false, ObjectType.Brep, out var objref);
  if (rc != Result.Success || null == objref)
    return rc;

  if (!(objref.Object() is BrepObject brep_object))
    return Result.Failure;

  // Create red material
  var red_material = new Material { DiffuseColor = Color.Red };
  var red_material_index = doc.Materials.Add(red_material);

  // Create blue material with red material on one channel
  var blue_material = new Material { DiffuseColor = Color.Blue };
  var red_channel_index = blue_material.MaterialChannelIndexFromId(doc.Materials[red_material_index].Id, true);
  var blue_material_index = doc.Materials.Add(blue_material);

  // Change object material to blue material
  brep_object.Attributes.MaterialIndex = blue_material_index;
  brep_object.Attributes.MaterialSource = ObjectMaterialSource.MaterialFromObject;

  // Set every second face material to red material channel
  var brep = brep_object.BrepGeometry;
  for (var i = 0; i < brep.Faces.Count; i++)
  {
    if (i % 2 == 0)
      brep.Faces[i].MaterialChannelIndex = red_channel_index;
    else
      brep.Faces[i].ClearMaterialChannelIndex();
  }

  // Commit changes and redraw
  brep_object.CommitChanges();
  doc.Views.Redraw();

  return Result.Success;
}

– Dale

1 Like

Ok,thanks very much!I have did it.