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).

Hi Andras,
Could you kindly clarify the following

  • that there’s no exporting colours with .STL
  • are .PLY colours only exported uniformly (based on the API), one can’t individually provide colours per point

regarding exporting colours in general, do I just attach the colour array to the polydata as a PointData field i.e
const char* colourString = "colour";
vtkUnsignedCharArray* colour = vtkUnsignedCharArray::New();
..... colour set up .....
would this suffice.

See https://en.wikipedia.org/wiki/STL_(file_format):

STL files describe only the surface geometry of a three-dimensional object without any representation of color, texture or other common CAD model attributes.

VTK can read/write point colors in .ply format. See documentation and source code for details.

If you write to .vtk or .vtp format then all arrays are saved, so your code will work. If you want to save it to .ply then you need to set the array name where you store the color. If you read .ply then colors are saved in to the “RGBA” array. Check documentation, examples, tests, and source code for details.

I’ll take a look at the link and suggested docs.