pyvista / pyvistaqt

Qt support for PyVista
http://qtdocs.pyvista.org
MIT License
111 stars 19 forks source link

Updating scalar field on Mapper does not work preoperly #635

Open zspasztori opened 2 months ago

zspasztori commented 2 months ago

I am working with point cloud data, which has multiple scalars associated. I am changing the active scalar based on user input. However, for some reason the color scale does not change. eg. if first scalar was bool, and next scalar will be float, the maximum color value will still be at 1 after the change

import numpy as np
import pyvista as pv
from pyvistaqt import BackgroundPlotter

coordinates = np.random.randint(0, 100, size=(100, 3))
bool_scalar = np.random.randint(0, 100, size=(100)) > 50 
int_scalar  = np.random.randint(0, 100, size=(100))

mesh = pv.PolyData(coordinates)
mesh['bool_scalar'] = bool_scalar
mesh['int_scalar'] = int_scalar

plotter = BackgroundPlotter()
actor = plotter.add_mesh(mesh, scalars='bool_scalar')

scalars = ['bool_scalar', 'int_scalar']
idx = 0
def my_key_callback():
    global idx
    print("You pressed the 's' key!", scalars, scalars[idx])
    idx = 1 - idx
    actor.mapper.array_name = scalars[idx]
    actor.mapper.scalar_range = mesh.get_data_range(scalars[idx])

plotter.add_key_event('s', my_key_callback)

Screenshot from 2024-09-26 13-18-07 Screenshot from 2024-09-26 13-18-24

zspasztori commented 2 months ago

it seems that the function get_data_range hasa bug the bool array is not working because of this condition, should I make a PR with fix? pyvista/core/dataset.py:893-894

  if arr.size == 0 or not np.issubdtype(arr.dtype, np.number):
      return (np.nan, np.nan)

also the scalar color mapping seems to work with :

mesh.set_active_scalars("my_scalar")
actor.mapper.scalar_range = mesh.get_data_range("my_scalar")

most likely the renderer is missing the automatic update of scalar range when after/during the set_active_scalars call