enthought/mayavi

Unable to change the color of points3D

GrowlingM1ke opened this issue · 2 comments

Hello there,

I'm developing an interactive GUI, where I've plotted a set of 3D points onto a plane. I managed to register clicks from the user side to recognize, which point has been selected, however I'm struggling to change the color of any selected point. All I want is for my white points to turn red when they are selected.

Instantiation of the points:

self.points_3d = mlab.points3d(*points_coords, color=(1, 1, 1), mode="cube", scale_factor=2.0)

Me attempting to update the color of a given point:

self.points_3d.mlab_source.set(x=points_coords[0], y=points_coords[1], z=points_coords[2])
sc=tvtk.UnsignedCharArray()
sc.from_array(points_coords[3])
self.points_3d.mlab_source.dataset.point_data.scalars = sc
self.points_3d.mlab_source.dataset.modified()

As it stands now whatever colors I try to input, either the points blow up in size or disappear but they don't actually change their color.

I've figured it out. For some reason if you attempt to overwrite the scalars with a new array it makes the points disappear. The solution? Overwrite the scalar information by indexing as needed:

self.points_3d.mlab_source.set(x=points_coords[0], y=points_coords[1], z=points_coords[2])
# Get indices where equal to 1
indices_selected = np.where(points_coords[3] == 1)[0]
indices_not_selected = np.where(points_coords[3] == 0)[0]
for idx in indices_selected:
  self.points_3d.mlab_source.dataset.point_data.scalars[int(idx)] = 2
for idx in indices_not_selected:
  self.points_3d.mlab_source.dataset.point_data.scalars[int(idx)] = 1
self.points_3d.mlab_source.dataset.modified()

FYI you will also need to declare the scalars in the creation of the points:

self.points_3d = mlab.points3d(*points_coords, np.ones(len(points_coords[0])), colormap="prism", mode="cube", scale_factor=2.0)