def _data_changed(self, old, new):
        if has_attributes(self.data):
            aa = self._assign_attribute
            self.configure_input_data(aa, new)
            self._update_data()
            aa.update()
            self.outputs = [aa]
        else:
            tp = tvtk.TrivialProducer()
            tp.set_output(self.data)
            self.outputs = [tp]
        self.data_changed = True

        self.output_info.datasets = [get_tvtk_dataset_name(self.outputs[0])]

        # Add an observer to the VTK dataset after removing the one
        # for the old dataset.  We use the messenger to avoid an
        # uncollectable reference cycle.  See the
        # tvtk.messenger module documentation for details.
        if old is not None:
            old.remove_observer(self._observer_id)
        self._observer_id = new.add_observer('ModifiedEvent',
                                             messenger.send)
        new_vtk = tvtk.to_vtk(new)
        messenger.connect(new_vtk, 'ModifiedEvent',
                          self._fire_data_changed)

        # Change our name so that our label on the tree is updated.
        self.name = self._get_name()
 def _setup_output(self, value):
     obj = self._my_input
     tp = tvtk.TrivialProducer()
     if hasattr(obj, 'number_of_blocks'):
         tp.set_output(obj.get_block(value))
     else:
         tp.set_output(self.inputs[0].outputs[value])
     self._set_outputs([tp])
Ejemplo n.º 3
0
def add_dataset(dataset, name='', **kwargs):
    """Add a dataset object to the Mayavi pipeline.

    **Parameters**

    :dataset: a tvtk/vtk dataset/tvtk/VTK Algorithm, or a Mayavi source. The
              dataset added to the Mayavi pipeline

    :figure: a figure identifier number or string, None or False, optional.

            If no `figure` keyword argument is given, the data
            is added to the current figure (a new figure if created if
            necessary).

            If a `figure` keyword argument is given, it should either the name
            the number of the figure the dataset should be added to, or None,
            in which case the data is not added to the pipeline.

            If figure is False, a null engine is created. This null
            engine does not create figures, and is mainly usefull for
            tensting, or using the VTK algorithms without visualization.

    **Returns**

    The corresponding Mayavi source is returned.

    """
    if isinstance(dataset, (tvtk.DataSet, vtk.vtkDataSet)):
        d = VTKDataSource()
        d.data = tvtk.to_tvtk(dataset)
    elif isinstance(dataset, (tvtk.DataObject, vtk.vtkDataObject)):
        d = VTKObjectSource()
        tp = tvtk.TrivialProducer()
        tp.set_output(tvtk.to_tvtk(dataset))
        d.object = tp
    elif isinstance(dataset, (tvtk.Object, vtk.vtkObject)):
        d = VTKObjectSource()
        d.object = tvtk.to_tvtk(dataset)
    elif isinstance(dataset, Source):
        d = dataset
    else:
        raise TypeError(
              "first argument should be either a TVTK object"
              " or a mayavi source")

    if len(name) > 0:
        d.name = name
    engine = _get_engine_from_kwarg(kwargs)
    if engine is None:
        # Return early, as we don't want to add the source to an engine.
        return d
    engine.add_source(d)
    return d