VTK 8.2 Python and VSCode

Hi,

I am unable to get VTK Python autocompletion to work properly using VS Code.
I am using VTK Python 8.2 with VSCode 1.33 on Windows 10 64 bits.

Using import vtk, I get the following result:
image

My Python environment is actually a conda environment, with libs like VTK, PyQt or HDF5 installed. The later ones are working with Intellisense.

Code using VTK works perfectly though, only autocompletion features are not.

Any idea?

The vtk package does dynamic loading of its components. I recommend using vtkmodules in 8.2. Completion should work there.

Yes I saw that, thanks. But since the updated wiki examples are still using the old ‘import vtk’, I thought this was the proper way.

Edit: even using vtkmodules does not work properly. I am able to get the list of classes of a given module (eg vtkFiltersGeneral), but then, when instantiating an object and trying to use it, no class members are available.

1 Like

Ah, we should update the wiki examples. @lorensen

Hmm. I wonder if we’re missing __all__ or something on the generated classes. However, I get completion in ipython, so maybe VSCode expects something different? @dgobbi

Autocompletion is on my to-do list for VTK 9, I’ve done a bit of work on it but it’s still a ways off because I haven’t had much time to dedicate to VTK recently.

The most robust method for supporting autocompletion is to generate .pyi files as described in PEP 484, so that’s the approach I am taking, but I’m also re-vamping the docstring generation.

1 Like

Ok I understand, thanks both of you for your quick response!

I can reproduce exactly this, autocompletion works for imported classes, but not for instantiated objects (not in ipython either). What is missing? Any tweak/workaround?

Hi,
I wanted to ask if there had been any updates.
I saw @dgobbi mentioned autcompletion might be available in vtk 9. I used pip install vtk in my conda environment to get vtk==9.0.3.

But autocompletion for import vtk still wasn’t available in PyCharm, PyDev on Eclipse, nor on VsCode.

@ben.boeckel suggests using import vtkmodules, but this works only partially.

An example of it working,
Let’s look at the example from https://kitware.github.io/vtk-examples/site/Python/GeometricObjects/Cone/ You being with import vtk and then call vtk.vtkConeSource()

Because you cannot call vtkmodules.vtkConeSource(), I had to google a bit.
This was relatively simple, I googled to find out that vtkNamedColors lived in vtkCommonColor then I called vtkmodules.vtkCommonColor.vtkNamedColors()

An example of it being unusable
Unfortunately, using vtkmodules I can’t find a lot of paths.
From the same example, https://kitware.github.io/vtk-examples/site/Python/GeometricObjects/Cone/ , there is the line mapper = vtk.vtkPolyDataMapper()
But, I can’t find a vtkmodules equivalent. I tried vtkmodules.vtkPolyDataMapper() but that did not work.
I googled, again, to find where vtkPolyDataMapper lives.
I saw vtkPolyDataMapper lives in vtkMapper. I tried vtkmodules.vtkMapper and vtkmodules. vtkAbstractMapper3D but neither were autocompleted nor appeared to be a valid path so I’m not sure what an equivalent vtkmodules path is for mapper = vtk.vtkPolyDataMapper()

Is there autocompletion for vtk? If so, how can I set it up.

If I must use vtkmodules, how can I find equivalent paths between it and vtkmodules since vtkmodules doesn’t seem to own all the packages that vtk does.

I apologize if these are ignorant questions. I am a new user.

Thank you,
CoffeeCup

I suspect there’s some tooling missing here. Aren’t there .pyi files now? I remember there being some mention of generating these files as well, but I don’t know the status there.

I have a script for generating .pyi files, but it’s still a work-in-progress. If you’re curious, the script is here: make_vtk_pyi.py (8.5 KB)

In addition to that script, the docstrings generated by the wrappers also need to be updated, which is happening in MR 7983.

Finally, of course, the cmake machinery for generating the .pyi files for each module needs to be written.

This has been on my to-do list for an embarrassingly long time. It will eventually be done, and when it is, I will definitely need people to test it because there are are at least a half-dozen different tools and IDEs that do type-checking and autocompletion, and each behaves differently from the others.

Thank you for responding so quickly!
I was searching but I don’t see any.

Hey David!

Thanks so much for responding so quickly! I’m very unfamiliar with c++. It sounds like the autocompletion won’t be available until each module has custom c++ code that interacts w/ the make_vtk_pyi.py script, correct?

In the meantime, is there a quick way to know what the corresponding paths for any vtk.whatEver are inthe vtkmodules equivalent?

dir(vtkmodules.vtkCommonCore should work. For knowing which module a specific class comes from…I’d search the source tree to find the file, but this may not be as handy for non-developers :confused: . The documentation might help here? It looks like the easiest way is to go to the class (e.g., vtkAMRDataSetCache, click on the #include link at the top and see the VTKIOAMR_EXPORT declaration. This indicates it is in the vtkIOAMR module. Note that the _EXPORT symbol is all-caps and the module name usually has camel-case (except for abbreviations where it is all caps).

1 Like

The modules are C++, but the docstrings aren’t custom-made for each module, they’re automatically generated. So the amount of work involved (on my part) does not increase as more modules are added. All the labour goes into the automation.

Finding the module for a Python class is easy:

>>> vtk.vtkRenderer.__module__
'vtkmodules.vtkRenderingCore'
1 Like

Hey Ben,
Thanks for responding so promptly.

Ignorant question.
If I look at the docs for vtkPolyDataMapper, I get

But if I do vtkPolyDataMapper.__module__ I get:
vtkmodules.vtkRenderingCore

I am a bit confused. Why is it that vtkPolyDataMapper is in the module vtkmodules.vtkRenderingCore but the docs don’t have vtkRenderingCore as a parent.