Python changing particle type names

Is it possible to set the names associated with each particle type (e.g. after reading a lammpstrj which has only types)? I tried to play around with make_mutable and the _ suffix, but my best guess was

data = pipeline.compute()
for new_name, t in zip(args.type_names, data.particles_.particle_types_.types):
    t.name = new_name

but I still get

Traceback (most recent call last):
  File "/home/cluster2/bernstei/src/work/ovito/pipeline_wrappers/./ovito_rdf.py", line 57, in <module>
    t.name = new_name
RuntimeError: You tried to modify a ParticleType object that is currently shared by multiple owners. Please explicitly request a mutable version of the data object by using the '_' notation or by calling make_mutable() on the parent object. See the documentation of the DataObject.make_mutable() method for further information.

I don’t see what else to try to make mutable - there’s apparently no types_ or name_ defined in the relevant objects, and everything else is already using a _ suffix.

You probably want to do the following in this case:

data = pipeline.compute()
type_property = data.particles_.particle_types_
for new_name, t in zip(args.type_names, type_property.types):
    type_property.make_mutable(t).name = new_name

The ParticleType objects are children of the Property data object. The generic DataObject.make_mutable() method may be used on the parent object to make one of its child objects mutable.

The “_” notation is a short-hand version of the make_mutable method and is available only in certain scenarios. In this particular case, where you want to iterate over all particle types without knowing their original names, there is no other way than calling make_mutable explicitly. Property.type_by_name_(), for instance, would also be a way to make a ParticleType mutable, but it requires specifying some name. In fact, this method performs a combined operation: Look-up by name followed by make_mutable.

Thanks @stukowski. With your suggested code it works, and I appreciate the help. Unfortunately, I just realized that perhaps I asked the wrong question. I am, in particular, calculating RDFs with CoordinationAnalysisModifier(..., partial=True), and I don’t have access to data until after the pipeline has run, at which time the original names (in this case just the types, since it was a lammpstrj) have already been baked into the column headings of the partial RDFs.

Is there any way to assign names to the types before the pipeline is run and creates the output data structures? Is that something I can pass to import_file if I know it’s reading a lammps dump trajectory?

You need to do the renaming of the particle types within the pipeline, by inserting a user-defined modifier function as the first step after file import - before adding the CoordinationAnalysisModifier to the pipeline. Have a look at this code snippet demonstrating the approach.

1 Like