How to bake ALPHA in CYCLES?

I can’t seem to find a way to bake the alpha map in cycles… there is no option… and the combined mode don’t use the alpha…

The only way i managed to get teh alpha was rendering it from camera, in ortographic view. But it don´t match the other baked textures… if there is no way of baking it, i guess i’ll have to render all of them…



What? alpha is a greyscale image. You can bake it however you like. What you probably want is the compositor. Use combine RGBA and put your baked alpha into the alpha channel of the texture. If that’s not going where you want, you’re going to have to be more specific what your desired result is.

1 Like

Sorry couldn’t help myself awakening this old thread, it’s still unfortunately relevant today. The issue is clear to me and still persists in current versions;

Baking layered alpha does NOT work in blender to this day. It will simply layer each bit of geometry and not texture of the alpha.

The result is; unwanted black areas where ever geometry happens to hang over the top.

It could possibly be raised as a bug, and this is kind of what needs to be done; For each layer of geometry with alpha data in the texture, combine these by way of ‘Divide’. In contrast, it’s kind of like they have been combined without any kind of blending method.

3 Likes

Hey folks,

Uber necroing because google landed me here,
This issue is still present in 2.7x which I use heavily.

I cooked up a solution which is not ideal *but* all things considered still faster than switching to another program and then manually combining two bakes – for my own usecase, that is.

Now the trick is you need two shaders and two image nodes for bake-to. Then, through a script, select the first image and bake the color data. Save the render. Now swap out the shaders, select the second image and bake that.

The rest is a simple question of multiplying the red channel of the second bake, against the alpha of the first.

I have, of course, already implemented this. Name your nodes accordingly and you won’t have any trouble adapting this to your own needs:

import bpy;
import numpy as np;

#get material
mat=bpy.context.object.active_material;
ntree=mat.node_tree;

#where to save to. do your relpath mambo here
path="C:\\some_fold\\image.png";

#set an output format that supports alpha
rend=bpy.context.scene.render;
rend.image_settings.file_format='PNG';
rend.image_settings.color_mode='RGBA';

#now get nodes
out=ntree.nodes["Material Output"];

bk_color=ntree.nodes["Bake0"];
bk_alpha=ntree.nodes["Bake1"];

sh_color=ntree.nodes["Shader0"];
sh_alpha=ntree.nodes["Shader0"];

#select color, connect & bake
ntree.nodes.active=bk_color;
ntree.links.new(sh_color.outputs[0], out.inputs[0]);
bpy.ops.object.bake(type='EMIT');

#save it.
bk_color.image.save_render(path);

#select alpha, connect & bake
ntree.nodes.active=bk_alpha;
ntree.links.new(sh_alpha.outputs[0], out.inputs[0]);
bpy.ops.object.bake(type='EMIT');

#i shouldn't have to do this but unrelated bug demands it
#leaving it here for documentation's sake
bk_color.image.source='FILE';
bk_color.image.filepath=path;

#now combine the two bakes
a=np.array(bk_color.image.pixels);
b=np.array((bk_alpha.image.pixels[:])[0::4]);
a[3::4]=np.multiply(a[3::4], b);
bk_color.image.pixels=a;

#save it again
bk_color.image.save_render(path);

Some footnotes:

  • As you may be able to tell, this doesn’t scale well; time required to complete two renders and then combine them will increase exponentially. For my usecase, in which all images are within the sizes of 64 to at most 512 square pixels, the time per bake falls between a tenth to a full half of a second, which may sound okay to you considering my rig is a decade old, but to me it is entirely unnaceptable. I’ll try moving it to C later on.

  • It’d be interesting to know why baking RGB+alpha in one go isn’t supported; a ten-times-as-fast OpenGL render of the viewport *does* deliver those alpha values when mixing with a transparency shader, so it’s not like the numbers are not in memory to begin with. There must be a good reason for this omission.

Hoping future search-engine travelers can make use of this.

Yes, i posted some few days ago about this big blender p̶r̶o̶b̶l̶e̶m̶ feature.

Blender is unable to handle alpha properly ( the overall result is okay but the bake problem reveals it’s badly handled ) and unfortunately there’s no shader able to do a ‘write-no-pixel’ operation and OSL seems not to have the ‘clip’ keywork GLSL has.

There’s also a deficiency in blender for heigh bake… but it’s another story.

I found no hack to solve this and indeed @Liebranca your solution is the only workaround that gives good result :slight_smile: thanks for this !
Unfortunately this don’t work on more complex objects than planar ones.
Eg a staking of textured object with alpha, like leaves in ivy:


Right is the 3D viewport render that works fine at the left is the f*cked-up bake image.

My best bet on this is to accurately set the image contour in UV editor so that minimum transparent pixels appear ( wich means you don’t need a leaf texture with alpha… )

to C ? you mean you’ll dig in the blender C code ?
Maybe you could take a peek in the input texture setup for baking and find the reason why alpha is totally lost and not blended at output ?
This would be great :smiley:

Happy blending !

It’s not about mesh complexity per se, the script is only looking at the active object’s active material. You have more than one object so that’s just not going to work.

You’d have to take all materials involved in the bake into consideration when swapping out shaders and images. And if you’re doing selected to active, then you need to swap out the shaders on the selected objects, while swapping out textures on the active one.

Your use-case is very much different, so it’d take a few adjustments. The array multiplication itself would still work, but the setup would be entirely another.

Honestly I’d much rather just pass around a plugin that does some dll calls from python than work on a straight up patch. It’s easier for a rope to go through the eye of a needle than to make small changes to Blender sources and NOT experience constant crashes.

okay :slight_smile: i can understant there are so many things crossing alltogether without collision in blender that it’s hard to add a comment to the code without bad influence ( XD of course this is exageration :stuck_out_tongue: )

The good point for the dll is that it could be writte in any language^^

But now back to the main problem: stacked alpha textures baking…
Do you think this could be achieved ( in a way or other ) in blender ?
I mean ‘bothering’ cycles when it bakes its pixel ?
Of course this can be done as it’s done in blender render and in any other
basic 3D engine but ‘interfacing’ to blender…? i guess this would be the hardest part ?

Happy blending ! :slight_smile:

Set the target image’s whole alpha channel to zero, then add to that from the baked alpha rather than multiply. That way you can just add one alpha bake after another and they won’t overwrite each other.

BUT: the values won’t be clamped, so you’d have to do that manually. More checks, more time. It is what it is.

Blender still didn’t make it possible. But is there any other program that can bake multiple meshes with alphas into one low poly?