Color desired mesh faces

Hi every one.
I have a mesh file in Rhino and just want to change the color of some specific mesh faces to red. First I tried to see what is the color of the mesh with:

foreach (Color color in mesh.VertexColors)
{
Debug.WriteLine(“Color is :”);
Debug.WriteLine(color);
}
It shows nothing as if the mesh is colorless. Then I tried to color the whole mesh with:
System.Drawing.Color colors = new System.Drawing.Color[mesh.Vertices.Count];
System.Drawing.Color colors = new System.Drawing.Color[mesh.Vertices.Count];
for (int i = 0; i < mesh.Vertices.Count; i++)
{
colors[i] = System.Drawing.Color.Red;
}
mesh.VertexColors.SetColors(colors);

And then again tried to see if the colors are set with:

foreach (Color color in mesh.VertexColors)
{
Debug.WriteLine(“Color is :”);
Debug.WriteLine(color);
}
It prints the red color at the console but the color of the mesh won’t change in the Render. What should I do to change the color in the render?

Have you replaced the mesh object in the document?

Also, I don’t think mesh vertices’ color will show in Render mode. You may need a material.

Now that you have asked me I saw that I have made an instance of the object into my mesh and add the color to that but since I didn’t want to create another object to my doc, I didn’t add the instance to doc.

Now I just added the mesh to doc and redraw it. The color is applied to the mesh but can not see it in render. How should I create a material?

  1. make sure that the original mesh is not overlapping with the new colored mesh, have you done a replacement?
  2. Using the code below, I can add a quad-face mesh to the document and see it in Shaded and Rendered mode (but not in Raytraced mode)
Mesh m = new Mesh();
m.Vertices.AddVertices(new []{
  new Point3d(0, 0, 0), 
  new Point3d(0, 1, 0), 
  new Point3d(1, 1, 0),
  new Point3d(1, 0, 0)
});

m.Faces.AddFace(0, 1, 2, 3);

m.VertexColors.AddRange(new Color[]{Color.Red, Color.Green, Color.Blue, Color.Gray });
doc.Objects.Add(m);
return Result.Success;

image

Thank you for your reply, since I wanted to make a constrain from multiple parts for mouse click, I appended all the objects to a mesh and used the mesh as a constrain. So I didn’t need any feature from it. Now I want to change the color of some mesh faces so I think I should delete the original objects and add the created mesh to the doc. Right?