# How do I make objects in my bpy script have transparent materials?

I'm working with an old python script that renders three spheres in Blender using the bpy module.

Each of these spheres has a unique material (they're simply red, blue, and green spheres, respectively). I want the red sphere to be slightly transparent. Below, I have my function for creating this transparent material:

def create_material(r, g, b, alpha=None):
'''
Creates a new material with optionally specified alpha vals.
So far, I only use this on the red sphere. The green and blue spheres have mats
assigned from bpy.materials.get() which I don' know how to use anymore nor where those
materials are "coming from".
'''
new_material = data.materials.new(name="MyNewMaterial")

if alpha:
new_material.diffuse_color = (r, g, b) # setting the color
new_material.alpha = alpha # Setting what I believe to be the value for transparency

# Here, I simply copy/pasted what I found related to my problem on StackExchange
#  but it does not work.
new_material.transparency_method = 'Z_TRANSPARENCY'
new_material.use_transparency = True
bpy.context.object.show_transparent = True

# Return this new material
return new_material

new_material.diffuse_color = (r, g, b)
return new_material


Even with the above function, I can't seem to create a transparent sphere in the scene. It always shows up as though the alpha value doesn't matter. To me, that implies either the alpha value I'm setting isn't being used or the material is being overwritten somewhere but I don't know where and am having difficulty debugging since I've forgotten most of Blender (this script is about two years old).

How can I make an object transparent?

Thanks!

This sets up a Principled BSDF using the transmission input for transparency. It also throws all the renderer switches for Eevee to show the transparency.

import bpy

name="MyMaterial"
overwrite=True

if(overwrite is True) and ( name in bpy.data.materials ):
blenderMat = bpy.data.materials[name]

else:
blenderMat = bpy.data.materials.new(name)
name = blenderMat.name

# get the nodes
blenderMat.use_nodes=True
nodes = blenderMat.node_tree.nodes
# clear all nodes to start clean
for node in nodes:
nodes.remove(node)

#create the basic material nodes
node_output.location = 400,0
node_pbsdf.location = 0,0
node_pbsdf.inputs['Base Color'].default_value = (0.8, 0.05, 0.05, 1.0)
node_pbsdf.inputs['Alpha'].default_value = 1 # 1 is opaque, 0 is invisible
node_pbsdf.inputs['Roughness'].default_value = 0.2
node_pbsdf.inputs['Specular'].default_value = 0.5
node_pbsdf.inputs['Transmission'].default_value = 0.5 # 1 is fully transparent


• Would this be code I could use within the create_material() function I detailed above? – Felix Sosa Aug 6 at 21:22