# how to write .xml .vtk or vtp with color?

**URL:** https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731
**Category:** Support
**Created:** [March 2, 2020, 4:41am UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731 "2020-03-02T04:41:47Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![ShaneKirkbride](https://discourse.vtk.org/user_avatar/discourse.vtk.org/shanekirkbride/32/1392_2.png) [@ShaneKirkbride](https://discourse.vtk.org/u/ShaneKirkbride)
#### Post date: [March 2, 2020, 4:41am UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/1 "2020-03-02T04:41:48Z")

</div>

I’m trying to save a polyData mesh with color.

I set the color like this:

```
		// Generate the colors for each point based on the color map
		vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
		colors->SetNumberOfComponents(3);

colorLookupTable->GetColor(val[i], dcolor);

colors->InsertNextTupleValue(color);

registeredMesh->GetPointData()->SetScalars(colors);

```

I know this is working because I can view the result and it looks colored.

I go to save the mesh like this:

```
			vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
			writer->SetFileName(filename.c_str());
			writer->SetInputData(mesh);
			writer->Update();
			writer->Write();

```

and it is not written correctly. When I don’t add color it writes the mesh fine but I need the color in my file.  
Any thoughts or ideas on what to do?  
Thanks,  
~Shane

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [March 2, 2020, 4:50am UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/2 "2020-03-02T04:50:27Z")

</div>

Can you share the outputed file ?

---

<div class="post-metadata">

### Author: ![ShaneKirkbride](https://discourse.vtk.org/user_avatar/discourse.vtk.org/shanekirkbride/32/1392_2.png) [@ShaneKirkbride](https://discourse.vtk.org/u/ShaneKirkbride)
#### Post date: [March 2, 2020, 4:57am UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/3 "2020-03-02T04:57:15Z")

</div>

[phase\_2.xml](https://discourse.vtk.org/uploads/short-url/7MmHo6N9f6vjSU52hhoquMzcIxl.xml) (1.7 MB)

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [March 2, 2020, 4:58am UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/4 "2020-03-02T04:58:14Z")

</div>

This is not a .vtp file.

---

<div class="post-metadata">

### Author: ![ShaneKirkbride](https://discourse.vtk.org/user_avatar/discourse.vtk.org/shanekirkbride/32/1392_2.png) [@ShaneKirkbride](https://discourse.vtk.org/u/ShaneKirkbride)
#### Post date: [March 2, 2020, 4:58am UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/5 "2020-03-02T04:58:59Z")

</div>

yes, sorry I put the wrong file there initially.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [March 2, 2020, 5:00am UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/6 "2020-03-02T05:00:34Z")

</div>

Are `mesh` and `registeredMesh` the same var ?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [March 2, 2020, 5:39am UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/7 "2020-03-02T05:39:28Z")

</div>

We can try to guess from these code snippets - for example you have not set a name for the array, which makes the data set invalid - but we don’t know if you set the name somewhere just did not include that part in the post or you have actually did not set the name.

If you provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) then we would see what’s happening and give informed advice. There is also a high chance that as you are creating the example, you find the root cause of the problem by yourself.

Another good strategy is to check out [VTK examples](https://lorensen.github.io/VTKExamples/site/). From the hundreds of examples you can almost always find code snippets that do exactly what you need. Download the entire VTK source tree and the whole VTKExamples repository and search for `SetScalars` in all text. I’m sure you’ll find dozens of complete, working examples and tests.

---

<div class="post-metadata">

### Author: ![ShaneKirkbride](https://discourse.vtk.org/user_avatar/discourse.vtk.org/shanekirkbride/32/1392_2.png) [@ShaneKirkbride](https://discourse.vtk.org/u/ShaneKirkbride)
#### Post date: [March 2, 2020, 12:55pm UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/8 "2020-03-02T12:55:48Z")

</div>

@lassoan, @mwestphal Here is an example, it is a modified version of the triangleColorPoints example.  
However, as you suspected I found the issue; thanks for the advice

```
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkCellArray.h>
#include <vtkTriangle.h>
#include <vtkUnsignedCharArray.h>

#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>

// For compatibility with new VTK generic data arrays
#ifdef vtkGenericDataArray_h
#define InsertNextTupleValue InsertNextTypedTuple
#endif
#include <vtkGLTFExporter.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkNamedColors.h>

int writeGLTF(std::string filename);

int main(int, char *[])
{
  // Setup points
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(0.0, 0.0, 0.0);
  points->InsertNextPoint(0.0, 1.0, 0.0);

  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 1.0, 0.0);
  points->InsertNextPoint(0.0, 1.0, 0.0);
  
  // Define some colors
  unsigned char red[3] = {255, 0, 0};
  unsigned char green[3] = {0, 255, 0};
  unsigned char blue[3] = {0, 0, 255};

  unsigned char other[3] = { 255, 0, 255 };

  // Setup the colors array
  vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
  colors->SetNumberOfComponents(3);
  colors->SetName("Colors");

  // Add the three colors we have created to the array
  colors->InsertNextTupleValue(green); 
  colors->InsertNextTupleValue(blue);
  colors->InsertNextTupleValue(red);
  
  colors->InsertNextTupleValue(red);
  colors->InsertNextTupleValue(blue);
  colors->InsertNextTupleValue(green);
  
  // Create a triangle
  vtkSmartPointer<vtkCellArray> triangles = vtkSmartPointer<vtkCellArray>::New();
  vtkSmartPointer<vtkTriangle> triangle1 = vtkSmartPointer<vtkTriangle>::New();
  triangle1->GetPointIds()->SetId(0, 0);
  triangle1->GetPointIds()->SetId(1, 1);
  triangle1->GetPointIds()->SetId(2, 2);
  triangles->InsertNextCell(triangle1);

  vtkSmartPointer<vtkTriangle> triangle2 = vtkSmartPointer<vtkTriangle>::New();
  triangle2->GetPointIds()->SetId(0, 3);
  triangle2->GetPointIds()->SetId(1, 4);
  triangle2->GetPointIds()->SetId(2, 5);
  triangles->InsertNextCell(triangle2);

  // Create a polydata object and add everything to it
  vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
  polydata->SetPoints(points);
  polydata->SetPolys(triangles);
  polydata->GetPointData()->SetScalars(colors);
  
  std::string filename = "outputFile.xml";
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName(filename.c_str());
  writer->SetInputData(polydata);
  writer->Update();
  writer->Write();

  writeGLTF(filename);

  // Visualize
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputData(polydata);

  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);

  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

int writeGLTF(std::string filename) {

    vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();
    vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    auto renderer = vtkSmartPointer<vtkRenderer>::New();
    auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();

    std::string inputFileName = filename; //phase_1.xml";

        // Read all the data from the file
        reader->SetFileName(inputFileName.c_str());
        reader->Update();
        mapper->SetInputConnection(reader->GetOutputPort());
        actor->SetMapper(mapper);

        renderer->AddActor(actor);
        renderer->SetBackground(colors->GetColor3d("Teal").GetData());
        renderer->ResetCamera();
        renderWindow->AddRenderer(renderer);
        renderWindow->SetSize(600, 600);
        renderWindow->Render();
        renderWindow->SetWindowName("ReadPolyData");

        //**Don't Delete use this if ever .obj is needed.
        //auto exporter = vtkSmartPointer<vtkOBJExporter>::New();
        //std::stringstream comment;
        //comment << "Converted by ImportExport from " << FileName;
        //exporter->SetOBJFileComment(comment.str().c_str());
        //exporter->SetMTLFileComment(comment.str().c_str());
        //exporter->SetActiveRenderer(renderer);
        //exporter->SetRenderWindow(renderWindow);
        //exporter->SetFilePrefix("0_");
        //exporter->Update();
        //exporter->Write();

        std::string exportFileName;
        int check;

        exportFileName = std::string("outputGLTFFile") + "." + "gltf";
        auto exporter = vtkSmartPointer<vtkGLTFExporter>::New();
        exporter->SetFileName(exportFileName.c_str());
        exporter->SetActiveRenderer(renderer);
        exporter->SetRenderWindow(renderWindow);
        std::cout << "Writing " << exportFileName << std::endl;
        exporter->Update();
        exporter->Write();

    return EXIT_SUCCESS;
}
```

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [March 3, 2020, 3:58am UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/9 "2020-03-03T03:58:22Z")

</div>

Thanks for the update. What was the issue in your original code?

---

<div class="post-metadata">

### Author: ![ShaneKirkbride](https://discourse.vtk.org/user_avatar/discourse.vtk.org/shanekirkbride/32/1392_2.png) [@ShaneKirkbride](https://discourse.vtk.org/u/ShaneKirkbride)
#### Post date: [March 4, 2020, 6:30pm UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/10 "2020-03-04T18:30:43Z")

</div>

I was not referencing the mesh correctly _face-palm_

---

<div class="post-metadata">

### Author: ![devngc](https://discourse.vtk.org/user_avatar/discourse.vtk.org/devngc/32/2750_2.png) [@devngc](https://discourse.vtk.org/u/devngc)
#### Post date: [July 9, 2021, 2:59pm UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/11 "2021-07-09T14:59:45Z")

</div>

Hi All,

I am interested in writing vtkActors to a file. I found this post to be related. Is it possible to write vtkActors to a file?

I am interested in changing colors and visual styles (wireframe, surface, etc.) before I write them. I apply such effect to vtkActors.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [July 14, 2021, 4:59pm UTC](https://discourse.vtk.org/t/how-to-write-xml-vtk-or-vtp-with-color/2731/12 "2021-07-14T16:59:54Z")

</div>

You can use one of [VTK exporter classes](https://vtk.org/doc/nightly/html/classvtkExporter.html).

Note that you will have a much higher chance that your question will be answered (and faster) if you create a new topic instead of adding extra questions to an old, somewhat related topic. Having shorter, more focused topics is also better for everyone who reads discussions. You can of course link add links to relevant earlier discussions in your topic if you think that those are useful.
