Skip to content

Instantly share code, notes, and snippets.

@tigarmo
Last active January 24, 2019 12:23
Show Gist options
  • Save tigarmo/c90ff07a1b63bbcf396fa4aaf1e824a9 to your computer and use it in GitHub Desktop.
Save tigarmo/c90ff07a1b63bbcf396fa4aaf1e824a9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# This example attempts to render 2 poly datas that share the same vtkPoints
import vtk
# Create 8 points
points = vtk.vtkPoints()
points.InsertPoint(0, 0.0, 0.0, 0.0)
points.InsertPoint(1, 0.0, 1.0, 0.0)
points.InsertPoint(2, 1.0, 0.0, 0.0)
points.InsertPoint(3, 1.0, 1.0, 0.0)
points.InsertPoint(4, 2.0, 0.0, 0.0)
points.InsertPoint(5, 2.0, 1.0, 0.0)
points.InsertPoint(6, 3.0, 0.0, 0.0)
points.InsertPoint(7, 3.0, 1.0, 0.0)
# Create 2 polydatas, each with a single quad.
# The first polydata uses the first 4 points.
quad_1 = vtk.vtkCellArray()
quad_1.InsertNextCell(4)
quad_1.InsertCellPoint(0)
quad_1.InsertCellPoint(1)
quad_1.InsertCellPoint(3)
quad_1.InsertCellPoint(2)
pd_1 = vtk.vtkPolyData()
pd_1.SetPoints(points)
pd_1.SetPolys(quad_1)
# The second polydata uses the last 4 points.
quad_2 = vtk.vtkCellArray()
quad_2.InsertNextCell(4)
quad_2.InsertCellPoint(4)
quad_2.InsertCellPoint(5)
quad_2.InsertCellPoint(7)
quad_2.InsertCellPoint(6)
pd_2 = vtk.vtkPolyData()
pd_2.SetPoints(points)
pd_2.SetPolys(quad_2)
# Use a vtkMultiPieceDataSet to render using a single mapper
mpd = vtk.vtkMultiPieceDataSet()
mpd.SetPiece(0, pd_1)
mpd.SetPiece(1, pd_2)
mapper = vtk.vtkCompositePolyDataMapper2()
mapper.SetInputDataObject(0, mpd)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(0.3800, 0.7000, 0.1600)
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren.AddActor(actor)
ren.SetBackground(1, 1, 1)
renWin.SetSize(500, 500)
iren.Initialize()
renWin.Render()
iren.Start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment