After add actor to render, can i change the actor data real time ?

Dear vtk users:

As we know, after add actor to render, we can change the properties of the actor.
Now I tried to change the data of the actor,but it does not work.
My code is as below:

vtkActor* temp_actor = render->GetActors()->GetLastActor();
vtkMapper* temp_mapper = temp_actor->GetMapper();
vtkPolyData* temp_poly_data = vtkPolyData::SafeDownCast(temp_mapper->GetInput());
vtkPoints* temp_points = temp_poly_data->GetPoints();
int points_num = temp_points->GetNumberOfPoints();

// change z value of 100 points
for (int i = 0; i < 100; i++)
//get a random idx
int idx = rand() % (points_num - 0 + 1);

//get point by idx
double* p_data = temp_points->GetPoint(idx);

//change z value of the point
p_data[2] *= 2;




You might find the markdown rules for code highlighting useful.

Before I get to your main question, I’ll warn you against doing the following, because GetPoint(idx) just returns a copy of the point, and modifying the copy doesn’t modify the original point.

double* p_data = temp_points->GetPoint(idx);
p_data[2] *= 2;

If you want to modify the point, you must use SetPoint():

double p_data[3];
temp_points->GetPoint(idx, p_data);
p_data[2] *= 2;
temp_points->SetPoint(idx, p_data);

Finally, to answer your question, after setting all of the points that you want to change, you must call Modified():


However, modifying an actor’s data like this is not always a safe thing to do. If the data object was generated as the output of a VTK algorithm object, and if it is still associated with that algorithm object, then the data object should only be modified by that algorithm object.

If you want to modify the data, you must make sure that it is no longer associated with the algorithm object. For example:

vtkDataSet* data = algorithm->GetOutput();
// separate the data from the algorithm
// now we can safely modify the data ourselves

Also, when you modify the data, you are responsible for the internal consistency of the data object. So if you add more points, you must ensure that any arrays associated with the points are properly resized. If you remove points, you must ensure that no cells refer to the points that you have removed.

Thanks so much, the problem bother me for a long time, I tried your method, it works well.