diff --git a/doc/changelog.d/407.miscellaneous.md b/doc/changelog.d/407.miscellaneous.md new file mode 100644 index 00000000..3b4e14cc --- /dev/null +++ b/doc/changelog.d/407.miscellaneous.md @@ -0,0 +1 @@ +Feat(plotly): Link names to hover info diff --git a/examples/01-basic-plotly-examples/plain-usage.py b/examples/01-basic-plotly-examples/plain-usage.py index adf3294a..75b61587 100644 --- a/examples/01-basic-plotly-examples/plain-usage.py +++ b/examples/01-basic-plotly-examples/plain-usage.py @@ -107,7 +107,7 @@ def name(self): color='lightblue', opacity=0.50 ) -pl.plot(custom_mesh3d) +pl.plot(custom_mesh3d, name="CustomMesh3d") # Show other plotly objects like Scatter3d from plotly.graph_objects import Scatter3d @@ -119,6 +119,6 @@ def name(self): mode='markers', marker=dict(size=5, color='red') ) -pl.plot(scatter) +pl.plot(scatter, name="CustomScatter3d") pl.show() diff --git a/src/ansys/tools/visualization_interface/backends/plotly/plotly_interface.py b/src/ansys/tools/visualization_interface/backends/plotly/plotly_interface.py index 7b598bef..09a840d9 100644 --- a/src/ansys/tools/visualization_interface/backends/plotly/plotly_interface.py +++ b/src/ansys/tools/visualization_interface/backends/plotly/plotly_interface.py @@ -144,6 +144,7 @@ def plot_iter(self, plotting_list: Iterable[Any]) -> None: def plot( self, plottable_object: Union[PolyData, pv.MultiBlock, MeshObjectPlot, go.Mesh3d], + name: str = None, **plotting_options ) -> None: """Plot a single object using Plotly. @@ -154,27 +155,34 @@ def plot( The object to plot. Can be a PyVista PolyData, MultiBlock, a MeshObjectPlot, or a Plotly Mesh3d. plotting_options : dict Additional plotting options. + name : str, optional + Name of the mesh for labeling in Plotly. Overrides the name from MeshObjectPlot if provided. """ if isinstance(plottable_object, MeshObjectPlot): mesh = plottable_object.mesh + name = name or plottable_object.name else: mesh = plottable_object if isinstance(mesh, (PolyData, pv.MultiBlock)): mesh_result = self._pv_to_mesh3d(mesh) - # Handle both single mesh and list of meshes if isinstance(mesh_result, list): # MultiBlock case - add all meshes for mesh_3d in mesh_result: + mesh_3d.name = name or mesh_3d.name self._fig.add_trace(mesh_3d) else: - # Single PolyData case + mesh_result.name = name if name is not None else mesh_result.name self._fig.add_trace(mesh_result) elif isinstance(plottable_object, go.Mesh3d): + if name is not None: + plottable_object.name = name self._fig.add_trace(plottable_object) else: + # Try in case there is a compatible Plotly object try: + plottable_object.name = name self._fig.add_trace(plottable_object) except Exception: raise TypeError("Unsupported plottable_object type for PlotlyInterface.")