66from typing import Union , Iterable , Any
77
88
9- class PlotlyInterface (BaseBackend ):
9+ class PlotlyBackend (BaseBackend ):
1010 """Plotly interface for visualization."""
1111
1212 def __init__ (self , ** kwargs ):
@@ -17,16 +17,20 @@ def _pv_to_mesh3d(self, pv_mesh: PolyData) -> go.Mesh3d:
1717 points = pv_mesh .points
1818 x , y , z = points [:, 0 ], points [:, 1 ], points [:, 2 ]
1919
20- faces = pv_mesh .faces .reshape ((- 1 , 4 )) # First number in each row is the number of points in the face (3 for triangles)
20+ # Convert mesh to triangular mesh if needed, since Plotly only supports triangular faces
21+ triangulated_mesh = pv_mesh .triangulate ()
22+
23+ # Extract triangular faces
24+ faces = triangulated_mesh .faces .reshape ((- 1 , 4 )) # Now we know all faces are triangular (3 vertices + count)
2125 i , j , k = faces [:, 1 ], faces [:, 2 ], faces [:, 3 ]
2226
2327 return go .Mesh3d (x = x , y = y , z = z , i = i , j = j , k = k )
2428 @property
2529 def layout (self ) -> Any :
2630 """Get the current layout of the Plotly figure."""
2731 return self ._fig .layout
28-
29- @setters . layout
32+
33+ @layout . setter
3034 def layout (self , new_layout : Any ):
3135 """Set a new layout for the Plotly figure."""
3236 self ._fig .update_layout (new_layout )
@@ -54,6 +58,22 @@ def plot(self, plottable_object: Union[PolyData, MeshObjectPlot, go.Mesh3d], **p
5458 except Exception :
5559 raise TypeError ("Unsupported plottable_object type for PlotlyInterface." )
5660
57- def show (self ):
61+ def show (self ,
62+ plottable_object = None ,
63+ screenshot : str = None ,
64+ name_filter = None ,
65+ ** kwargs ):
5866 """Render the Plotly scene."""
59- self ._fig .show ()
67+ if plottable_object is not None :
68+ self .plot (plottable_object )
69+
70+ # Only show in browser if no screenshot is being taken
71+ if not screenshot :
72+ self ._fig .show (** kwargs )
73+
74+ if screenshot :
75+ screenshot_str = str (screenshot )
76+ if screenshot_str .endswith ('.html' ):
77+ self ._fig .write_html (screenshot_str )
78+ else :
79+ self ._fig .write_image (screenshot_str )
0 commit comments