Blender materials bulk handling - fix specularity via Python script


#1

Trying to update thousands of materials across hundreds of objects I am looking for a way to handle the specularity settings in a more dynamic way. I am sure lots of others want the same thing.

Until I build spec and hardness maps I find setting spec color to black, intensity to 0 and hardness to 0.
This is a safe setting for now.

Enter Python.
I discovered that Blender will allow a simple python script to be run which can change every single material in an object to whatever you want, it can also change settings for all materials in one pass.

This doesnt seem to be as much scary magic as I thought.
According to the docs I have read, this should work…

import bpy
for mat in bpy.data.materials:
mat.use_specular_intesity = 0

and then maybe this

import bpy
for mat in bpy.data.materials:
mat.use_specular_hardness = 0

But it throws an error saying something about indentation.

I am hoping a Blender/Python guru out there might be able to help get this little script working for us.


#2

I haven’t really used Python but I think you need to indent the third line (e.g., add 4 spaces to the front) so that the for statement can tell which lines to iterate on.


#3

ahh indent the 3rd line, I was indenting the second line, I will try that, thanks Dave.


#4

Basic architecture of python is that code blocks are indented, literally. Be very careful to be consistent about that indentation because python cannot figure out that one tab is 4 spaces. There is no standard about that. So, choose: either stick with spaces only or stick with tabs, don’t mix else a misery of code debugging will ensue.
[edit] Forgot you wanted to reset intensity too:

So:

import bpy
for mat in bpy.data.materials:
    mat.use_specular_hardness = 0
    mat.use_specular_intensity = 0

#5

Just as a reminder,
the script it self makes every texture matte. It also doesnt fix it if these values already have a texture defined for the,

If doing it with Texturemaps, some more drastic measures are needed.
basically:

map.specular_color-> map.specular_hardness
map.specular_intensity -> 0 if not metallic, 1 if metallic. else create map manually (this is something not defined before)

Ill try to whip up a simple python script to do so later in the week


#6

yep indent code equally in each scope doesn’t have to be 4 spaces just has to be consistent. so basically the code after the colons has to be indented until you reach the end of that scope. In these cases that is just one line.


#7

Indeed. Like I said, it needs to be consistent. The 4 spaces is just one example of indentation - it could have been one space. The main point is not to intermix spaces and tabs.