Skip to content

Commit

Permalink
fix(ref): automatically assign non conflicting ref
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Nov 16, 2023
1 parent 6f92907 commit 3cfdf2a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
6 changes: 4 additions & 2 deletions examples/validation/ManyViews.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,11 @@ def update_sphere(active_view, show_sphere, **kwargs):
):
render_window = VIEWS[c].get("render_window")
view = vtk_widgets.VtkLocalView(
render_window, ref=f"view_{c}", v_if=(f"widget_on_{c}", True)
render_window, v_if=(f"widget_on_{c}", True)
)
# view = vtk_widgets.VtkRemoteView(render_window, ref=f"view_{c}", v_if=(f"widget_on_{c}", True))
# view = vtk_widgets.VtkRemoteView(
# render_window, v_if=(f"widget_on_{c}", True)
# )
ctrl[f"view_{c}_update"] = view.update
ctrl[f"view_{c}_reset"] = view.reset_camera

Expand Down
45 changes: 42 additions & 3 deletions trame_vtk/widgets/vtk/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ class VtkRemoteLocalView(HtmlElement):
... )
"""

_next_id = 0

def __init__(self, view, enable_rendering=True, widgets=[], **kwargs):
super().__init__("vtk-remote-local-view", **kwargs)

Expand All @@ -375,6 +377,12 @@ def __init__(self, view, enable_rendering=True, widgets=[], **kwargs):
MODULE.has_capabilities("web", "rendering")

__ns = kwargs.get("namespace", "view")

if __ns == "view":
VtkRemoteLocalView._next_id += 1
if VtkRemoteLocalView._next_id > 1:
__ns = f"view{VtkRemoteLocalView._next_id}"

self.__mode_key = f"{__ns}Mode"
self.__scene_id = f"{__ns}Scene"
self.__view_key_id = f"{__ns}Id"
Expand Down Expand Up @@ -659,6 +667,8 @@ class VtkRemoteView(HtmlElement):
... )
"""

_next_id = 0

@staticmethod
def push_image(view):
"""
Expand All @@ -667,13 +677,17 @@ def push_image(view):
if MODULE:
MODULE.push_image(view)

def __init__(self, view, ref="view", **kwargs):
def __init__(self, view, ref=None, **kwargs):
super().__init__("vtk-remote-view", **kwargs)

activate_module_for(self.server, view)

MODULE.has_capabilities("web", "rendering")

if ref is None:
VtkRemoteView._next_id += 1
ref = f"trame__remote_view_{VtkRemoteView._next_id}"

self.__view = view
self.__ref = ref
self.__view_key_id = f"{ref}Id"
Expand Down Expand Up @@ -761,6 +775,10 @@ def capture_image(self, format="image/png", opts={}):
def release_resources(self):
self.__view = None

@property
def ref_name(self):
return self.__ref


class VtkShareDataset(HtmlElement):
def __init__(self, children=None, **kwargs):
Expand Down Expand Up @@ -790,13 +808,19 @@ class VtkLocalView(HtmlElement):
... )
"""

def __init__(self, view, ref="view", widgets=[], **kwargs):
_next_id = 0

def __init__(self, view, ref=None, widgets=[], **kwargs):
super().__init__("vtk-local-view", **kwargs)

activate_module_for(self.server, view)

MODULE.has_capabilities("web")

if ref is None:
VtkLocalView._next_id += 1
ref = f"trame__local_view_{VtkLocalView._next_id}"

self.__scene_id = f"scene_{ref}"
self.__view = view
self.__ref = ref
Expand Down Expand Up @@ -967,10 +991,21 @@ def release_resources(self):
self.__view = None
self._widgets = None

@property
def ref_name(self):
return self.__ref


class VtkView(HtmlElement):
def __init__(self, children=None, ref="view", **kwargs):
_next_id = 0

def __init__(self, children=None, ref=None, **kwargs):
super().__init__("vtk-view", children, **kwargs)

if ref is None:
VtkView._next_id += 1
ref = f"trame__client_view_{VtkView._next_id}"

self._ref = ref
self._attributes["ref"] = f'ref="{ref}"'
self._attr_names += [
Expand Down Expand Up @@ -1029,3 +1064,7 @@ def reset_camera(self, **kwargs):
Move camera to center actors within the frame
"""
self.server.js_call(ref=self._ref, method="resetCamera")

@property
def ref_name(self):
return self._ref

0 comments on commit 3cfdf2a

Please sign in to comment.