Author Topic: Modifying particle type name in latest Beta  (Read 153 times)

clemo

  • Newbie
  • *
  • Posts: 2
Modifying particle type name in latest Beta
« on: March 15, 2019, 01:56:05 PM »
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: [Select]

    try:
        for defect_type in data.particles_['Particle Type_'].types:
            try:
                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))

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 612
Re: Modifying particle type name in latest Beta
« Reply #1 on: March 15, 2019, 03:28:13 PM »
Hi,

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: [Select]
    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]

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.

-Alex
« Last Edit: March 15, 2019, 03:31:05 PM by Alexander Stukowski »

clemo

  • Newbie
  • *
  • Posts: 2
Re: Modifying particle type name in latest Beta
« Reply #2 on: March 22, 2019, 11:14:21 AM »
Okay, cool!

That cleared a few things up. I realized, I actually don't need to modify the source but can instead work with the data at the end of the pipeline. So your solution works perfectly!

Thanks!
Clemens