My users would like to be able to adjust the transparency of their mesh objects with a single slider in the panel of an addon, where the transparency ranges from invisible to opaque. The desired functionality is exactly like the functionality of the alpha channel of the diffuse color in Solid display mode:
mat = bpy.context.object.active_material row.prop(mat, "diffuse_color", text="color+alpha")
However, we also have image textures in our scenes, which are only visible in the Material Preview or Rendered display modes. Is there a way to reproduce this one single slider functionality for a transparency that is visible in the Material Preview/Rendered display modes?
I have been using a Principled BSDF node, but there are many different settings for the transparency, and none of them range from invisible to opaque. If Blend Mode = "Alpha Blend", then the object is never opaque, but if Blend Mode = "Opaque" then there is no transparency. The alpha channel of the Base Color is more helpful than the Alpha input of the BSDF node, but neither of them range from invisible to opaque. And there are several other color settings, but none of their alphas help to achieve what I need. I'm hoping that I've been searching down the wrong path, and there is actually a simpler way to deal with transparencies that is visible in the Material Preview/Rendered display modes.
Thanks for your help!
Following Gorgious' solution, I am now trying to link the full diffuse color as well, using the same method. However, the resulting color isn't correct, I think the problem might be with deleting the variable and creating a new one for all three color channels, but I'm having trouble determining the data structure:
def attach_color_driver(mat, rgb_ind): color_input = mat.node_tree.nodes["Principled BSDF"].inputs["Base Color"] driver = color_input.driver_add("default_value")[rgb_ind].driver [driver.variables.remove(value) for value in reversed(driver.variables.values())] var = driver.variables.new() target = var.targets target.id_type = 'MATERIAL' target.id = mat target.data_path = "diffuse_color[" + str(rgb_ind) + "]" driver.expression = "var" attach_color_driver(mat, 0) attach_color_driver(mat, 1) attach_color_driver(mat, 2)
This displays green as green, blue as cyan, and red as green.