3
$\begingroup$

I'm trying to use the blender python API to change the base color of multiple materials from an object. I'm pretty new to the bpy and the documentation is likely poor.

This is my selected object, and the colors i'm trying to change:

enter image description here

And the code am trying to use but with no effect:

bpy.data.materials["chasis"].diffuse_color = (0, 0, 0)

This is the error message i got:

>>> bpy.data.materials["chasis"].diffuse_color = (0, 0, 0)
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
ValueError: bpy_struct: item.attr = val: sequences of dimension 0 should contain 4 items, not 3

Any help would be much appreciated!

$\endgroup$
0

2 Answers 2

6
$\begingroup$

The diffuse_color is used for viewport display in solid mode. It's this:

diffuse_color under Viewport Display

To change the Base Color, you need to access the right node in the node tree. The simplest case is

mat = bpy.data.materials["chasis"]
principled = mat.node_tree.nodes["Principled BSDF"]
principled.inputs["Base Color"].default_value = (r, g, b, a)

It would be more complicated if you have a more complicated node setup.

And as already said, the color is a 4-tuple, so you need to set an alpha value (even though the alpha is never used).


I recommend you turn on Python Tooltips in the Preferences. You can hover over things and find out how to access them in Python.

Base Color tooltip

$\endgroup$
0
5
$\begingroup$

"should contain 4 items, not 3"

bpy.data.materials["chasis"].diffuse_color = (0, 0, 0, 1)

4th item would be Alpha in this case. Above line would be Black with opacity of 1.

This, however, would be setting the Viewport Colour. To set the node base colour. Use this :

bpy.data.materials["chasis"].node_tree.nodes["Principled BSDF"].inputs[0].default_value = (0, 0, 0, 1)

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.