From b2859a3a41ef9b69d587473644a58e928511b2c1 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Thu, 29 Jul 2021 18:09:27 -0400 Subject: [PATCH] Don't track lightmaps by name. That's bugprone and can lead to pain. --- korman/exporter/convert.py | 7 +++---- korman/exporter/etlight.py | 7 ++++++- korman/operators/op_lightmap.py | 4 ++-- korman/properties/modifiers/render.py | 7 +------ 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/korman/exporter/convert.py b/korman/exporter/convert.py index d338a42..d2f3a9a 100644 --- a/korman/exporter/convert.py +++ b/korman/exporter/convert.py @@ -56,6 +56,7 @@ class Exporter: self.image = image.ImageCache(self) self.locman = locman.LocalizationConverter(self) self.decal = decal.DecalConverter(self) + self.oven = etlight.LightBaker(self.report) # Step 0.8: Init the progress mgr self.mesh.add_progress_presteps(self.report) @@ -126,10 +127,8 @@ class Exporter: self.report.raise_errors() def _bake_static_lighting(self): - lighting_method = self._op.lighting_method - if lighting_method != "skip": - oven = etlight.LightBaker(self.report) - oven.bake_static_lighting(self._objects) + if self._op.lighting_method != "skip": + self.oven.bake_static_lighting(self._objects) def _collect_objects(self): scene = bpy.context.scene diff --git a/korman/exporter/etlight.py b/korman/exporter/etlight.py index 9e78ff0..00ad6a0 100644 --- a/korman/exporter/etlight.py +++ b/korman/exporter/etlight.py @@ -43,6 +43,7 @@ class LightBaker(_MeshManager): self.lightmap_uvtex_name = "LIGHTMAPGEN" self.retain_lightmap_uvtex = True self.force = False + self._lightmap_images = {} self._uvtexs = {} def __del__(self): @@ -213,6 +214,9 @@ class LightBaker(_MeshManager): material.light_group = dest return shouldibake + def get_lightmap(self, bo): + return self._lightmap_images.get(bo.name) + def get_lightmap_name(self, bo): return self.lightmap_name.format(bo.name) @@ -282,7 +286,7 @@ class LightBaker(_MeshManager): def _pack_lightmaps(self, bake): lightmap_iter = itertools.chain.from_iterable((value for key, value in bake.items() if key[0] == "lightmap")) for bo in lightmap_iter: - im = bpy.data.images.get(self.get_lightmap_name(bo)) + im = self.get_lightmap(bo) if im is not None and im.is_dirty: im.pack(as_png=True) @@ -337,6 +341,7 @@ class LightBaker(_MeshManager): # Force delete and recreate the image because the size is out of date data_images.remove(im) im = data_images.new(im_name, width=size, height=size) + self._lightmap_images[bo.name] = im # If there is a cached LIGHTMAPGEN uvtexture, nuke it uvtex = uv_textures.get(self.lightmap_uvtex_name, None) diff --git a/korman/operators/op_lightmap.py b/korman/operators/op_lightmap.py index d68999e..f95f7ad 100644 --- a/korman/operators/op_lightmap.py +++ b/korman/operators/op_lightmap.py @@ -81,7 +81,7 @@ class LightmapAutobakePreviewOperator(_LightingOperator, bpy.types.Operator): if tex is None: tex = bpy.data.textures.new("LIGHTMAPGEN_PREVIEW", "IMAGE") tex.extension = "CLIP" - image = bpy.data.images[bake.get_lightmap_name(context.object)] + image = bake.get_lightmap(context.object) tex.image = image if self.final: lightmap_mod.image = image @@ -126,7 +126,7 @@ class LightmapBakeMultiOperator(_LightingOperator, bpy.types.Operator): for i in filtered_objects: lightmap_mod = i.plasma_modifiers.lightmap if lightmap_mod.bake_lightmap: - lightmap_mod.image = bpy.data.images[bake.get_lightmap_name(i)] + lightmap_mod.image = bake.get_lightmap(i) return {"FINISHED"} diff --git a/korman/properties/modifiers/render.py b/korman/properties/modifiers/render.py index c56daa5..07d0621 100644 --- a/korman/properties/modifiers/render.py +++ b/korman/properties/modifiers/render.py @@ -463,14 +463,9 @@ class PlasmaLightMapGen(idprops.IDPropMixin, PlasmaModifierProperties, PlasmaMod if not self.bake_lightmap: return - if self.image is not None: - lightmap_im = self.image - else: - # Gulp... - lightmap_im = bpy.data.images.get("{}_LIGHTMAPGEN.png".format(bo.name)) - # If no lightmap image is found, then either lightmap generation failed (error raised by oven) # or baking is turned off. Either way, bail out. + lightmap_im = self.image if self.image is not None else exporter.oven.get_lightmap(bo) if lightmap_im is None: return mat_mgr = exporter.mesh.material