2
$\begingroup$

So I have some models that have some transparent textures but they will appear black when imported, now I can easily fix this by using the names of the objects and then settings material to alpha blend and connecting the alpha nodes.

Thing is I wonder if blender has no knowledge whatsoever about wether an image/texture contains alpha?

I've seen something like this, where you make a new image one yourself:

bpy.data.images.new("albedo", width=1024, height=1024, alpha = True)

But I guess the imported images don't have the alpha option set to true by default, otherwise I would've expected Blender to already configure it correctly.

I also believe the only way to know this for sure would be to scan each pixel? As each image (rgb in my case) supports alpha but is not guaranteed to have alpha so I can't just "fix" all materials for transparency.

If I could just know what images were transparent I could just loop through all meshes, check textures, and if they have alpha fix the blend mode and link the nodes without relying on object names.

$\endgroup$
4
  • $\begingroup$ I currently use (image.depth == 32) to detect alpha, but I'm interested in better solutions too. $\endgroup$
    – scurest
    Commented Sep 26, 2020 at 12:10
  • $\begingroup$ Oh alright, but does this detect wether it has alpha support or if alpha is present? $\endgroup$
    – Aseliot
    Commented Sep 26, 2020 at 12:37
  • $\begingroup$ It should detect whether there is an alpha channel at all, ex. a JPG has depth==24. image.depth == (128 if image.is_float else 32) should handle 16-bit RGB{A} PNGs too, but there are still eg. grayscale+Alpha PNGs.... $\endgroup$
    – scurest
    Commented Sep 26, 2020 at 13:17
  • $\begingroup$ Alright this appears to work quite well for me, I am not sure what you mean by a better solution? $\endgroup$
    – Aseliot
    Commented Sep 29, 2020 at 9:57

1 Answer 1

2
$\begingroup$

It's been a while, but I'll promote this to an answer. This is what I'm using to detect presence of an alpha channel.

def image_has_alpha(img):
    b = 32 if img.is_float else 8
    return (
        img.depth == 2*b or   # Grayscale+Alpha
        img.depth == 4*b      # RGB+Alpha
    )

This should work for most common cases.

$\endgroup$
2
  • $\begingroup$ Btw, I noticed the FBX importer just uses my original suggestion, img.depth == 32. $\endgroup$
    – scurest
    Commented Jan 3, 2022 at 11:42
  • $\begingroup$ any way to get this with multilayer exr files? $\endgroup$ Commented May 28, 2022 at 12:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .