Executing commands from the optiSLang Python API#
When optiSLang is active, you can send individual Python commands to it as a genuine Python snippet. For example, this code gets general information about the sensitivity actor:
from ansys.optislang.core import Optislang
osl = Optislang()
print(osl.application.project.run_python_script("""help(actors.SensitivityActor)"""))
osl.dispose()
Note
Be aware that each time the
run_python_script()
method is called, a new Python context is created inside optiSLang. This
means that variables from previous calls won’t be available.
For longer scripts, instead of sending a string to optiSLang as in the preceding example, you can send the path to the Python script. This script is converted to a string automatically. For example, the following code creates a workflow with variable actors and a calculator. For more information, see the Simple calculator example.
from ansys.optislang.core import Optislang
from ansys.optislang.core import examples
osl = Optislang()
path_to_file = examples.get_files("simple_calculator")[0]
osl.application.project.run_python_file(file_path=path_to_file)
osl.dispose()