Example M5: Shrink-wrap simulation box

This modifier function newly creates a simulation cell or adjusts an existing cell to match the axis-aligned bounding box of the particles. Please refer to the documentation page of this modifier in the OVITO user manual to learn more about this Python modifier function.

from ovito.data import DataCollection
import numpy

def modify(frame: int, data: DataCollection):

    # There's nothing we can do if there are no input particles.
    if not data.particles or data.particles.count == 0:
        return

    # Compute min/max range of particle coordinates.
    coords_min = numpy.amin(data.particles.positions, axis=0)
    coords_max = numpy.amax(data.particles.positions, axis=0)

    # Build the new 3x4 cell matrix:
    #   (x_max-x_min  0            0            x_min)
    #   (0            y_max-y_min  0            y_min)
    #   (0            0            z_max-z_min  z_min)
    matrix = numpy.empty((3,4))
    matrix[:,:3] = numpy.diag(coords_max - coords_min)
    matrix[:, 3] = coords_min

    # Assign the cell matrix - or create whole new SimulationCell object in
    # the DataCollection if there isn't one already.
    data.create_cell(matrix, (False, False, False))