That's a common misunderstanding of the material system. Material.diffuse_color
property is used for solid viewport shading (or renders created with the workbench engine) and does not affect the (final) rendering of all other engines, the main reason why you can find that property in the Viewport Display panel:
Due to the nature of node based materials (and almost endless possibilities of wiring things up), it's not that easy to get the diffuse/albedo color of a material as you might think.
If you're using e.g. just one Principled BSDF, you would have to get the material of the object, find the principled node in the node tree and read its 'Base Color'
input property:
import bpy
# Get the object reference
obj = bpy.context.object
# Get the material via active slot
mat = obj.active_material
# or: bpy.data.materials.get("MaterialName")
if hasattr(mat, 'node_tree'):
# Get the principled bsdf
prince = mat.node_tree.nodes.get('Principled BSDF')
if prince:
# Get the base color of the principled bsdf
r, g, b, a = prince.inputs['Base Color'].default_value
print(r, g, b, a)
Result
0.01615985296666622 0.04138465225696564 0.8000000715255737 1.0
Further reading
HSV, sRGB and Hex triplet
In case you'd like to get the HSV
color and the Hex
triplet as well, you'd have to implement the actual conversion using python since there is no API function to get them:
import bpy
import colorsys
import math
def lin_to_srgb(c):
if c < 0.0031308:
srgb = 0.0 if c < 0.0 else c * 12.92
else:
srgb = 1.055 * math.pow(c, 1.0 / 2.4) - 0.055
return max(min(int(srgb * 255 + 0.5), 255), 0)
def hextriplet(c):
return '#' + ''.join(f'{i:02X}' for i in c)
# Get the object reference
obj = bpy.context.object
# Get the material via active slot
mat = obj.active_material
# or: bpy.data.materials.get("MaterialName")
if hasattr(mat, 'node_tree'):
# Get the principled bsdf
prince = mat.node_tree.nodes.get('Principled BSDF')
if prince:
# Get the base color of the principled bsdf
r, g, b, a = prince.inputs['Base Color'].default_value
# Convert rgb to hsv
h, s, v = colorsys.rgb_to_hsv(r, g, b)
# Convert to sRGB
srgb = [lin_to_srgb(c) for c in (r, g, b)]
# Print the values
print("RGBA:", r, g, b, a)
print("HSV:", h, s, v)
print("sRGB:", ", ".join(map(str, srgb)))
print("Hex:", hextriplet(srgb))
Result
RGBA: 0.04373500123620033 0.09530799835920334 0.7991030216217041 1.0
HSV: 0.655287445825263 0.9452698837911484 0.7991030216217041
sRGB: 59, 87, 231
Hex: #3B57E7