I create a vtkPolyData
, and I can display it. The code is:
import vtkmodules.all as vtk
import time
def point(x, y, z):
polyData = vtk.vtkPolyData()
polyData.SetPoints(vtk.vtkPoints())
polyData.SetVerts(vtk.vtkCellArray())
polyData.GetPoints().InsertNextPoint(x, y, z)
polyData.GetVerts().InsertNextCell(1)
polyData.GetVerts().InsertCellPoint(0)
return polyData
if __name__ == '__main__':
polyData1 = point(0, 0, 0)
actor1 = vtk.vtkFollower()
mapper1 = vtk.vtkPolyDataMapper()
mapper1.SetInputData(polyData1)
actor1.SetMapper(mapper1)
actor1.GetProperty().RenderPointsAsSpheresOn()
actor1.GetProperty().SetRepresentationToPoints()
actor1.GetProperty().SetPointSize(10)
polyData2 = point(10, 0, 0)
actor2 = vtk.vtkFollower()
mapper2 = vtk.vtkPolyDataMapper()
mapper2.SetInputData(polyData2)
actor2.SetMapper(mapper2)
actor2.GetProperty().RenderPointsAsSpheresOn()
actor2.GetProperty().SetRepresentationToPoints()
actor2.GetProperty().SetPointSize(10)
ren = vtk.vtkRenderer()
ren.AddActor(actor1)
ren.AddActor(actor2)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.Render()
for i in range(2, 10):
polyData2.GetPoints().SetPoint(0, i, 0, 0)
polyData2.Modified()
mapper2.SetInputData(polyData2)
mapper2.Update()
actor2.Modified()
renWin.Render()
time.sleep(0.5)
We can see that the point
is not updated. The reason is that the verts
has not been updated.
The for
loop should be modified as:
for i in range(2, 10):
polyData2.GetPoints().SetPoint(0, i, 0, 0)
polyData2.Modified()
polyData2.GetVerts().Modified() ##################### modified
polyData2.GetPoints().Modified() ##################### modified
mapper2.SetInputData(polyData2)
mapper2.Update()
actor2.Modified()
renWin.Render()
time.sleep(0.5)
Then, the points
is updated as I expected.
However, I think that polyData2.Modified()
should automatically update the time for verts
and points
.
I think the solution should be that overwrite the modified
method in vtkPolyData
:
void vtkPolyData::Modified()
{
vtkPointSet::Modified();
if(this->Verts != nullptr)
{
this->Verts->Modified();
}
if(this->Lines!= nullptr)
{
this->Lines->Modified();
}
if(this->Polys!= nullptr)
{
this->Polys->Modified();
}
if(this->Strips!= nullptr)
{
this->Strips->Modified();
}
if(this->GetPoints()!= nullptr)
{
this->GetPoints()->Modified();
}
}