I noticed something that looks like a "Big Bad Problem" with how Korman handles light baking. Maybe one of the guys who know Korman best might be able to confirm this...
I'm currently working on my Canyon Age in Blender with Korman. As you might remember, I also use my own plugin for baking light in a scene, which I made shortly before Korman was first released. Up until recently, I was still using this script alongside with Korman, because it felt a bit more convenient for various reasons (better handling of objects with modifiers being one).
Then a few days ago I decided I'd stop using my script and let Korman do most of the baking stuff on its own (mostly due to my script not being compatible with how Korman stores textures for consecutive exports, which means longer export times).
Now, my Age has a lot of objects in it (some of them pretty big), which must all be baked, using mainly vertex shading and a few lightmaps. It's a perfect test file to test how fast a script can process a big scene.
However, my Age never exports when I tell Korman to bake lighting - most of the time, Blender is stuck at exporting, which means I cancel the export after 15 minutes.
On the other hand, my lightbaking script can rebake the whole vertex shading in under 60 seconds. Then Korman can export it in less than 30 seconds. Huh, quite a difference.
Actually, I found out why tonight by tweaking my own script. The problem lies in how Korman processes light baking. When, to be more precise.
What my script does:
- select all objects which must be baked
- call bpy.ops.object.bake_image()
What Korman does (from what I've seen):
- pick an object in the scene
- call bpy.ops.object.bake_image()
- export the object
- pick an object in the scene
- call bpy.ops.object.bake_image()
- export the object
- ...
Exporting the object being processed takes little time. The problem is we call bake_image several times.
Now, why is it bad ? Let's take a look at what bake_image() does internally:
- 1. Prepare scene data (single threaded): this step puts the whole rendered geometry in memory, which sometime involve recomputing modifiers, etc.
- 2. Build Raytree (single threaded): computes the path of shadows for the whole scene.
- 3. Actual rendering (multi-threaded): for each selected object, computes color for each vertex (or when dealing with lightmaps, "compute each pixel in each texture applied to each object").
- 4. Trigger scene update (I'm not sure about that one, but most operators do)
As you might be guessing now, the difference is that my script triggers step 1, 2 and 4 only once - Korman triggers it once per object in my scene. Step 1 and 2 alone take roughly 10 seconds. Now, imagine those 10 seconds multiplied by the number of objects in the scene (more than 700). Uh oh, this means theoretically it would take roughly 2 hours to bake.
BTW, this is actually simple to verify: have Korman export a big scene. Because step 1 and 2 are single threaded, your CPU usage will rarely rise above 100/(num cores) percent. Now, if you instead select all objects in your scene and hit "Bake", you'll notice your CPU usage being at 100% most of the time, indicating most of the computing is done in step 3.
Anyhow, on to the big question.
Korman is designed to process meshes and light groups in a specific order, because translating Blender data to Plasma is a complex process.
Would it be feasible to call bake_image only once or twice per page ?