Assign colors to mesh vertices via C#

Hi guys,
what is the correct way to assign colors programmatically to an Autodesk.DesignScript.Geometry,Mesh object? The class does not have have an appropiate method for that.

any hints here would be great.

Theres no real ability to do this to the Mesh Object - you can do it to the render geometry though of any mesh which is displayed in the background preview using IGraphicItem interface:

I guess the real question is what you want to do with this mesh and the vertex colors? If you just need to associate them… just keep two lists (one of verts and one of colors)

1 Like

Hi @Michael_Kirschner2

I tried to implement your example, but nothing is displayed.

Here is the code

    public class MeshWrapper : IGraphicItem
    {

         Vector[] m_normals;
         Autodesk.DesignScript.Geometry.Point[] m_vertices;
         Color[] m_vertexColors;

        [IsVisibleInDynamoLibrary(false)]
        public MeshWrapper(Vector[] normals, Autodesk.DesignScript.Geometry.Point[] vertices, Color[] vertexColors)
        {
            m_normals = normals;
            m_vertices = vertices;
            m_vertexColors = vertexColors;
        }
    
        [IsVisibleInDynamoLibrary(false)]
        public void Tessellate(IRenderPackage package, TessellationParameters parameters)
        {
            package.RequiresPerVertexColoration = true;
            AddColoredQuadToPackage(package);
        }


        private  void AddColoredQuadToPackage(IRenderPackage package)
        {
            for (int i = 0; i < m_vertices.Length; i++)
            {
                package.AddTriangleVertex(m_vertices[i].X, m_vertices[i].Y, m_vertices[i].Z);
                package.AddTriangleVertexColor(m_vertexColors[i].R, m_vertexColors[i].G, m_vertexColors[i].B, m_vertexColors[i].A);
                package.AddTriangleVertexNormal(m_normals[i].X, m_normals[i].Y, m_normals[i].Z);
                package.AddTriangleVertexUV(0, 0);
            }
        }

    }

And here is where I construct a MeshWrapper object from an Autodesk...Mesh. I will ommit some detials of the method for clarity.

 public static List<object> Receive(string id)
        {
            List<object> outPut = new List<object>();

            RequestData rd = new RequestData();

            try
            {
                DataContainer dataContainer = rd.RequestDataLocal(id);

                foreach (var item in dataContainer.Data)
                {
                    if (item.Value.Node is Parasite_Mesh mesh)
                    {
                        if (mesh.VertexColors == null)
                            outPut.Add(DynamoConversion.ToDynamoType(mesh));
                        else
                        {
                            Mesh m = DynamoConversion.ToDynamoType(mesh);

                            MeshWrapper wrapper = new MeshWrapper(m.VertexNormals, m.VertexPositions, mesh.VertexColors);

                            outPut.Add(wrapper);
                        }

                    }

            /// etc .....

                }



            }

            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return outPut;
        }

    }

The Receive method is exposed as a Dynamo node

Any hints would be great… again. Thank you for answering my questions.

Nicholas

can you confirm that when nothing is displayed that the tessellate method is called and your data is not null?

You should enable all exceptions, even handled ones in visual studio - as there is a try catch around package generation and this will hide exceptions from you.

FYI:

I removed the try catch statements I didn’t really need them anyways. Yes, I confirmed that all fields were not null. I also went through the debugger with the example file you provided, as well as with mine, and they both looked the same. Attached the screen shots.

  1. MeshWrapper class showing fields with values.

  2. MeshWrapper class showing properties in package object after Tesselate method returns

  3. Example file showing values in package object after Tesselate method returns

sorry, I did not mean your try catch - I mean there is a try catch in Dynamo’s rendering code.

When you build and run the DynamoSample Color example node, does it produce colored geometry?
Can you verify you see any geometry in the background preview - just using normal nodes?

Yes, the example node does produce a colored mesh and a colored line

okay, so we are on the right track!
does your node return your MeshWrapper class into the graph as an output?

Yes it does

It technically returns a List<object> then at runtime the compiler does the appropriate casting… So I don’t know if this might be causing some issues down the line .