How to load external material for pathtracer from python

Hi Everyone,

I while back I read on one of the threads that materials could be imported from python using that feature:

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

I tried providing my json file containing my materials (provide the file path), but with no succes.
Or should that be the actual file object from open(‘path/to/my/file.json’) ?

Can someone give a bit more details about how to use this ?

Thanks in advance!

It should be the filename of the json file, not the file object. Do you get any error or warning messages when you try?

I tested 5.8.1 on windows a moment ago by downloading ParaView and renaming the materials directory. I could load the misplaced materials both from View->Python Shell and from pvpython, so there must be something different about our setups.

That what I did in the first place.
I also checked that the path I provided exists, which is the case.
ParaView does not complain about anything until the render happens.
Then I get an error that the material does not exists and it reverts to the default obj material.
I tried with 5.8.0, not 5.8.1 though, and on linux.
I’ll try with 5.8.1 in case this was solved in that release.
Or is it an issue with the linux release ?

I tested ParaView 5.8.0 on linux and was able to change materials from python too.

I suspect a mismatch in the path or the material names, either in your script or in your json file.

Try what you are doing from within View->Python Shell in the gui. Change the material filename, to force the property to update , load the materials file, and then render. Does the list of available material change or not? If not then something is going wrong with the path. If the list does change then something is wrong with the script or the file itself.

In my case when I refer to a material that isn’t in the file (“water” instead of “Water” here):

r.OSPRayMaterial = "Water’

I get this message on the Render() call:

Warning: unrecognized material “”, using a default OBJMaterial

Meanwhile when my json file has an invalid material (“nonexistentmaterial” here) in it I get the following when I go to use it:

Warning: unrecognized material “nonexistentmaterial”, using a default OBJMaterial


After some more searching I finally figured it out!

The trick that my script resets the session at every loop, and that seemed to be conflicting with materials library.

if I set the materials at every loop before the rendering, then it just works.
tried with 5.8.0 with success.

So, case closed!

1 Like