Python scripting in ovito

Hello I am running this python code with ovito 3.0:
from ovito.io import import_file
from ovito.modifiers import ComputePropertyModifier, SpatialBinningModifier

Load file

pipeline = import_file(“/home/sepideh/projects/Uncertainty_propagation/MD/Wendian/Al-3Cu/GB-1-11/_1_10/traj/five.0.lammpstrj”, multiple_frames=False)

Print available properties before computing

data_raw = pipeline.compute()
print(“Available particle properties:”, data_raw.particles.keys())

Compute T1 = 1 for atoms of type 1

compute = ComputePropertyModifier()
compute.output_property = ‘T1’
compute.expressions = [‘ParticleType == 1 ? 1 : 0’]
pipeline.modifiers.append(compute)
data = pipeline.compute()

Add spatial binning

binning = SpatialBinningModifier()
binning.property = “T1”
binning.direction = SpatialBinningModifier.Direction.Z
binning.reduction_operation = SpatialBinningModifier.Operation.Sum
binning.bin_count_x = 1
pipeline.modifiers.append(binning)

Recompute with modifiers

data = pipeline.compute()

Show particle count for debugging

print(“Total particles:”, data.particles.count)
if ‘T1’ in data.particles:
print(“Sum of T1 (should be number of type 1 atoms):”, data.particles[‘T1’].array.sum())
else:
print(“T1 property not created — check ParticleType availability.”)

Check if spatial binning result exists

if “Spatial binning: T1” in data.grids:
grid = data.grids[“Spatial binning: T1”]
print(grid.field[:, 0, 0]) #
else:
print(" Spatial binning failed: No voxel grid was generated for ‘T1’.“)
print(” Possible reasons: no atoms of type 1, invalid bin range, or missing property.")

And I receive the error:
Available particle properties: KeysView(Particles())
Total particles: 33024
Sum of T1 (should be number of type 1 atoms): 32350.0
Spatial binning failed: No voxel grid was generated for ‘T1’.
Possible reasons: no atoms of type 1, invalid bin range, or missing property.

It computes the property T1 but does not perform binning. I do not know where I am making the mistake

five.0.lammpstrj (1.1 MB)
I uploaded the lammpstrj file for your reference

Could you please format your source code above? See Posting code or preformatted text - Using Discourse - Discourse Meta. Thank you!

Please take a look at the docs of the SpatialBinningModifier class. When set to SpatialBinningModifier.Direction.Z, the modifier creates a 1-dimensional DataTable object, not a 2- or 3-dimensional VoxelGrid object. This data table can be accessed as follows:

table = data.tables['binning']
z = table['Position'] # z-coordinates of the bins
T1 = table['T1'] # Bin values
from ovito.io import import_file
from ovito.modifiers import ComputePropertyModifier, SpatialBinningModifier

# Load file
pipeline = import_file("/home/sepideh/projects/Uncertainty_propagation/MD/Wendian/Al-3Cu/GB-1-11/_1_10/traj/five.0.lammpstrj", multiple_frames=False)

# Print available properties before computing
data_raw = pipeline.compute()
print("Available particle properties:", data_raw.particles.keys())

# Compute T1 = 1 for atoms of type 1
compute = ComputePropertyModifier()
compute.output_property = 'T1'
compute.expressions = ['ParticleType == 1 ? 1 : 0']
pipeline.modifiers.append(compute)
#data = pipeline.compute()
# Add spatial binning
binning = SpatialBinningModifier()
binning.property = "T1"
binning.direction = SpatialBinningModifier.Direction.Z
binning.reduction_operation = SpatialBinningModifier.Operation.Sum
binning.bin_count_z = 20
pipeline.modifiers.append(binning)

# Recompute with modifiers
data = pipeline.compute()

# Show particle count for debugging
print("Total particles:", data.particles.count)
if 'T1' in data.particles:
    print("Sum of T1 (should be number of type 1 atoms):", data.particles['T1'].array.sum())
else:
    print("T1 property not created — check ParticleType availability.")

# Check if spatial binning result exists
if "Spatial binning: T1" in data.grids:
    grid = data.grids["Spatial binning: T1"]
    print(grid.field[:, 0, 0])  # 
else:
    print(" Spatial binning failed: No voxel grid was generated for 'T1'.")
    print(" Possible reasons: no atoms of type 1, invalid bin range, or missing property.")

Thanks for your response. But I am using ovito3.0.0 and it does not save output in table

The docs say it does, but the identifier is a different one: data.tables['binning[T1]']

See OVITO 3.0.0-dev815 documentation: https://ovito.org/docs/3.0.0/python/modules/ovito_modifiers.php#ovito.modifiers.SpatialBinningModifier

But by the way, what’s the reason you are still using the 3.0.0 version of the OVITO Python module? Why not the latest one?