Skip to content

Commit

Permalink
fix(local): workaround for lut serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Jan 30, 2024
1 parent edab22d commit 47bbda1
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
110 changes: 110 additions & 0 deletions examples/validation/ColorByComponentPyVista.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from trame.app import get_server
from trame.widgets import vuetify, vtk as vtk_widgets
from trame.ui.vuetify import SinglePageLayout

import pyvista as pv
from trame_vtk.modules.vtk.serializers import encode_lut

pv.OFF_SCREEN = True

# Can only be set once before initialization
encode_lut(True) # true: ok / false: ko

# -----------------------------------------------------------------------------
# Trame initialization
# -----------------------------------------------------------------------------

server = get_server()
server.client_type = "vue2"
state, ctrl = server.state, server.controller

# -----------------------------------------------------------------------------
# VTK code
# -----------------------------------------------------------------------------

plotter = pv.Plotter()
actor = plotter.add_mesh(
pv.Sphere(phi_resolution=60, theta_resolution=60),
)

mapper = actor.mapper
mapper.SelectColorArray("Normals")
mapper.SetScalarModeToUsePointFieldData()
mapper.SetScalarVisibility(True)
mapper.SetUseLookupTableScalarRange(True)

lut = mapper.lookup_table
lut.cmap = "viridis"
lut.scalar_range = (-1, 1)
lut.SetVectorModeToComponent()
lut.SetVectorSize(3)

plotter.render()
plotter.reset_camera()


@state.change("component_idx")
def color_by_array(component_idx, **kwargs):
lut.SetVectorModeToComponent()
lut.SetVectorSize(3)
lut.SetVectorComponent(component_idx)

ctrl.remote_view_update()
ctrl.local_view_update()


@state.change("cmap")
def color_preset(cmap, **kwargs):
lut.cmap = cmap
ctrl.remote_view_update()
ctrl.local_view_update()


with SinglePageLayout(server) as layout:
layout.icon.click = ctrl.view_reset_camera
layout.title.set_text("Color By Normal")

with layout.toolbar:
vuetify.VSpacer()
vuetify.VSelect(
v_model=("component_idx", 0),
items=(
"components",
[
dict(value=0, text="X"),
dict(value=1, text="Y"),
dict(value=2, text="Z"),
],
),
dense=True,
hide_details=True,
)
vuetify.VSelect(
v_model=("cmap", "viridis"),
items=(
"presets",
[
"viridis",
"hot",
],
),
dense=True,
hide_details=True,
)

with layout.content:
with vuetify.VContainer(
fluid=True,
classes="pa-0 fill-height",
):
with vuetify.VCol(classes="pa-0 fill-height"):
view = vtk_widgets.VtkLocalView(plotter.render_window, ref="local")
ctrl.local_view_update = view.update
ctrl.view_reset_camera.add(view.reset_camera)
ctrl.view_push_camera.add(view.push_camera)
with vuetify.VCol(classes="pa-0 fill-height"):
view = vtk_widgets.VtkRemoteView(plotter.render_window, ref="remote")
ctrl.remote_view_update = view.update
ctrl.view_reset_camera.add(view.reset_camera)

server.start()
2 changes: 2 additions & 0 deletions trame_vtk/modules/vtk/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .export import extract_array_hash
from .synchronization_context import SynchronizationContext
from .utils import reference_id
from .initialize import encode_lut

logger = logging.getLogger(__name__)
# By default, only show critical messages for serializers
Expand All @@ -17,6 +18,7 @@
logger.setLevel(logging.DEBUG)

__all__ = [
"encode_lut",
"reference_id",
"initialize_serializers",
"mesh",
Expand Down
18 changes: 17 additions & 1 deletion trame_vtk/modules/vtk/serializers/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
color_transfer_function_serializer,
discretizable_color_transfer_function_serializer,
lookup_table_serializer,
lookup_table_serializer2,
pwf_serializer,
)
from .mappers import generic_mapper_serializer, generic_volume_mapper_serializer
Expand All @@ -31,6 +32,20 @@

logger = logging.getLogger(__name__)

CONVERT_LUT = False


def encode_lut(value=True):
global CONVERT_LUT
CONVERT_LUT = value


def lookup_table_serializer_selector(*args, **kwargs):
global CONVERT_LUT
if CONVERT_LUT:
return lookup_table_serializer2(*args, **kwargs)
return lookup_table_serializer(*args, **kwargs)


def initialize_serializers():
# Define which serializer will be used for which VTK classes
Expand Down Expand Up @@ -87,7 +102,8 @@ def initialize_serializers():
"vtkOffscreenOpenGLRenderWindow",
],
# LookupTables/TransferFunctions
lookup_table_serializer: "vtkLookupTable",
# lookup_table_serializer: "vtkLookupTable",
lookup_table_serializer_selector: "vtkLookupTable",
discretizable_color_transfer_function_serializer: "vtkPVDiscretizableColorTransferFunction",
color_transfer_function_serializer: "vtkColorTransferFunction",
pwf_serializer: "vtkPiecewiseFunction",
Expand Down

0 comments on commit 47bbda1

Please sign in to comment.