OVITO > Support Forum

Modifying particle type name in latest Beta


Hi all!

I use a python modifier to rename the bond and particle names to more meaningful names. Those names are read from a file that is stored next to the trajectory. With the latest Beta (3.0.0-dev349), I am running into the "Please explicitly request a mutable version" problem, so I modified the code like so:

--- Code: ---
        for defect_type in data.particles_['Particle Type_'].types:
                defect_type.name = defect_names[defect_type.id]
            except KeyError:
                print("Unknown id: ",defect_type.id)
            except Exception as e:
                print("ERROR: %s"%str(e))
    except Exception as e:
        print("ERROR: %s"%str(e))
--- End code ---

Alexander Stukowski:

Catching the exception won't help. The ParticleType object, whose name you are trying to change, is locked by default. This is to prevent accidental and permanent modification of data that actually belongs to the upstream pipeline. Modifiers are only allowed to modify copies of data, which are generated along the way down the pipeline. They can never alter the original input data objects.

That being said, here is how you can request a copy of the ParticleType object, which can then be safely modified within the modifier function:

--- Code: ---    ptype_property = data.particles_['Particle Type_']
    for defect_type in ptype_property.types:
        mutable_defect_type = ptype_property.make_mutable(defect_type)
        mutable_defect_type.name = defect_names[defect_type.id]

--- End code ---

The code uses the yet undocumented DataObject.make_mutable() method, which allows you to turn a sub-object into a modifiable version. In this case the ParticleType object is a sub-object of the Property object. That's why we call make_mutable() on the ptype_property. The method basically has the same effect as the '_' notation, just doing it explicitly.

Note that this code does not rename the original particle types. It renames copies of these types that are made along the data pipeline. Thus, the changes will only be visible to modifiers following your Python modifier function in the pipeline.

If you would instead like to modify the original particle types, you would have to do it from outside of a modifier function.

Let me know if you have any further questions.



[0] Message Index

Go to full version