Author Topic: PythonScriptModifier / Custom Modifier: Import not working ?  (Read 228 times)

RU

  • Newbie
  • *
  • Posts: 12
PythonScriptModifier / Custom Modifier: Import not working ?
« on: August 28, 2018, 03:26:27 PM »
Hi,
when I try to run the example for the PythonScriptModifier (http://www.ovito.org/manual/python/modules/ovito_modifiers.html#ovito.modifiers.PythonScriptModifier), I get an error because the ParticleProperty is not defined.
I changed the example slightly to use the existing node:
Code: [Select]
import ovito
from ovito.modifiers import PythonScriptModifier
from ovito.data import ParticleProperty

# Load input data and create an ObjectNode with a modification pipeline.
node = ovito.dataset.selected_node

# Define our custom modifier function, which assigns a uniform color
# to all particles, just like the built-in Assign Color modifier.
def assign_color(frame, input, output):
    color_property = output.create_particle_property(ParticleProperty.Type.Color)
    color_property.marray[:] = (0.0, 0.5, 1.0)

# Insert custom modifier into the data pipeline.
modifier = PythonScriptModifier(function = assign_color)
node.modifiers.append(modifier)

This is the error message:
Code: [Select]
The Python script has exited with an error.
Traceback (most recent call last):
  File "C:\CODE\ovito\python_mod_text.py", line 11, in assign_color
    color_property = output.create_particle_property(ParticleProperty.Type.Color)
NameError: name 'ParticleProperty' is not defined

From your documentation I understood that everything that is imported/loaded at the beginning should be known within the method, or am I missing something?
Can you help me with this error?

Thanks!
RU

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 500
Re: PythonScriptModifier / Custom Modifier: Import not working ?
« Reply #1 on: August 28, 2018, 08:38:20 PM »
Dear RU,

Are you running this script within a graphical program session via the "Run Script File" function from the menu of Ovito 2.9.0?

Under these conditions I was able to reproduce the error, which seems to be due to an old bug in the Python interface. Python modules imported outside of the user-defined modifier function are not visible inside the function. The problem does not occur if you run the script outside of a graphical program session using the "ovitos" interpreter. And in Ovito 3.0 the problem has been solved altogether.

As a workaround, I suggest to import the module inside the assign_color() function:

Code: [Select]
def assign_color(frame, input, output):
    from ovito.data import ParticleProperty
    color_property = output.create_particle_property(ParticleProperty.Type.Color)
    color_property.marray[:] = (0.0, 0.5, 1.0)

-Alex

RU

  • Newbie
  • *
  • Posts: 12
Re: PythonScriptModifier / Custom Modifier: Import not working ?
« Reply #2 on: August 29, 2018, 02:54:02 PM »
Dear Alex,

Yes, I was running the GUI (v2.9.0) with the "Run Script File" function.

Thanks for the fast answer. I had the same idea and tried your workaround in advance. This also leads to an error:
Code: [Select]
The Python script has exited with an error.
Traceback (most recent call last):
  File "C:\CODE\ovito\python_mod_text.py", line 10, in assign_color
    from ovito.data import ParticleProperty
ImportError: __import__ not found

However, I will wait for a 3.x stable version or use the existing development version.

Best regards
RU