# WritePLY

VTKExamples/Python/IO/WritePLY

### Description

Note

Unlike vtkPolyDataXMLWriter and most other VTK writers, to write colors to the .ply file you must specify to the vtkPLYWriter:

plyWriter->SetArrayName("Colors");

where your color array was created/named like this:

vtkSmartPointer vtkUnsignedCharArray> colors = vtkSmartPointer vtkUnsignedCharArray>::New(); colors->SetNumberOfComponents(3); colors->SetName("Colors");

(This was found in response to a user question on StackOverflow: http://stackoverflow.com/questions/17783612/save-mesh-with-rgb-in-vtk/19525938)

Other Languages

See (Cxx)

### Code

WritePLY.py

    import vtk

    filename = "writeply.ply"

    sphereSource = vtk.vtkSphereSource()
    sphereSource.Update()

    plyWriter = vtk.vtkPLYWriter()
    plyWriter.SetFileName(filename)
    plyWriter.SetInputConnection(sphereSource.GetOutputPort())
    plyWriter.Write()

    # Read and display for verication
    reader = vtk.vtkPLYReader()
    reader.SetFileName(filename)
    reader.Update()

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(reader.GetOutputPort())

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)

    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)

    renderer.AddActor(actor)
    renderer.SetBackground(.3, .6, .3)  # Background color green

    renderWindow.Render()
    renderWindowInteractor.Start()