Writing Polydata including its colour to file

Hi everyone,
I was wondering how one could write a polydata including its colour to file. For instance I understand that polydata’s do not intrinsically hold information about their colour, well unless one includes this in its field data i.e the point data. So what I do is tag the colour as an array (vtkDoubleArray) and set that as the scalar for the point data.

   const char* colourString = "colour";
   vtkSmartPointer<vtkDoubleArray> colour = vtkSmartPointer<vtkDoubleArray>::New();

    //... loop & set values
    colour->InsertNextTuple3(r,g,b); // r, g, b are doubles


The problem is when I try writing the file out and loading the result in for example Paraview I don’t see the expected colour. I’m trying the write the files in formats such as stl (using vtkSTLWriter), ply (using vtkPLYWriter) and lastly vtk using just vtkPolyDataWriter. Can someone help me out with this?

STL cannot store color in a standard way. Some software save a solid color in the file comment, which you can implement in your application.

PLY file format supports point and cell colors and VTK can read and write them.

VTK/VTP format can save any number of point and cell arrays that can be used for coloring the mesh.

I figured out how to export the colours in .ply, and you are right it seems stl doesn’t support storing colours. I was wondering though, if colours are saved in a Polydata’s field data to be used for colouring a mesh, if such Polydata is written to file, is the colour also written - I understand that to colour the mesh all one needs to do is retrieve the colours saved in it but I don’t quite see how this colour data is saved to file (I tried this when exporting to .vtk and on loading the exported file in Paraview - there seems to be no colour).

Color is saved as point or cell data array. You can save multiple arrays and if you load it into Paraview, 3D Slicer, etc. then you need to choose which one you want to use for coloring and how (color map, range, etc).