Blender: Changing the alpha transparency of an object through Python?
Asked Answered
C

1

6

I'm looking to change the transparency of a plane in real time using the games engine. I found this script:

for x in [0,1,2,3]:
    own.meshes[0].getVertex(0,x).setRGBA([1,1,1,alpha_rate])

This works on materials with no image attached. However I have now added a UV texture to it and attached an image. The code above doesn't work now. Is there any way to change the alpha of the object?

I'm using Blender 2.49b with Python 2.5.1

Crape answered 27/3, 2011 at 12:45 Comment(1)
Did you try to change the alpha of the texture too, or add a texture that is already transparent ?Pietra
S
1

Changing alpha transparency programmatically

This code works fine not only for colored surfaces but also for textured surfaces as well (both image textures and procedural textures). Funny, it took 13.5 years to answer this question :)) I tested this code in Blender 4.2.2 on macOS Sequoia 15.0. Copy this code and paste it in the Blender's Python Console.

import bpy

materials = bpy.data.materials

for material in materials:
    if material.node_tree is None:
        continue
    nodes = material.node_tree.nodes
    for node in nodes:
        if node.bl_idname == "ShaderNodeBsdfPrincipled":
            node.inputs["Alpha"].default_value = 0.25

Press Enter key twice to execute this script in the console. Here's my cube with alpha = 0.25.

Stephanstephana answered 2/10 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.