Please provide example code for baking textures

Hi,

Mine is a basic 3D CAD drawing application written in C++
I am trying to integrate cycles in my application. I could build cycles scene and session in memory. Now I need to bake textures of each object and export the scene. I could not find how to bake textures. Spent more than a year but no luck. Tried exploring how blender is doing but lost in its RNA and python stuff.

Can you please provide a code sample?
Thanks in advance.

Hi Subrahmanya,

The entrypoint for baking is here:
https://developer.blender.org/diffusion/B/browse/master/intern/cycles/render/bake.h

You could look at the standalone app here:
https://developer.blender.org/diffusion/B/browse/master/intern/cycles/app/cycles_standalone.cpp

Then search for the bake references in the blender addon here eg:
https://developer.blender.org/diffusion/B/browse/master/intern/cycles/blender/blender_session.cpp

And meet somewhere in the middle. I’m going to be looking into this pretty soon as well but it looks fairly straightforward.

Cheers

The difficulty is that much of the baking code is implemented in Blender. For Cycles you can evaluate shaders at specified coordinates on triangles, while the logic for rasterization and baking high-to-low poly is implemented inside Blender.

Thank you @boberfly and @brecht
I think, I am progressing well with your support.

My meshes have both UV Channels. (Texture UV and Unwrapped UV).
How to tell Cycles which channel to take?
It looks like we need to calculate UV at each pixel it seems. Basically I need to populate bake_data after the below step. Please help.

BakeData *bake_data = scene->bake_manager->init(object_index, tri_offset, num_pixels);

I did some looking into the bake code a while back, still haven’t found the time to finish the project I started, but I’ve been writing down my notes and they may help you – Help: Information on Cycles Bake code

1 Like

Hi,

Getting exception in this method from kernel_compat_cpu.h because the index is 8388607 but width is 4.

I verified with the bake data that I am populating with the bake data that blender populates. They are exactly same. So, now could not understand what I missed.

Any clue?

ccl_always_inline const T &fetch(int index)
{
kernel_assert(index >= 0 && index < width);
return data[index];
}