Can't Add scalar data array to polydata.

Hello,

I’ve been using a function to try to add a material scalar array to a mesh but have not been able to do it.

Thank you,

     vtkSmartPointer<vtkPolyData> 
   SegmentationIO::Add_Material(vtkSmartPointer<vtkPolyData> polyData, int material, 
   vtkSmartPointer<vtkIntArray> MATERIAL){
      
     
      int numberOfTriangles;
      
      numberOfTriangles = polyData->GetNumberOfCells();
  MATERIAL->SetNumberOfValues(numberOfTriangles);

  // Assign some example integer values to each triangle
  for (int i = 0; i < numberOfTriangles; ++i) {MATERIAL->SetValue(i, material);}
  
  // Add the attributes to the cell data
  polyData->GetCellData()->AddArray(MATERIAL);
  std::cerr << "Array OK" << std::endl;  
    
    return polyData;
    }


  // ADD MATERIAL
   
  vtkSmartPointer<vtkIntArray> MATERIAL = vtkSmartPointer<vtkIntArray>::New();
 
  vtkSmartPointer<vtkPolyData> material_mesh;  	  
  
  material_mesh = Process.Add_Material(rotated_mesh,threshold, MATERIAL);

This worked fine:

    vtkSmartPointer<vtkPolyData> SegmentationIO::Add_Material(vtkSmartPointer<vtkPolyData> polyData, int material){
      
      vtkSmartPointer<vtkIntArray> MATERIAL = vtkSmartPointer<vtkIntArray>::New();

     
      int numberOfTriangles;
      
      numberOfTriangles = polyData->GetNumberOfCells();
  MATERIAL->SetNumberOfTuples(numberOfTriangles);
  MATERIAL->SetName("MATERIAL");
  MATERIAL->SetNumberOfComponents(1);
  
  vtkCellData* cellData = polyData->GetCellData();
  vtkDataArray* arrayData = cellData->GetScalars();
  
  
  
  // Assign some example integer values to each triangle
  for (auto i = 0; i < numberOfTriangles; ++i) {
  MATERIAL->InsertTuple1(i, (double)material);

  }
  
  
  
  // Add the attributes to the cell data
  polyData->GetCellData()->AddArray(MATERIAL);
  polyData->GetCellData()->SetActiveScalars("MATERIAL");
  std::cerr << "Array OK" << std::endl;  
  

      //polyData->GetCellData()->Modified();
      polyData->GetCellData()->GetArray("MATERIAL")->Modified();

for (vtkIdType i = 0; i < numberOfTriangles; ++i) {
std::cerr << "Tuple " << i << ": " << MATERIAL->GetTuple1(i) << std::endl;

FROM TERMINAL:

Tuple 152392: 9
Tuple 152393: 9
Tuple 152394: 9
Tuple 152395: 9
Tuple 152396: 9
Tuple 152397: 9
Tuple 152398: 9
Tuple 152399: 9
Tuple 152400: 9
Tuple 152401: 9
Tuple 152402: 9
Tuple 152403: 9