Exemple #1
0
    def __init__(self, dataset, plotter=None, scalars=None, preference='cell',
                 show_bounds=False, reset_camera=True, outline=None,
                 display_params=None, default_params=None,
                 continuous_update=False, clean=True, **kwargs):
        if not scooby.in_ipython() or not IPY_AVAILABLE:
            logging.warning('Interactive plotting tools require IPython and the ``ipywidgets`` package.')
        # Check the input dataset to make sure its compatible
        if not is_pyvista_obj(dataset):
            dataset = wrap(dataset)
            if not is_pyvista_obj(dataset):
                raise RuntimeError('Object not supported for plotting in pyvista.')

        # Make the input/output of this tool available
        self.input_dataset = dataset
        self.output_dataset = None

        self.continuous_update = continuous_update

        if plotter is None:
            plotter = BackgroundPlotter(**kwargs)
            plotter.setWindowTitle(type(self).__name__)
        self._plotter = plotter
        self._loc = plotter.index_to_loc(plotter._active_renderer_index)

        # This is the actor that will be removed and re-added to the plotter
        self._data_to_update = None

        self._last_scalars = None

        # Intialize plotting parameters
        self.valid_range = self.input_dataset.get_data_range(arr=scalars, preference=preference)
        if display_params is None:
            display_params = {}
        if default_params is None:
            default_params = {}
        display_params.setdefault('rng', self.valid_range)
        display_params.setdefault('scalars', scalars)
        display_params.setdefault('preference', preference)
        display_params.setdefault('show_edges', False)
        # Make sure to remove the reset_camera parameter if present
        display_params.pop('reset_camera', None)
        # Make a name
        display_params.setdefault('name', '{}({})'.format(type(self).__name__, str(hex(id(self)))))
        self.display_params = display_params

        self.clean = clean

        # Set the tool status
        self._need_to_update = True

        # Add some intital plotting stuff to the scene
        self._initialize(show_bounds, reset_camera, outline)

        # Run the tool
        self.tool(default_params=default_params, **kwargs)
Exemple #2
0
 def wrap_nested(self):
     """A helper to ensure all nested data structures are wrapped as PyVista
     datasets. This is perfrom inplace"""
     for i in range(self.n_blocks):
         block = self.GetBlock(i)
         if not is_pyvista_obj(block):
             self.SetBlock(i, pyvista.wrap(block))
     return
Exemple #3
0
    def overwrite(self, mesh):
        """
        Overwrites this mesh inplace with the new mesh's geometries and data

        Parameters
        ----------
        mesh : vtk.vtkDataSet
            The overwriting mesh.

        """
        self.DeepCopy(mesh)
        if is_pyvista_obj(mesh):
            self.copy_meta_from(mesh)
Exemple #4
0
 def __getitem__(self, index):
     """Get a block by its index or name (if the name is non-unique then
     returns the first occurence)"""
     if isinstance(index, str):
         index = self.get_index_by_name(index)
     if index < 0:
         index = self.n_blocks + index
     if index < 0 or index >= self.n_blocks:
         raise IndexError('index ({}) out of range for this dataset.'.format(index))
     data = self.GetBlock(index)
     if data is None:
         return data
     if data is not None and not is_pyvista_obj(data):
         data = wrap(data)
     if data not in self.refs:
         self.refs.append(data)
     return data
Exemple #5
0
    def __setitem__(self, index, data):
        """Sets a block with a VTK data object. To set the name simultaneously,
        pass a string name as the 2nd index.

        Example
        -------
        >>> import pyvista
        >>> multi = pyvista.MultiBlock()
        >>> multi[0] = pyvista.PolyData()
        >>> multi[1, 'foo'] = pyvista.UnstructuredGrid()
        >>> multi['bar'] = pyvista.PolyData()
        >>> multi.n_blocks
        3
        """
        if isinstance(index,
                      collections.Iterable) and not isinstance(index, str):
            i, name = index[0], index[1]
        elif isinstance(index, str):
            try:
                i = self.get_index_by_name(index)
            except KeyError:
                i = -1
            name = index
        else:
            i, name = index, None
        if data is not None and not is_pyvista_obj(data):
            data = wrap(data)
        if i == -1:
            self.append(data)
            i = self.n_blocks - 1
        else:
            self.SetBlock(i, data)
        if name is None:
            name = 'Block-{0:02}'.format(i)
        self.set_block_name(i, name)  # Note that this calls self.Modified()
        if data not in self.refs:
            self.refs.append(data)