Removing a modifier: OVITO Python API

Hi OVITO Developers!

I am using the Construct Surface Mesh Modifier to select and delete the surface atoms in my simulation box. I was able to visualize the constructed surface and output to a png file, and I would like to output the same image, but without the surface.

Or in general: I put in a modifier and it manifests visually, and I later want to remove it without actually creating a duplicate pipeline. (Option mentioned on this page in the manual, quoted “it is possible to change the parameters of modifiers in a pipeline at any time, or to remove modifiers from a pipeline again”). How do I do this?

To add a modifier the pipeline.append() function is available. Is there also a function named like pipeline.remove()? Sorry if this info already exists in the manual, I was unable to spot it.

I have also tried to remove the surface visual by accessing the surface transparency property like this:

surf_vis = pipeline.source.data.surfaces
surf_vis.surface_transparency=0

But this also didn’t work.

Could you please give me any tips?

Thanks!

Hi,
Thank you for your question.

You might seen already seen in the graphical user interface of OVITO that it is possible to turn off individual modifiers in the data pipeline by unchecking the box next to their entry in the pipeline editor. When you disable a modifier in this way, it will be completely skipped during pipeline evaluation and the modifier will have no effect on the output of the pipeline. That’s probably not what you want, because you still would like the Construct Surface Mesh modifier to select the surface atoms.

So what you need is to turn off is the visual element, which is generated by the modifier to visualise the computed surface mesh. In the GUI this can be done by unchecking the corresponding box next to the “Surface” entry in the pipeline editor.

Using the Python programming interface you can do the same. The DataVis class, from which all visual element types derive, provides the enabled attribute, which can be set to False to remove the visual element from rendered images. When using the ConstructSurfaceModifier, you should do the following:

modifier = ConstructSurfaceModifier(...)
pipeline.modifiers.append(modifier)
modifier.vis.enabled = False

Note that you can turn the display of a visual element on or off at any time. Such a change will not trigger a reevaluation of the data pipeline. It only affects the rendered image when you call Viewport.render_image(), for example.

You also asked how to remove a modifier from a pipeline again. This can be done using Python’s del statement for deleting an element from a list:

# Insert a modifier into the pipeline:
pipeline.modifiers.append(ConstructSurfaceModifier(...))
# Remove the first modifier from the pipeline by deleting the list item at index 0:
del pipeline.modifiers[0]

Alternatively, you can turn a modifier in a pipeline off without removing it by setting its enabled property to False:

pipeline.modifiers[0].enabled = False

Thanks Dr. Stukowski! I realised I had not responded to this, and I wanted to write back for the future reader of this thread that this issue is solved.

I tried your solution and it works perfectly! I would, however, like to add to your answer on how to turn off a modifier from a pipeline.

When a modifier is assigned to a variable, as you did in your answer:

modifier = ConstructSurfaceModifier(…)

It should, at any point in the script, be possible to turn this modifier off like this:

modifier.enabled = False

Using this snippet, however:

pipeline.modifiers[0].enabled = False

would work only for the last modifier added to the list, as you rightly pointed out.

Thanks once again for responding, that too in such great detail!

Best,
Praneeth