Using Inviwo from within Python
While Python Processors allow you to embed Python code inside Inviwo processors, you can also use Inviwopy to use your Inviwo networks from within a Python application. This guide shows you first how to build Inviwopy, then how to load existing Inviwo workspaces for use in your Python scripts and lastly some ways to modify a loaded Inviwo network from within Python.
To use Inviwopy, you have to build the appropriate
.so yourself, since it is currently not available through Pypi. To do so, enable the
IVW_MODULE_PYTHON3QT CMake flags. Next you need to specify the Python executable to which the produced library shall be compatible in the
Note: Using Inviwopy with Anaconda environments:
PYTHON_EXECUTABLEflag to your environment’s executable (e.g.
If not set automatically, also adapt the
<conda env>/lib/libpython3.6m.so(according to your Python version).
Depending on your Python version and operating system this will generate two dynamic libraries with names similar to:
On Windows similarly named
.dlls are generated. Note that those libraries depend heavily on the other libraries in the
bin folder. Those libraries need to be in your
So make sure to add the following lines to the top of your Python script:
sys.path.append('<path to inviwo build>/bin')
Loading an Inviwo workspace
Once your Python interpreter finds the Inviwo libraries, you can just import Inviwopy in Python:
import inviwopy as ivw
import inviwopyapp as qt
From here you have to initialize a logger and the Inviwo application
lc = ivw.LogCentral()
cl = ivw.ConsoleLogger()
app = qt.InviwoApplicationQt()
The Inviwo application
app gives you access to the network and its components. You can load the example workspaces, in this case the Boron example, as follows:
app.network.load(app.getPath(ivw.PathType.Workspaces) + '/boron.inv')
From here on you can modify your network and its processors through
app.network.processors. All that is left to do is to actually run the network, after possible modifications have been made:
In order to expose some Properties without starting the full GUI, you can add a
PropertyListWidget and populate it as follows:
plw = ivw.qt.PropertyListWidget(app)
See the full example here.