# Shaders in Python

**URL:** https://discourse.paraview.org/t/shaders-in-python/12459
**Category:** Development
**Tags:** opengl, python, vtk
**Created:** [July 6, 2023, 4:07pm UTC](https://discourse.paraview.org/t/shaders-in-python/12459 "2023-07-06T16:07:27Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![tjggriffin](https://discourse.paraview.org/user_avatar/discourse.paraview.org/tjggriffin/32/11986_2.png) [@tjggriffin](https://discourse.paraview.org/u/tjggriffin)
#### Post date: [July 6, 2023, 4:07pm UTC](https://discourse.paraview.org/t/shaders-in-python/12459/1 "2023-07-06T16:07:27Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.paraview.org/u/ben.boeckel)
#### Post date: [July 20, 2023, 5:49pm UTC](https://discourse.paraview.org/t/shaders-in-python/12459/2 "2023-07-20T17:49:15Z")

</div>

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

Something like:

```python
from vtkmodules.vtkRenderingOpenGL2 import vtkShader

```

---

<div class="post-metadata">

### Author: ![tjggriffin](https://discourse.paraview.org/user_avatar/discourse.paraview.org/tjggriffin/32/11986_2.png) [@tjggriffin](https://discourse.paraview.org/u/tjggriffin)
#### Post date: [July 20, 2023, 6:47pm UTC](https://discourse.paraview.org/t/shaders-in-python/12459/3 "2023-07-20T18:47:41Z")

</div>

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](http://vtk.org) make a lot more sense now that I understand the organization.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.paraview.org/u/ben.boeckel)
#### Post date: [July 20, 2023, 7:03pm UTC](https://discourse.paraview.org/t/shaders-in-python/12459/4 "2023-07-20T19:03:41Z")

</div>

> [@tjggriffin](#):
>
> 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.

`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.

> [@tjggriffin](#):
>
> 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.

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.

---

<div class="post-metadata">

### Author: ![tjggriffin](https://discourse.paraview.org/user_avatar/discourse.paraview.org/tjggriffin/32/11986_2.png) [@tjggriffin](https://discourse.paraview.org/u/tjggriffin)
#### Post date: [July 21, 2023, 1:44am UTC](https://discourse.paraview.org/t/shaders-in-python/12459/5 "2023-07-21T01:44:01Z")

</div>

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).

```auto
>>> 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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.paraview.org/u/ben.boeckel)
#### Post date: [July 21, 2023, 11:53am UTC](https://discourse.paraview.org/t/shaders-in-python/12459/6 "2023-07-21T11:53:26Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Sankhesh\_Jhaveri](https://discourse.paraview.org/user_avatar/discourse.paraview.org/sankhesh_jhaveri/32/3374_2.png) [@Sankhesh\_Jhaveri](https://discourse.paraview.org/u/Sankhesh_Jhaveri)
#### Post date: [July 21, 2023, 12:52pm UTC](https://discourse.paraview.org/t/shaders-in-python/12459/7 "2023-07-21T12:52:13Z")

</div>

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)`](https://vtk.org/doc/nightly/html/classvtkShaderProperty.html#a351f3b6dc04672f35f8a4ca3e90bc87e).

For an example, look at [TestUserShader2.py](https://gitlab.kitware.com/vtk/vtk/-/blob/fcc6af2c07bffcd918e89917f9aad592424dc846/Rendering/OpenGL2/Testing/Python/TestUserShader2.py)

For future reference, VTK related issues should be posted to [VTK’s discourse](https://discourse.vtk.org)

---

<div class="post-metadata">

### Author: ![tjggriffin](https://discourse.paraview.org/user_avatar/discourse.paraview.org/tjggriffin/32/11986_2.png) [@tjggriffin](https://discourse.paraview.org/u/tjggriffin)
#### Post date: [July 21, 2023, 4:32pm UTC](https://discourse.paraview.org/t/shaders-in-python/12459/8 "2023-07-21T16:32:24Z")

</div>

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.
