Mapping Scalars into colors

Hello,

I am trying to color the connected components in the image according to their cell scalar value array. The scalarbar renders ok, with the correct labels, but the image renders only with one color.

Thank you

  selectCells->Update();
  
  vtkNew<vtkGeometryFilter> geometry;
  geometry->SetInputConnection(selectCells->GetOutputPort());
  geometry->Update();
  
  vtkSmartPointer<vtkPolyData> geometry_mesh;
  geometry_mesh = geometry->GetOutput();
  
  if(geometry_mesh){	  
  
  vtkCellData* cellDataGeometry = geometry_mesh->GetCellData();
  vtkDataArray* bodyScalars  = cellDataGeometry->GetArray(1);
  
  if(geometry_mesh->GetNumberOfCells()!=0){
  
  double bodies[2];
  std::cerr << "No of Cells:  "<< geometry_mesh->GetNumberOfCells() << std::endl;  
  
    bodyScalars->GetRange(bodies);
    
    std::cerr << "Bodies:  "<< "["<< bodies[0]<<","<<bodies[1]<<"]"<< std::endl;  
    
    // Create a named colors object for setting renderer background and actor color
    vtkNew<vtkNamedColors> colors;

    // Create a renderer
    vtkNew<vtkRenderer> renderer;
    renderer->SetBackground(colors->GetColor3d("LightSteelBlue").GetData());
    
    // Use a color series to create a transfer function
    vtkNew<vtkColorSeries> colorSeries;
    colorSeries->SetColorScheme(vtkColorSeries::BREWER_DIVERGING_SPECTRAL_11);


    // Create a render window
    vtkNew<vtkEGLRenderWindow> renderWindow;
    renderWindow->OffScreenRenderingOn();
    renderWindow->SetShowWindow(false);
    renderWindow->WindowInitialize();
    renderWindow->SetSize(640, 480);
    renderWindow->SetWindowName("Mesh Render Window");
    renderWindow->AddRenderer(renderer);

    std::cerr << "Render Render Window OK" << std::endl;  		

    // Create a render window interactor
    vtkNew<vtkRenderWindowInteractor> interactor;
    interactor->SetRenderWindow(renderWindow);
    
    
    int n = int(bodies[1]-bodies[0]);
      vtkSmartPointer<vtkLookupTable> lut = MakeColors(n+1);
  
       std::string array_name = "IBODY";

        vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputData(geometry_mesh);
    mapper->SetLookupTable(lut);
    mapper->ScalarVisibilityOn();
    mapper->SetScalarModeToUseCellData();
    mapper->SetColorModeToMapScalars();
    mapper->InterpolateScalarsBeforeMappingOff();
    mapper->SetScalarRange(geometry_mesh->GetCellData()->GetArray(1)->GetRange()[0],geometry_mesh->GetCellData()->GetArray(1)->GetRange()[1]);
    mapper->SelectColorArray(1);
    
    int scalarValue=1;
 //   mapper->GetScalars(geometry_mesh, 1, 0, 1, "IBODY", scalarValue);

    vtkNew<vtkScalarBarActor> scalarBar;
    scalarBar->SetLookupTable(mapper->GetLookupTable());
    scalarBar->SetTitle("Bodies");
    scalarBar->SetNumberOfLabels(n+1);
    

    // Create an actor
    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
        actor->GetProperty()->SetInterpolationToFlat();
        actor->RotateWXYZ (1.0, -90.0, 0.0, 0.0);


//  actor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());

    // Add the actor to the renderer
    renderer->AddActor(actor);
    renderer->AddActor2D(scalarBar);
        

    // Render the window
    renderWindow->Render();
    
    std::cerr << "Render OK" << std::endl;  		
    
    vtkNew<vtkWindowToImageFilter> windowToImageFilter;
    windowToImageFilter->SetInput(renderWindow);
    windowToImageFilter->Update();
    
    std::string body = "bodies for tissue" + std::to_string(i);
  
    vtkNew<vtkPNGWriter> writer;
    writer->SetFileName(body.c_str());
    writer->SetInputConnection(windowToImageFilter->GetOutputPort());
    writer->Write();std::cerr << "Bodies Complete." << std::endl;
    //end loop
    }}else{std::cerr << "Error: Unable to Threshold Mesh." << std::endl;}

using point data worked well:

        vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputData(geometry_mesh);
    mapper->ScalarVisibilityOn();
    mapper->SetColorModeToMapScalars();
    mapper->SetScalarModeToUsePointData();
//  mapper->SelectColorArray(0);
    mapper->SetLookupTable(lut);
  //mapper->SetColorModeToDirectScalars();
    mapper->InterpolateScalarsBeforeMappingOff();
  //  mapper->ColorByArrayComponent(1,0);
//  mapper->SetScalarRange(geometry_mesh->GetCellData()->GetArray("IBODY")->GetRange(0));
    mapper->SetScalarRange(bodies[0], bodies[1]);

  // vtkNew<vtkScalarsToColors.h> stc;
     //   stc->MapScalarsThroughTable(geometry_mesh->GetCellData()->GetArray(1))



    
    int scalarValue=1;
 // mapper->GetScalars(geometry_mesh, 1, 0, 1, "IBODY", scalarValue);

    vtkNew<vtkScalarBarActor> scalarBar;
    scalarBar->SetTitle("Bodies");
    scalarBar->SetNumberOfLabels(n);
    scalarBar->SetLookupTable(lut);

    // Create an actor
    vtkNew<vtkActor> actor;
    actor->SetMapper(mapper);
        actor->RotateWXYZ(-90.0, 1.0, 0.0, 0.0);
        actor->GetProperty()->SetInterpolationToFlat();
        actor->SetVisibility(true);
    actor->Modified();

//  actor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());

    // Add the actor to the renderer
    renderer->AddActor(actor);
    renderer->AddActor2D(scalarBar);
        

    // Render the window
    renderWindow->Render();
    
    std::cerr << "Render OK" << std::endl;  		
    
    vtkNew<vtkWindowToImageFilter> windowToImageFilter;
    windowToImageFilter->SetInput(renderWindow);
    windowToImageFilter->Update();
    
    std::string body = "bodies for tissue" + std::to_string(i);
  
    vtkNew<vtkPNGWriter> writer;
    writer->SetFileName(body.c_str());
    writer->SetInputConnection(windowToImageFilter->GetOutputPort());
    writer->Write();std::cerr << "Bodies Complete." << std::endl;