Skip to content

Comments

Operator to automatically bake lightmaps for selected objects.#281

Open
GottfriedHofmann wants to merge 45 commits intoHubs-Foundation:masterfrom
BlenderDiplom:LightMapBakeOperator
Open

Operator to automatically bake lightmaps for selected objects.#281
GottfriedHofmann wants to merge 45 commits intoHubs-Foundation:masterfrom
BlenderDiplom:LightMapBakeOperator

Conversation

@GottfriedHofmann
Copy link
Contributor

Added an operator that bakes lightmaps using Cycles for all selected objects that have at least one material. It uses UV slot 1 for the lightmap and creates the slot if not present already. It also automatically creates the required MOZ_Lightmap nodes and wires them correctly. If a MOZ_Lightmap node is present, the addon assumes that the user has taken care of the correct setup and bakes directly.
Baking happens using the Cycles render engine. The created lightmaps are saved as .hdr to Blender's temporary directory, packed and then removed to keep the user's system clean.
The new operator can be found in the Object Properties Editor in a new Panel "Hubs Lightmap Baker"
image

Copy link
Member

@Exairnous Exairnous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR and apologies for the delay in reviewing it. I think it will provide some good functionality, but I think it's scope should be modified slightly. I think your PR is great for baking light maps for objects that don't have them yet, but your PR doesn't address all the intricacies that can arise when attempting to rebake already set up light maps. PR #48 does this very well and has a lot of error checking already in place, so I'm thinking it makes the more sense to have your PR to bake an initial light map for objects and let the other PR handle any rebaking.

With that in mind, I'm thinking that a check should be added to this PR to avoid baking light maps when they are already present on an object.

PR #48 also adds a panel in the render settings, so I'm thinking that maybe it would be better to add the operator there once that's in, rather than to a new panel in the object properties.

I believe calling bake via execute instead of invoke causes the UI to block with no feedback to the user. I think it would be better to use the non-blocking version and reset all the settings after the bake has completed via a handler (like what is done with reflection probes). The overridden settings can be stored and restored as with reflection probes in reflection_probe.py lines 362-379 and lines 334-337

bpy.app.handlers.object_bake_complete is probably the needed handler.

GottfriedHofmann and others added 6 commits September 3, 2024 17:53
At some point in the Blender 4 series linear was separated out into a bunch of different versions, Linear Rec.709 is what we have used when importing HDRs and seems to be the new default. This code should make it work for all Blender versions

Co-authored-by: Exairnous <mythologylover75@gmail.com>
@GottfriedHofmann
Copy link
Contributor Author

I have created this addon with PR #48 in mind.
My addon currently uses the following logic:
Is a material present but no lightmap? - build the lightmap setup
Is a material with lightmap present? - assume the user has set up the light map correctly and re-bake.

…g, uses the space better for multiple objects and materials but is prone to overlaps in some corner cases.
@GottfriedHofmann
Copy link
Contributor Author

Here is the Hubs Blender addon with the autobaker included:

io_hubs_addon_1.7.2.524.zip

Copy link
Member

@Exairnous Exairnous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the changes! Overall, this looks generally good, but there are some things I've left inline comments on.

Comment on lines +903 to +906
if bpy.app.version < (4, 0, 0):
lightmap_texture_node.image.colorspace_settings.name = "Linear"
else:
lightmap_texture_node.image.colorspace_settings.name = "Linear Rec.709"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any errors reported, but this also doesn't seem to fully work as the colorspace is displayed as sRGB when I go to check the image after baking for both HDR and JPEG. If I watch closely it looks like it is initially set to Linear, but then it gets changed to sRGB when the bake finishes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, there is actually a whole can of worms behind this one. The problem seems to have been introduced with the switch to non-blocking with a modal operator. Here are the issues I have encountered when investigating this:

  • The packed images seem not to get saved, you have to prompt the save yourself. Blender will ask you about that when closing or loading another file but many users will probably just click it away, losing their bake.
  • The images get saved to disk, but are completely black with all pixel values at 0.0. The image datablock loaded in Blender still has the color information, though.
  • As mentioned the color space gets set to SRGB at the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants