Shaders in Python

I am trying to write a script in Python 3 to use shaders (GLSL) in VTK.

When I google, I see a lot of stuff that does not seem to be valid anymore. I see mention of:

vtkShader
vtkShaderProgram
vtkShader2
vtkShader2Collection
vtkShaderProgram2
vtkGLSLShaderDeviceAdapter2
vtkOpenGLShaderCache
vtkOpenGLShaderState

The examples are in C++ and have include directives for these .h header files. There few, if any Python 3 examples. The only Python package/module that can be imported are vtk and vtkmodules. Importing vtk seem to imply importing vtkmodules, based on len(sys.modules)

Importing vtk, I can not find any modules, classes or methods with ‘shader’ in the name.

And both vtk and vtkmodules have an ‘all’ sub-module, but:

from vtk import all

get a lot more than:

import vtk

len(sys.modules) is 166 after import vtk and 293 after import all from vtk.

I am confused about how to use GLSL shaders with VTK in Python3. All of this is done using the Python 3.9.13 that came with ParaVIew 5.11.1. Please help!

Thank you.

These classes are in the RenderginOpenGL2 module (except vtkGLSLShaderDeviceAdapter2…not sure where that comes from.

Something like:

from vtkmodules.vtkRenderingOpenGL2 import vtkShader

Yes! Thank you. I meant to come back and answer my own question, but I wanted to get my code working first.

What I learned, for the sake of others who have the same question(s). Please correct me if I am wrong about anything.

  1. In Python, the vtk and vtkmodules packages allow access to the VTK objects. However they are (very) different versions. vtkmodules has most or all of vtk, plus a whole lot more. In addition, everything is broken down in to sub-modules for better organization.

  2. Sub-modules are not imported by default, allowing finer grain importing. I have to import each class I need, but that means I don’t have to import everything.

  3. The example code I was starting from imported the vtk module. Our installation of python allowed that, but the actual libraries couldn’t be found. Either they aren’t there or require a different environment to get to them.

  4. I was able to find all of the calls in the example code within sub-modules and was able change them to work with vtkmodules.

  5. Some things have change such that my example code can’t run with vtkmodules. For example, it seem one used to be able add a ShaderProgram to an actor, but in the current version, actor does not have a SetShaderProgram() method. I am currently working on learning the right way to do this and other things using vtkmodules.

  6. The calss documentation on vtk.org make a lot more sense now that I understand the organization.

vtk is the “old” module. However, there wasn’t a way to make that compatible with the preferred way of only importing what is actually needed that vtkmodules allows. It is basically from vtkmodules.all import *, so the VTK classes should be no different between the two.

You might need a vtkOpenGLActor to get that method. Due to the object factories, you may need to import vtkRenderingOpenGL2 to ensure that the overrides are available there.

I try to avoid import *, except maybe in the interpreter. I mean, it sure would save a lot of typing, but I like separate namespaces.

I couldn’t find a SetShaderProgram() method in an vtkOpenGLActor (I tabbed for completion candidates).

>>> from vtkmodules import vtkRenderingOpenGL2
>>> ogl2a=vtkRenderingOpenGL2.vtkOpenGLActor()
>>> ogl2a.SetS
ogl2a.SetScale(           ogl2a.SetShaderProperty(  
>>> ogl2a.SetS

I also searched, I think, all of the different actors, and all of them only have SetShaderProperty(). There is a vtkShaderProgram class. I can’t find ‘SetShaderProgram’ anywhere in the VTK 9.2 documentation. VTK 7.1 is the last version I can find mention of it.

Indeed. I’m not sure of the intended route here. @Sankhesh_Jhaveri?

The shader program is created and managed internally by the render window based on the shader properties set on the mapper.

To set custom shader code, use shaderProperty.set[Fragment/Vertex]ShaderCode(glsl_string).

For an example, look at TestUserShader2.py

For future reference, VTK related issues should be posted to VTK’s discourse

I’m mixing paradigms. I will follow the example for my code. Thanks!

My apologies I posted here because eventually, I want use what I’m working on in Paraview, so that’s where my mindset was. I will post pure VTK questions the VTK discourse.