# PolyData face level attributes ?

**URL:** https://discourse.vtk.org/t/polydata-face-level-attributes/10009
**Category:** Support
**Created:** [November 29, 2022, 11:56pm UTC](https://discourse.vtk.org/t/polydata-face-level-attributes/10009 "2022-11-29T23:56:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [November 29, 2022, 11:56pm UTC](https://discourse.vtk.org/t/polydata-face-level-attributes/10009/1 "2022-11-29T23:56:45Z")

</div>

I wish to produce a VTK polydata (eventually write it out as a \*.vtp file) where I can associate some scalar value (e.g. pH, temperature, salinity) on a per face level, how should I go about doing that ? Is there an example that has something similar I can learn from?

I intend to load the VTP file in ParaView for visualization.

Cheers

---

<div class="post-metadata">

### Author: ![Christos\_Tsolakis](https://discourse.vtk.org/user_avatar/discourse.vtk.org/christos_tsolakis/32/2076_2.png) [@Christos\_Tsolakis](https://discourse.vtk.org/u/Christos_Tsolakis)
#### Post date: [November 30, 2022, 2:03am UTC](https://discourse.vtk.org/t/polydata-face-level-attributes/10009/2 "2022-11-30T02:03:22Z")

</div>

you need to add “Cell data” attributes . Check the Data attributes format section as well as the examples in the subsequent section.

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [November 30, 2022, 11:39pm UTC](https://discourse.vtk.org/t/polydata-face-level-attributes/10009/3 "2022-11-30T23:39:50Z")

</div>

Thank you @Christos_Tsolakis , I got it working.

```auto
#include <vtkActor.h>
#include <vtkCellData.h>
#include <vtkColorTransferFunction.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlaneSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkFloatArray.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkXMLPolyDataWriter.h>

#include <algorithm>
#include <iomanip>
#include <iostream>

void MakeCellData(size_t const& tableSize,
	vtkFloatArray* scalars) {
	for (size_t i = 1; i < tableSize; i++) {
		scalars->InsertNextTuple1(i);
	}
}

int main() {

	// Provide some geometry
	int resolution = 3;
	vtkNew<vtkPlaneSource> plane11;
	plane11->SetXResolution(resolution);
	plane11->SetYResolution(resolution);

	// Force an update so we can set cell data
	plane11->Update();

	int tableSize = std::max(resolution * resolution + 1, 10);

	vtkNew<vtkFloatArray> kelvin;
	kelvin->SetName("kelvin");
	kelvin->SetNumberOfComponents(1);

	MakeCellData(tableSize, kelvin);

	plane11->GetOutput()->GetCellData()->SetScalars(kelvin);

	// Write the file
	vtkSmartPointer<vtkXMLPolyDataWriter> writer =
		vtkSmartPointer<vtkXMLPolyDataWriter>::New();
	writer->SetFileName("celldata.vtp");
	writer->SetInputData(plane11->GetOutput());

	// Optional - set the mode. The default is binary.
	//writer->SetDataModeToBinary();
	writer->SetDataModeToAscii();

	writer->Write();

	return 0;
}

```

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [December 2, 2022, 7:18am UTC](https://discourse.vtk.org/t/polydata-face-level-attributes/10009/4 "2022-12-02T07:18:44Z")

</div>

I have found that I can only set one scalar, I have other attributes I’d like to add to the polydata e.g. salinity. If I set my salinity data, my temperature attribute is lost.

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [December 2, 2022, 7:23am UTC](https://discourse.vtk.org/t/polydata-face-level-attributes/10009/5 "2022-12-02T07:23:25Z")

</div>

Answering my own question. Use AddArray() rather than SetScalars() works for me.

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [December 7, 2022, 5:00am UTC](https://discourse.vtk.org/t/polydata-face-level-attributes/10009/6 "2022-12-07T05:00:55Z")

</div>

My AddArray() calls are now crashing in VTK but I am not sure why, do I have to do something to vtkCellData before I can call AddArray() ?

My code did not get to the line `printf("xx00102\n");`

```auto
	if (euclidean_areas_exist) {
		printf("xx00100\n");
		vtkNew<vtkFloatArray> vtk_euclidean_areas;
		vtk_euclidean_areas->SetName("eu_areas");
		vtk_euclidean_areas->SetNumberOfComponents(1);
		for (FloatContainer::const_iterator iter = euclidean_areas.begin();
				iter != euclidean_areas.end(); ++iter) {
			vtk_euclidean_areas->InsertNextTuple1((*iter));
		}
		printf("xx00101\n");
		assert(source);
		vtkCellData *cd = source->GetCellData();
		assert(cd);
		cd->AddArray(vtk_euclidean_areas);
		printf("xx00102\n");
	}

```

I have built a debug version of VTK 9.2.2 and step through the code and it appears to failed at the call to Modified()

 ![image](https://discourse.vtk.org/uploads/default/original/2X/a/a97edf7fcf189c118cbe2f92297de8178a529094.png)

---

<div class="post-metadata">

### Author: ![Nicholas\_Yue](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nicholas_yue/32/3751_2.png) [@Nicholas\_Yue](https://discourse.vtk.org/u/Nicholas_Yue)
#### Post date: [December 7, 2022, 4:18pm UTC](https://discourse.vtk.org/t/polydata-face-level-attributes/10009/7 "2022-12-07T16:18:55Z")

</div>

I’ll submit the above as a separate post
