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 vtki >>> multi = vtki.MultiBlock() >>> multi[0] = vtki.PolyData() >>> multi[1, 'foo'] = vtki.UnstructuredGrid() >>> multi['bar'] = vtki.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_vtki_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()
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) data = self.GetBlock(index) if data is None: return data if data is not None and not is_vtki_obj(data): data = wrap(data) return data
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_vtki_obj(mesh): self.copy_meta_from(mesh)
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 run_from_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_vtki_obj(dataset): dataset = wrap(dataset) if not is_vtki_obj(dataset): raise RuntimeError( 'Object not supported for plotting in vtki.') # 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 = vtki.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)