# Add an OSPRay material via Python

**URL:** https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673
**Category:** ParaView Support
**Created:** [February 26, 2020, 6:47pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673 "2020-02-26T18:47:15Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Andy\_Bauer](https://discourse.paraview.org/user_avatar/discourse.paraview.org/andy_bauer/32/5442_2.png) [@Andy\_Bauer](https://discourse.paraview.org/u/Andy_Bauer)
#### Post date: [February 26, 2020, 6:47pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/1 "2020-02-26T18:47:15Z")

</div>

Hi,

Is there a way to add an OSPRay material via Python? I’m working on a `pvpython` script for an automated workflow but the materials aren’t in the default list. I found some online that I like but I’m expecting multiple users to use the script from a variety of locations so being able to add materials through Python would really simplify things. FYI: I’m hoping to not have to create a .json file and then read that in.

Thanks,  
Andy

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [February 27, 2020, 4:08am UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/2 "2020-02-27T04:08:53Z")

</div>

@Michael

---

<div class="post-metadata">

### Author: ![mconti](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mconti/32/2168_2.png) [@mconti](https://discourse.paraview.org/u/mconti)
#### Post date: [February 27, 2020, 4:21pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/3 "2020-02-27T16:21:38Z")

</div>

i’d be very interested in this capability as well. Please let me know if you figure out a workflow for this.

---

<div class="post-metadata">

### Author: ![Andy\_Bauer](https://discourse.paraview.org/user_avatar/discourse.paraview.org/andy_bauer/32/5442_2.png) [@Andy\_Bauer](https://discourse.paraview.org/u/Andy_Bauer)
#### Post date: [February 27, 2020, 4:30pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/4 "2020-02-27T16:30:52Z")

</div>

You can load a material file via Python commands like:

```auto
materialLibrary1 = GetMaterialLibrary()
materialLibrary1.LoadMaterials = <file>

```

That’s the best I have so far so my solution will likely be write out a json file with the two materials I need and then load that in. The Python Trace feature should capture everything else you need to properly set path tracer materials. If I figure out something better I’ll let you know…

---

<div class="post-metadata">

### Author: ![Dave\_DeMarle](https://discourse.paraview.org/user_avatar/discourse.paraview.org/dave_demarle/32/27_2.png) [@Dave\_DeMarle](https://discourse.paraview.org/u/Dave_DeMarle)
#### Post date: [February 29, 2020, 5:49pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/5 "2020-02-29T17:49:18Z")

</div>

At the ParaView level, there isn’t an API to programmatically add new materials yet other than by loading new ones from file. There is at the VTK level though, so it shouldn’t be very difficult to punch a hole down and do it. The following is a sketch for how we can go about it.

This [c++ test at the VTK level](https://gitlab.kitware.com/vtk/vtk/blob/master/Rendering/RayTracing/Testing/Cxx/TestPathTracerMaterials.cxx) demonstrates creating new materials from scratch by making calls on the VTK level [vtkOSPRayMaterialLibrary](https://vtk.org/doc/nightly/html/classvtkOSPRayMaterialLibrary.html) class.

This [class at the ParaView implementation level](https://gitlab.kitware.com/paraview/paraview/blob/master/Remoting/Views/vtkPVMaterialLibrary.h) is a handle for the underlying VTK level instance. It adds the feature of loading the default materials at startup time. It does not expose any way to access the contained VTK level instance - yet. Alternatively we could add a passthrough API to construct new materials here. This is the primary thing that needs to be done.

This [class at the ParaView proxy level](https://gitlab.kitware.com/paraview/paraview/blob/master/Remoting/Views/vtkSMMaterialLibraryProxy.cxx) manages the implementation level handle, and makes it do the right thing in client/server sessions. Most likely nothing needs to be done with this other than expose the implementation level’s extended API in the rendering.xml file. We do have to at least check and make sure that whatever we do does work correctly in client server configurations though, most likely by calling Synchronize() here.

---

<div class="post-metadata">

### Author: ![Dave\_DeMarle](https://discourse.paraview.org/user_avatar/discourse.paraview.org/dave_demarle/32/27_2.png) [@Dave\_DeMarle](https://discourse.paraview.org/u/Dave_DeMarle)
#### Post date: [February 29, 2020, 6:58pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/6 "2020-02-29T18:58:43Z")

</div>

Also I might add that adding this feature would be a good start on the concept of adding a GUI to let you define new materials within ParaView, although I really think that is best left to a dedicated mini application.

@Michael

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.paraview.org/user_avatar/discourse.paraview.org/michael/32/16_2.png) [@Michael](https://discourse.paraview.org/u/Michael)
#### Post date: [March 2, 2020, 9:29am UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/7 "2020-03-02T09:29:15Z")

</div>

I have a prototype that let the user create materials interactively with a dock widget and use ParaView proxy so python can be used.  
I didn’t have time to finish it though because it was more difficult to handle textures properly and I was running out of time/funding.

Note that it is already possible in ParaView 5.8 to create OBJMaterial or Principled materials interactively (or with python) but some properties are not exposed yet.

Principled is probably the most generic and powerful material so I recommend using it.  
The exposed properties of Principled in ParaView are baseColor, metallic, diffuse, roughness, specular, baseNormal, opacity.  
For example, the following image has been produced using different Principled materials, using only ParaView GUI, without json material library:

 ![2020-03-02-123419_1234x696](https://discourse.paraview.org/uploads/default/original/2X/9/9ec73029c75c3fa65bf1d3612e41eb1b894e31d7.png)

---

<div class="post-metadata">

### Author: ![Vincent\_Rivola](https://discourse.paraview.org/user_avatar/discourse.paraview.org/vincent_rivola/32/2007_2.png) [@Vincent\_Rivola](https://discourse.paraview.org/u/Vincent_Rivola)
#### Post date: [March 2, 2020, 12:17pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/8 "2020-03-02T12:17:02Z")

</div>

Hi Michael,

Where can we find the Principled material editor in 5.8? I can’t find it anywhere.

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.paraview.org/user_avatar/discourse.paraview.org/michael/32/16_2.png) [@Michael](https://discourse.paraview.org/u/Michael)
#### Post date: [March 2, 2020, 1:30pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/9 "2020-03-02T13:30:23Z")

</div>

Hi Vincent,  
Sorry I didn’t mention it.

Everything can be configured under the Lighting group of the representation properties.  
If you select Gouraud, the material is translated to OBJMaterial.  
If you select PBR, it is Principled.  
The baseColor uses the current Solid color (or the current color map), the opacity is configured under the Styling property group.

![2020-03-02-124021_354x382](https://discourse.paraview.org/uploads/default/original/2X/b/b58c3ca2aee5d3991293cef64f4fc052edd9b633.png)

If you want to use a texture for roughness or metalness, you have to set the Material Texture in ORM format (roughness is the green channel, metalness is the blue channel)  
This blog article on PBR/Principled materials is not focused on OSPRay but it might help: [https://blog.kitware.com/vtk-pbr/](https://blog.kitware.com/vtk-pbr/)

---

<div class="post-metadata">

### Author: ![mconti](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mconti/32/2168_2.png) [@mconti](https://discourse.paraview.org/u/mconti)
#### Post date: [March 2, 2020, 2:17pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/10 "2020-03-02T14:17:37Z")

</div>

@Michael the new GUI updates to allow direct interaction with OBJ and Principled materials is great. Especially for testing out textures, this is so much easier than making a ton of JSON files.

Is there a way to save out these materials created through the GUI by chance?

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.paraview.org/user_avatar/discourse.paraview.org/michael/32/16_2.png) [@Michael](https://discourse.paraview.org/u/Michael)
#### Post date: [March 2, 2020, 2:27pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/11 "2020-03-02T14:27:20Z")

</div>

Not yet, sorry!

---

<div class="post-metadata">

### Author: ![Vincent\_Rivola](https://discourse.paraview.org/user_avatar/discourse.paraview.org/vincent_rivola/32/2007_2.png) [@Vincent\_Rivola](https://discourse.paraview.org/u/Vincent_Rivola)
#### Post date: [March 2, 2020, 3:34pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/12 "2020-03-02T15:34:37Z")

</div>

Ok I see! I already used PBR but I did not know it was actually an OSPRAY principled material in the background…

---

<div class="post-metadata">

### Author: ![jone](https://discourse.paraview.org/user_avatar/discourse.paraview.org/jone/32/9307_2.png) [@jone](https://discourse.paraview.org/u/jone)
#### Post date: [October 7, 2020, 11:20am UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/13 "2020-10-07T11:20:43Z")

</div>

Is this method independent of OSPRay? So,does it use a GPU for rendering?

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.paraview.org/user_avatar/discourse.paraview.org/michael/32/16_2.png) [@Michael](https://discourse.paraview.org/u/Michael)
#### Post date: [October 7, 2020, 12:59pm UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/14 "2020-10-07T12:59:15Z")

</div>

Yes, PBR is independent of OSPRay, it is an OpenGL implementation of the Principled material.

---

<div class="post-metadata">

### Author: ![jone](https://discourse.paraview.org/user_avatar/discourse.paraview.org/jone/32/9307_2.png) [@jone](https://discourse.paraview.org/u/jone)
#### Post date: [October 8, 2020, 6:33am UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/15 "2020-10-08T06:33:15Z")

</div>

Thanks your reply.  
This function looks like blender’s default material shader node( Principled BSDF )similar,but Paraview does not support directly read has produced good Disney principled material, for example .exr format file, could you tell me how can I add materials to the model in Paraview, or are there tutorials on this?

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.paraview.org/user_avatar/discourse.paraview.org/michael/32/16_2.png) [@Michael](https://discourse.paraview.org/u/Michael)
#### Post date: [October 8, 2020, 7:12am UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/16 "2020-10-08T07:12:29Z")

</div>

You will be able to open .hdr files in ParaView 5.9  
There is no plan to support .exr files yet.

---

<div class="post-metadata">

### Author: ![jone](https://discourse.paraview.org/user_avatar/discourse.paraview.org/jone/32/9307_2.png) [@jone](https://discourse.paraview.org/u/jone)
#### Post date: [October 8, 2020, 7:16am UTC](https://discourse.paraview.org/t/add-an-ospray-material-via-python/3673/18 "2020-10-08T07:16:28Z")

</div>

That’s great. Thank you very much for the work you’ve done.
