Writing a Polydatamapper to vtk

Hi All,

The following code creates polygons from point and adds polygons. I eventually write this appended data to a vtk file. I want to color these polygons to a specific RGB color so that when I load the vtk file in Paraview glance, I can see the colors instead of solid colors. I have checked several examples on and I learned that the way to assign colors is to use a lookuptable and a mapper. Now the question is, Can I write a mapper to a vtk file or I should not think about mappers at all in this case?

def create_polygon(points):
    """Create a vtk Polygon from a list of points.
    The point will have to be in an order. This function cannot be used to create a
    polygon with holes.

    Args:
        points: A list of points. Here, each point is a list of X, Y, and Z cordinate.

    Returns:
        A tuple with two elements.
        - vtk_points: A list of vtk point objects
        - vtk_polygon: A vtk polygon object created from the points provided.
    """

    vtk_points = vtk.vtkPoints()
    vtk_polygon = vtk.vtkPolygon()
    vtk_polygon.GetPointIds().SetNumberOfIds(len(points))

    for point in points:
        vtk_points.InsertNextPoint(tuple(point))

    for i in range(len(points)):
        vtk_polygon.GetPointIds().SetId(i, i)

    return vtk_points, vtk_polygon


def create_polygons(points_lst):
    """Create a vtk object with multiple vtk polygons.
    Args:
        points_lst: A list of lists of points. Here, each point is a list of X, Y, 
            and Z cordinate.

    Returns:
        A vtk object that has multiple vtk polygons.
    """

    vtk_polydata_lst = []

    for i in range(len(points_lst)):
        # Create a list of vtk polydata objects with polygons
        vtk_points, vtk_polygon = create_polygon(points_lst[i])
        vtk_polygons = vtk.vtkCellArray()
        vtk_polygons.InsertNextCell(vtk_polygon)

        vtk_polydata = vtk.vtkPolyData()
        vtk_polydata.SetPoints(vtk_points)
        vtk_polydata.SetPolys(vtk_polygons)
        vtk_polydata.Modified()

        vtk_polydata_lst.append(vtk_polydata)

    # Create a vtk object with multiple polygons
    vtk_polydata_extended = vtk.vtkAppendPolyData()

    for i in range(len(vtk_polydata_lst)):
        vtk_polydata_extended.AddInputData(vtk_polydata_lst[i])

    vtk_polydata_extended.Update()

    return vtk_polydata_extended

I wouldn’t use lookup tables, but would add an array of scalar values to the cells. You can add several different scalar arrays, and set one of them as the default. Paraview (and glance) will then use that to colour the cells, and you can switch between the different data sets to get different colouring options.

Hey @Euan,
That sounds very promising. I know how to assign scalars to celldata. However, I don’t know how to set them as Default so that when I load them in Paraview, the file can be loaded with the scalar rather than the solid color. Can you share how I can do that please?