Local libraries with Python wrapping

I’m after advice on the current recommended route for creating a local library for which the
classes can be wrapped [Python]
Previously releases provided an
Examples/Build/vtkLocal directory that could be used as a template for local libraries.
This seems to have been deprecated at least its not in the git archive.
The replacement Examples/Build/vtkMy provides scaffolding for a local library
but des not seem to support wrapping, at least not via Python. The classes in the
example library are used via C++. Am I missing something, or is there an
example somewhere of a local library with wrapped classes that I could follow?

I’ve nothing against going back to 1st principles and starting again from CMake
but there used to be a good solution and if the problem is already solved
its unnecessarily wasteful.

thanks,
David

The new example is here. In VTK 9, the wrapping of local libraries is more-or-less automated (more so than before), but the local libraries must be structured as a VTK module.

In the example, the important pieces are:

  1. A subdirectory (the module, any name can be used) with a vtk.module file
  2. A CMakeLists.txt file with vtk_module_wrap_python to wrap the module

Thanks David, Exactly what I was looking for. However In trying to built the local example as included as a test
I’ve run into issues:

1.CMake Error at CMakeLists.txt:11 (include):
include could not find requested file:

 /vtkExternalModuleMacros.cmake

If I look in /usr/local/libcmake/vtk that macro is indeed not present
I find it present when using 6.1, 6.3, 7.1, 9.0

If I cp the file to the 9.1 dir and rererun cmake I get

include could not find requested file:

/vtkExternalModuleMacros.cmake
made me wonder if the problem is in cmake concatenating
${VTK_CMAKE_DIR} with the filename . However
the core vtk build is fine. I’ve tried multiple versions
of Cmake.

2.from line 8 of CMakeLists
The VTK_USE_FILE is no longer used starting with 8.90

I’m using CMake 3.8.0 to satisfy the version constraint 3.8…3.12
I’m having the issues with both the VTK-9.0.1 tarball and the git head.

Any suggestions?
thanks,
David

Hi David, the example that I pointed to is Examples/Modules/Wrapping

It’s very possible that the old Examples/Build/vtkLocal is not working anymore.


Edit: The vtkLocal example was removed from ‘master’ in March, and it’s not in the current release branch either. So it’s a dead end, it’s been superseded by the Examples/Modules examples.

Thanks David for theclarifications, got that working now.

Thanks. I could get the example module built and working from boththe interactive vtkpython prompt and with the example python script
inthe Testing directory.
However if I try to add my own class to the module I get

AttributeError: module ‘wrapping.vtkWrappable’ has no attribute ‘vtkGrid’

where vtkGrid is the name of the class I want to add & instantiate

I suspect Ive missed a step, I

put the .h, .cc files in the module directory

ensured the header file was including the module header, and put

the appropriate export in the class declaration

UpdateCMakeLists to add the new class.

rerun CMake, built and installed.

The example class vtkWrappable is working fine, my class is not

have I missed anything, or am I using wrong steps to

instantiate froma module?

If anyone has time to look I attach a tarball

of my directory. Th new class is vtkGrid

Testing/import_grid.py is the python script I wrote to test,

based on the import_wrappable script

thanks, David

meta.tgz (38.9 KB)

You were close, there were just a couple errors holding you back.

Syntax error in CMakeLists.txt:

 set(classes
-  vtkGrid,vtkWrappable)
+  vtkGrid
+  vtkWrappable
+)

Missing dependency in vtk.module:

 NAME
   Wrapping::Wrappable
 LIBRARY_NAME
   vtkWrappable
 DEPENDS
   VTK::CommonCore
+  VTK::CommonExecutionModel

The VTK 9 wrapping puts the module (in this case, ‘vtkWrappable’) into a package (in this case, ‘wrapping’) so imports are like this:

-from wrapping import vtkWrappable
-from wrapping import vtkGrid
+from wrapping.vtkWrappable import vtkWrappable
+from wrapping.vtkWrappable import vtkGrid
 
-wrap = vtkWrappable.vtkWrappable()
+wrap = vtkWrappable()
 print( 'test=', wrap.GetString())
 print('instantiate grid')
-grid = vtkWrappable.vtkGrid()
-grid.PrintSelf()
+grid = vtkGrid()
+print(grid)

Note that the PrintSelf() isn’t called directly from Python (or from C++ either). In C++ use ‘grid->Print()’, and in Python use ‘print(grid)’.

Thanks David. Not clear this is strictly the same issue however I didn’t think it
worth creating a new thread

I’m running into a different problem now. Trying to build the
Examples/Modules/Wrappable from VTK9.1
Attempting to ccmake I get
but it set VTK_FOUND to FALSE so package “VTK” is considered to be NOT
FOUND. Reason given by package:

Could not find the VTK package with the following required components:
Python.

CMake Warning at CMakeLists.txt:10 (find_package):
Found package configuration file:
Skipping example: Could not find the VTK package with the following required components: Python.
VTK is built and installed and python scripts are working . Paths appear to be okay.

Any thoughts on what might trigger this error might help me track down the issue.

thanks
David

You haven’t given me much information to work with. Is your installed VTK one that you built yourself? What are the details of its configuration, e.g. BUILD_SHARED_LIBS, which version of Python, is there anything special about your environment?

I just ran a check on my own system (ubuntu 20.04, VTK 9.1, using ubuntu’s python 3.8) and Examples/Modules/Wrappable built just fine. My VTK was built with BUILD_SHARED_LIBS=ON and VTK_WRAP_PYTHON=ON and I used “make install” to do the installation.

When I configured the Wrappable example, I set VTK_DIR=<install-prefix>/lib/cmake/vtk-9.1 (the VTK_DIR should be set to the directory where vtk-config.cmake is located).

Hi David,
Sorry - I tried to do a clean build and think I found the problem.
When I try to run ccmake in the dir for the example Wrappable example I get

Parse error. Expected “(”, got newline with text "

I’ve looked at the CMake file with vi and od but can’t see

why its generating this error, would you check the attached please.

Although given the above they may be moot To answer your Qs from your reply post.

I’m using my own build of VTK9.1 I’m on OSX 11.6.1

I attach the CMake Cache from my vtk build

CMakeLists.txt (1.83 KB)

CMakeCache.txt (161 KB)

CMake usually gives a line number when it reports a syntax error, like the following:

CMake Error at CMakeLists.txt:27:
  Parse error.  Expected "(", got newline with text "

As for the error, it’s not in your main CMakeLists.txt, so maybe it’s in a CMakeLists.txt in a subdirectory?

That particular error means you have a stray word somewhere in the CMakeLists.txt that is neither followed by nor within parentheses. Valid cmake syntax:

AAAAA(BBBB CCCC ....)

Invalid syntax that will cause that parse error:

AAAAA

Apols for the delay in replying I tried diging deeper. I never isolated the location of the parse error but doing a build with a fresh copy cleared the error.
however I’m now stuck with

from wrapping import vtkWrappable

wrap = vtkWrappable()

wrap = vtkWrappable()

Traceback (most recent call last):

File “”, line 1, in

TypeError: ‘module’ object is not callable

I looked in /usr/local/lib/python3.10/site-packages:

README.txt pip-20.2.3.dist-info vtk.py vtkmy

pip vtk vtkmodules wrapping

wrappig contains vtkWrappable.so

my PYTHONPATH is set to

/usr/local/lib/python3.10/site-packages/:/usr/local/lib/python3.10/site-packages/wrapping/:/usr/local/lib/python3.10/site-packages/vtkmodules/

If it helps I attach a tarball of the directory that I’m building the Wrappable module - should not be

an issue as its a direct copy of /Examples/Modules/Wrapping

I also attach the CMake Cache from my VTK9.1 build

thanks

David

CMakeCache.txt (161 KB)

meta.tar (1.02 MB)

Try this sample code, which I posted above (as a diff), on Nov 1st:

from wrapping.vtkWrappable import vtkWrappable
from wrapping.vtkWrappable import vtkGrid

wrap = vtkWrappable()
print( 'test=', wrap.GetString())
print('instantiate grid')
grid = vtkGrid()
print(grid)

To break this down:

  • wrapping is the package (the PYTHON_PACKAGE as set in CMakeLists.txt)
  • wrapping.vtkWrappable is the module (as set in vtk.module and elsewhere)
  • wrapping.vtkWrappable.vtkWrappable is the class (as declared in vtkWrappable.h)

So, the following gives a class named ‘vtkWrappable’:

from wrapping.vtkWrappable import vtkWrappable
wrap = vtkWrappable()

While this gives a module named vtkWrappable, of which the class is a member:

from wrapping import vtkWrappable
wrap = vtkWrappable.vtkWrappable()

The example really should not have used the same name for the module and the class, since that’s what appears to be causing the confusion.