|
| 1 | +.. _ref_migration_guide: |
| 2 | + |
| 3 | +Migration |
| 4 | +######### |
| 5 | + |
| 6 | +This section helps you migrate from PyVista plotters to the Ansys Tools Visualization Interface plotters. |
| 7 | +It consists of two major topics: |
| 8 | + |
| 9 | +- `Code migration`_ |
| 10 | +- `Documentation configuration migration`_ |
| 11 | + |
| 12 | +Code migration |
| 13 | +============== |
| 14 | +This topic explains how to migrate from PyVista plotters to the new Ansys Tools Visualization Interface plotters. Because cases vary greatly, it provides a few examples that cover the most common scenarios. |
| 15 | + |
| 16 | +Replace PyVista plotter code with Ansys Tools Visualization Interface plotter code |
| 17 | +---------------------------------------------------------------------------------- |
| 18 | +If you only need to plot simple PyVista meshes, you can directly replace your PyVista plotter code with the Ansys Tools Visualization Interface plotter code. |
| 19 | +On top of common PyVista functionalities, the Ansys Tools Visualization Interface plotter provides additional interactivity such as view buttons and mesh slicing. |
| 20 | + |
| 21 | +The following code shows how to do the plotter code replacement: |
| 22 | + |
| 23 | +- PyVista code: |
| 24 | + |
| 25 | +.. code-block:: python |
| 26 | +
|
| 27 | + import pyvista as pv |
| 28 | +
|
| 29 | + # Create a PyVista mesh |
| 30 | + mesh = pv.Cube() |
| 31 | +
|
| 32 | + # Create a plotter |
| 33 | + pl = pv.Plotter() |
| 34 | +
|
| 35 | + # Add the mesh to the plotter |
| 36 | + pl.add_mesh(mesh) |
| 37 | +
|
| 38 | + # Show the plotter |
| 39 | + pl.show() |
| 40 | +
|
| 41 | +- Ansys Tools Visualization Interface code: |
| 42 | + |
| 43 | +.. code-block:: python |
| 44 | +
|
| 45 | + import pyvista as pv |
| 46 | + from ansys.tools.visualization_interface import Plotter |
| 47 | +
|
| 48 | + # Create a PyVista mesh |
| 49 | + mesh = pv.Cube() |
| 50 | +
|
| 51 | + # Create a plotter |
| 52 | + pl = Plotter() |
| 53 | +
|
| 54 | + # Add the mesh to the plotter |
| 55 | + pl.plot(mesh) |
| 56 | +
|
| 57 | + # Show the plotter |
| 58 | + pl.show() |
| 59 | +
|
| 60 | +
|
| 61 | +Convert your custom meshes to objects usable by the Ansys Tools Visualization Interface plotter |
| 62 | +----------------------------------------------------------------------------------------------- |
| 63 | + |
| 64 | +Your custom object must have a method that returns a PyVista mesh and a method that exposes a ``name`` or ``id`` attribute of your object: |
| 65 | + |
| 66 | +.. code-block:: python |
| 67 | +
|
| 68 | + class CustomObject: |
| 69 | + def __init__(self): |
| 70 | + self.name = "CustomObject" |
| 71 | + self.mesh = pv.Cube(center=(1, 1, 0)) |
| 72 | +
|
| 73 | + def get_mesh(self): |
| 74 | + return self.mesh |
| 75 | +
|
| 76 | + def name(self): |
| 77 | + return self.name |
| 78 | +
|
| 79 | +
|
| 80 | +You then need to create a :class:`~ansys.tools.visualization_interface.types.mesh_object_plot.MeshObjectPlot` instance that relates the PyVista mesh with your custom object: |
| 81 | + |
| 82 | +.. code-block:: python |
| 83 | +
|
| 84 | + from ansys.tools.visualization_interface import MeshObjectPlot |
| 85 | +
|
| 86 | + custom_object = CustomObject() |
| 87 | + mesh_object_plot = MeshObjectPlot( |
| 88 | + custom_object=custom_object, |
| 89 | + mesh=custom_object.get_mesh(), |
| 90 | + ) |
| 91 | +
|
| 92 | +With this, you can use the Ansys Tools Visualization Interface plotter to visualize your custom object. It enables interactivity such as picking and hovering. |
| 93 | + |
| 94 | + |
| 95 | +Customize the PyVista backend |
| 96 | +----------------------------- |
| 97 | + |
| 98 | +You can customize the backend of the Ansys Tools Visualization Interface plotter to enable or turn off certain functionalities. The following code shows how to enable picking: |
| 99 | + |
| 100 | +.. code-block:: python |
| 101 | +
|
| 102 | + from ansys.tools.visualization_interface import Plotter |
| 103 | + from ansys.tools.visualization_interface.backends import PyVistaBackend |
| 104 | +
|
| 105 | + backend = PyVistaBackend(allow_picking=True) |
| 106 | +
|
| 107 | + # Create a plotter |
| 108 | + pl = Plotter(backend=backend) |
| 109 | +
|
| 110 | + # Add the MeshObjectPlot instance to the plotter |
| 111 | + pl.plot(mesh_object_plot) |
| 112 | +
|
| 113 | + # Show the plotter |
| 114 | + pl.show() |
| 115 | +
|
| 116 | +If you want to customize the backend even more, you can create your own backend by inheriting from the :class:`~ansys.tools.visualization_interface.backends.pyvista.PyVistaBackendInterface` class |
| 117 | +and implementing the required methods: |
| 118 | + |
| 119 | +.. code-block:: python |
| 120 | +
|
| 121 | + @abstractmethod |
| 122 | + def plot_iter(self, plottable_object: Any, name_filter: str = None, **plotting_options): |
| 123 | + """Plot one or more compatible objects to the plotter. |
| 124 | +
|
| 125 | + Parameters |
| 126 | + ---------- |
| 127 | + plottable_object : Any |
| 128 | + One or more objects plot. |
| 129 | + name_filter : str, default: None. |
| 130 | + Regular expression with the desired name or names to include in the plotter. |
| 131 | + **plotting_options : dict, default: None |
| 132 | + Keyword arguments. For allowable keyword arguments, see the |
| 133 | + :meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method. |
| 134 | +
|
| 135 | + """ |
| 136 | + pass |
| 137 | +
|
| 138 | +
|
| 139 | + @abstractmethod |
| 140 | + def plot(self, plottable_object: Any, name_filter: str = None, **plotting_options): |
| 141 | + """Plot a single object to the plotter. |
| 142 | +
|
| 143 | + Parameters |
| 144 | + ---------- |
| 145 | + plottable_object : Any |
| 146 | + Object to plot. |
| 147 | + name_filter : str |
| 148 | + Regular expression with the desired name or names to include in the plotter. |
| 149 | + **plotting_options : dict, default: None |
| 150 | + Keyword arguments. For allowable keyword arguments, see the |
| 151 | + :meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method. |
| 152 | +
|
| 153 | + """ |
| 154 | + pass |
| 155 | +
|
| 156 | +
|
| 157 | +The rest of the methods are implemented for you. This ensures that while you can customize what you need for plotting, |
| 158 | +the rest of the functionalities still work as expected. For more information, see the backend documentation. If you |
| 159 | +need to even go further, you can create your own plotter by inheriting from the :class:`~ansys.tools.visualization_interface.backends._base.BaseBackend` class and implementing the required methods, |
| 160 | +although this may break existing features. |
| 161 | + |
| 162 | +Customize the picker or hover behavior |
| 163 | +-------------------------------------- |
| 164 | +You can customize the picker of the Ansys Tools Visualization Interface plotter to decide what happens when you pick or hover over an object. |
| 165 | +For example, if you want to print the name of the picked object, you can do it as described in the :ref:`sphx_glr_examples_00-basic-pyvista-examples_custom_picker.py` example. |
| 166 | + |
| 167 | +Use the PyVista Qt backend |
| 168 | +-------------------------- |
| 169 | +You can use the PyVista Qt backend with the Ansys Tools Visualization Interface plotter. To do this, you must set the PyVista backend to Qt |
| 170 | +before creating the plotter: |
| 171 | + |
| 172 | +.. code-block:: python |
| 173 | +
|
| 174 | + cube = pv.Cube() |
| 175 | + pv_backend = PyVistaBackend(use_qt=True, show_qt=True) |
| 176 | + pl = Plotter(backend=pv_backend) |
| 177 | + pl.plot(cube) |
| 178 | + pl.backend.enable_widgets() |
| 179 | + pv_backend.scene.show() |
| 180 | +
|
| 181 | +You can then integrate the plotter into a PyQt or PySide app by disabling the ``show_qt`` parameter. |
| 182 | +For more information about this, see the `PyVista documentation <https://qtdocs.pyvista.org/>`_. |
| 183 | + |
| 184 | + |
| 185 | +Documentation configuration migration |
| 186 | +===================================== |
| 187 | + |
| 188 | +This topic explains how to migrate from the PyVista documentation configuration to the new Ansys Tools Visualization Interface documentation configuration. |
| 189 | + |
| 190 | +1. Add environment variables for documentation: |
| 191 | + |
| 192 | +.. code-block:: python |
| 193 | +
|
| 194 | + os.environ["PYANSYS_VISUALIZER_DOC_MODE"] = "true" |
| 195 | + os.environ["PYANSYS_VISUALIZER_HTML_BACKEND"] = "true" |
| 196 | +
|
| 197 | +2. Use PyVista DynamicScraper: |
| 198 | + |
| 199 | +.. code-block:: python |
| 200 | +
|
| 201 | + from pyvista.plotting.utilities.sphinx_gallery import DynamicScraper |
| 202 | +
|
| 203 | + sphinx_gallery_conf = { |
| 204 | + "image_scrapers": (DynamicScraper()), |
| 205 | + } |
| 206 | +
|
| 207 | +3. Add PyVista viewer directive to extensions: |
| 208 | + |
| 209 | +.. code-block:: python |
| 210 | +
|
| 211 | + extensions = ["pyvista.ext.viewer_directive"] |
| 212 | +
|
| 213 | +4. Make sure you are executing the notebook cells: |
| 214 | + |
| 215 | +.. code-block:: python |
| 216 | +
|
| 217 | + nbsphinx_execute = "always" |
0 commit comments