Why is there no color in the scene I exported with vtkVRMLExporter

Viewed 78

I want to export the result, but there is always no correct color after opening it with ParaView. If you have any other suggestions please let me know.

    polydata = vtkAppendPolyData()
    reader = vtkNIFTIImageReader()
    reader.SetFileName('Brats18_2013_10_1_seg.nii.gz')

    for label,color,name in zip(labels,Colors,CC):
        pointColors = vtkUnsignedCharArray()
        pointColors.SetNumberOfComponents(3)
        pointColors.SetName('Colors')

        dmc = vtkDiscreteMarchingCubes()
        dmc.SetInputConnection(reader.GetOutputPort())
        dmc.SetValue(0, label)

        smooth = smooth_functions(dmc, 1)

        output = smooth.GetOutput()

        for i in range(output.GetNumberOfPoints()):
            pointColors.InsertNextTuple3(*color)

        output.GetPointData().Update()
        output.GetPointData().SetScalars(pointColors)
        polydata.AddInputData(output)


    mapper = vtkPolyDataMapper()
    mapper.SetInputConnection(polydata.GetOutputPort())
    actor = vtkActor()

    actor.SetMapper(mapper)

    ren = vtkRenderer()
    ren.AddActor(actor)
    renWin = vtkRenderWindow()
    renWin.AddRenderer(ren)
    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    iren.Initialize()
    iren.Start()

    exporter = vtkVRMLExporter()
    exporter.SetRenderWindow(renWin)
    exporter.SetActiveRenderer(ren)

    exporter.SetFileName("test.wrl")
    exporter.Write()

I originally used multiple actors to display different parts, but the exported result cannot be opened by ParaView, so I used vtkAppendPolyData

1 Answers

Please note the difference between two (VTK ?) concepts: Exporter and Writer (and their counterpart Importer and Reader)

Exporter/Importer

An exporter is intended to store the whole scene, in term of visible objects, in a file. It takes into account each object, the background, current colors etc ... Importing the resulting file should provide a same-looking scene. But in the process you can lose a lot of info, not visible at the export time

Writer/Reader

They are intended to write only one but full dataset on the disk, and is unrelated to any kind of visualization properties. It contains usually a mesh and associated data but no background color or such things.

Your problem

You are mixing both approach. ParaView use the Reader paradigm but you use the exporter one. That is probably why you cannot open the file with the multiple actors version, and why you do not have color now.

Possible solutions

  1. First way is to make sure the colors you want are correctly rendered. If yes, maybe the export / reader will work.

  2. Another possibility is to use a writer instead of the exporter. There is no vtkVRMLWriter (from my quick search at least) but you can use any format supporting polygonal meshes (as the vtkXMLPolyDataWriter )

  3. The last option, the cleaner from my point of view, is to keep separate objects: you can get rid of the vtkPolyDataAppend and replace it with vtkGroupDataSetsFilter and write it (for instance) with vtkXMLMultiBlockDataWriter)

Related