Forum Navigation
You need to log in to create posts and topics.

use variable in modifier

Dear all,

i have a simple script to cut a cylindrical shape   from my  simulation box:

pipe = import_file('my_file')
pipe.modifiers.append(ExpressionSelectionModifier(expression = 'sqrt((Position.X-5)*(Position.X-5)+(Position.Y-4)*(Position.Y-4))>10'))
data = pipe.compute()

the code above selects a cylinder with z-axis, with the center at (x=5.y=4) and the radius r=10.

but what if I need many cylinders with random coordinates (for the center of the cylinder ) e.g., x and y to be variables that I can assign random values.

kind regards,


Please take a look at the documentation of the ExpressionSelectionModifier.expressions field. This part of the documentation has recently been expanded to explain how Python variables can be incorporated into the expression.


Dear Alex





Dear Vardan,

Alternatively you can use my PythonScriptModifier that constructs a cylinder connecting two coordinates with a given radius. It can be called repeatedly with the "add_to_selection" flag to cut multiple cylinders from a given sample.

from import *
from ovito.modifiers import ExpressionSelectionModifier

def modify(frame: int, data: DataCollection, radius=2.0, p1=(0.0, 0.0, 0.0),
           p2=(0.0, 0.0, 0.0), add_to_selection=True):
    x1, y1, z1 = p1
    x2, y2, z2 = p2
    xn, yn, zn = x2-x1, y2-y1, z2-z1

    denom = (x2-x1)**2+(y2-y1)**2+(z2-z1)**2
    enum1 = f'((Position.Y-{y1})*(Position.Z-{z2})-(Position.Z-{z1})*(Position.Y-{y2}))^2'
    enum2 = f'((Position.Z-{z1})*(Position.X-{x2})-(Position.X-{x1})*(Position.Z-{z2}))^2'
    enum3 = f'((Position.X-{x1})*(Position.Y-{y2})-(Position.Y-{y1})*(Position.X-{x2}))^2'

    cylinder = f'({enum1}+{enum2}+{enum3})/({denom})<={3*radius**2}'
    base = f'{xn}*(Position.X-{x1})+{yn}*(Position.Y-{y1})+{zn}*(Position.Z-{z1}) > 0'
    top = f'{xn}*(Position.X-{x2})+{yn}*(Position.Y-{y2})+{zn}*(Position.Z-{z2}) < 0'

    if add_to_selection and 'Selection' in data.particles.keys():
        expr = f'(Selection==1) || ({cylinder} && {base} && {top})'
        expr = f'{cylinder} && {base} && {top}'

This and other useful modifiers can be found on my github page:


Uploaded files:
  • Screenshot-from-2022-05-10-14-15-22.png

Dear Daniel







New for our users in China: OVITO on WeChat 

Official OVITO WeChat channel operated by Foshan Diesi Technology Co., Ltd.