0
$\begingroup$

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!

$\endgroup$
0
$\begingroup$

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)
    # link nodes
links = blenderMat.node_tree.links

    #create the basic material nodes
node_output  = nodes.new(type='ShaderNodeOutputMaterial')
node_output.location = 400,0
node_pbsdf    = nodes.new(type='ShaderNodeBsdfPrincipled')
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

link = links.new(node_pbsdf.outputs['BSDF'], node_output.inputs['Surface'])

blenderMat.blend_method = 'HASHED'
blenderMat.shadow_method = 'HASHED'
blenderMat.use_screen_refraction = True

bpy.context.scene.eevee.use_ssr = True
bpy.context.scene.eevee.use_ssr_refraction = True
| improve this answer | |
$\endgroup$
  • $\begingroup$ Would this be code I could use within the create_material() function I detailed above? $\endgroup$ – Felix Sosa Aug 6 at 21:22
  • $\begingroup$ Are you asking permission to use the code, or how to wrap your function around it? $\endgroup$ – Ron Jensen Aug 6 at 21:38
  • $\begingroup$ I'm asking how to wrap my function around it. $\endgroup$ – Felix Sosa Aug 7 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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