My Lightmap Setup & Images Saver scripts for Blender

I use Blender too now, but it’s still 2 weeks though.
At the first start, I was wondering how hard it could be to get a bunch of objects lightmapped by batch process as I used to do in Maya with MEL.
So I tried it and found it’s pretty simple and straightforward.

Download :
Lightmapped Scene
Lightmap Scripts

There are 3 scripts in the zip :

  1. Lightmap Setup
  2. Lightmap Image Saver
  3. UV Layer Switcher
    NOTE : I put those scripts to Help menu, just to minimize the find & execute time.

Brief overview :
My Lightmap Setup script also works on object with multiple materials. It’s demonstrated by the cube and the cylinders.
Instanced (linked duplicates) objects are not processed at all, only unique meshes are processed.
Only 1 lightmap texture is assigned to each object. The texture can be independent per object or shared by some objects.
For more info, read each script’s docstring.

Detailed features of the Lightmap Setup script :

  1. creates new UV layer for lightmap, if it’s not already exist
  2. sets the lightmap UV to be active, ready for baking
  3. creates a material if the object doesn’t have one already
  4. duplicates material which is used by multiple objects, so each object can use its own
    lightmap texture, if it’s using an originally shared material
  5. removes existing lightmap textures from texture slots
  6. creates a new texture (+ an image) to bake to, and insert it to the material
    after the last found texture in the slots
  7. sets the lightmap texture’s mapping to use the lightmap UV instead of the original (ORCO)
  8. links the lightmap texture to the lightmap UV layer, so Chicken will write out
    that UV layer’s name instead of the first layer
  9. turns on “tangent vector usage in shading calculation” (at Shaders panel) to get full shading
    for shadow baking, otherwise some areas of faces which is facing away from the light
    wouldn’t be filled with shadow color.
  10. turns on “auto smooth during rendering” (at Mesh panel) and sets the max angle to minimum
    to fully prevent artifacts, if user wants to bake shadow
  11. turns off “auto smooth during rendering” (at Mesh panel) and restores the max angle,
    if user wants to bake AO
  12. runs Blender’s smart-projections UV unwrap script

Lightmapping steps using my scripts :

[0] select objects (it will be filtered to process Mesh only objects)
[1] run Lightmap Setup script
[2] fine tune the generated UV, in case any overlapping UV faces
[3] bake

Once the desired objects are processed, save the result :
[4] run Lightmap Image Saver script
[5] save your .blend file

You should run step 1-3 for small, medium, large, and extra large objects separately,
just to use optimum texture size for different sized objects.

I don’t use “Lightmap UVPack” UV unwrap module because :

  1. it can’t process selected objects, it only processes the active object only,
    or all objects, including camera, lights, and everything else in the scene
  2. it’s harder to get good proportion quality
  3. it’s impossible to post-process the image (eg. blur) since the faces are
    scattered all over
    I use the “Smart Projections” instead.

The Lightmap Setup script turns on 2 flags :

  1. tangent vector usage for shading (at Shaders panel)
    This flag is NOT good for normal rendering in Blender, but it’s the only way to get
    correct shadow baking calculation. It doesn’t affect AO baking.
  2. auto smooth during rendering (at Mesh panel)
    This is only good to fully remove artifacts in shadow baking,
    but BAD for AO baking and normal rendering.

Screenshots :

The results in Blender :

The end results in Panda3D :

Some experiment results :
torus :

suzie :

(with Lightmap UVPack)

Note that those are my first Blender scripts, so I might miss something obvious, like this one :
:question: I couldn’t get smooth shading when baking shadow, no matter what I did.

Any input, criticism, offensive feature objection, and better implementation are welcome. :smiley:

Great work! :slight_smile:

Thanks for sharing :smiley:

I’ll take a deep look at it tommorow so I can rate it somehow.

–Your Mešča

I ran into the smooth problem as you describe.

I discover two points, wondering if it is related to the problem ?

First, if more than one objects is selected to run the setup script, the UV mapping of the objects seems shared with the same image no mater “share image” option is used or not. Looks like something is wrong in uvcalc_smart_project ?

If I process mesh one by one, I can get a smooth baking if “auto smooth” option is off.

The smooth problem is only when baking shadow, and not AO.
Are you sure you get smooth shading when baking shadow ?
Any screenshot ?

In uvcalc_smart_project setup, there is “Share Tex Space” option, so if you want the selected objects share the same image, then you should let them share texture space too, otherwise don’t. I’ve mentioned it via the “Share Image” button’s tooltip. Did you get a chance to read it ?

oic, with "“Share Tex Space” option off, I can separate them now.

About the smooth problem, here is the screenshoot: … directlink … directlink

btw, why set the max angle to 1 when auto smooth option is selected ? Shouldn’t it be something big, or I misunderstand the purpose ?

As you can see in my shots, if the autosmooth is OFF, there are alot of artifacts. It’s gone if I use 1 degree, but remains if I use the default 30 or even 80.
If you download the first zip, could you try the scene in /noLightmap ?
Just to be sure we start with the same thing.

As long as the mesh is set smooth before baking, it is ok. May be I am not getting the real question. Anyway, thank you for the script, it is working very well.

Suzie is already smooth before baking. Increasing soft size and samples to maximum values doesn’t make any difference. It’s still flat.

BTW, I use 2.4.8 internal renderer.

I get an error when I try to run the setup. It says my materials don’t have a ‘texture’ attribute.

What material is it, and how did you create it ?
Could you post the full traceback ?