diff --git a/korman/exporter/material.py b/korman/exporter/material.py index 00b4ded..8413ac9 100644 --- a/korman/exporter/material.py +++ b/korman/exporter/material.py @@ -139,10 +139,7 @@ class _Texture: if name.find(".") == -1: return "{}{}".format(name, newext) name, end = os.path.splitext(name) - if name.find(".") == -1: - return "{}{}".format(name, newext) - name, oldext = os.path.splitext(name) - return "{}{}{}".format(name, end, newext) + return "{}{}".format(name, newext) def _update(self, other): """Update myself with any props that might be overridable from another copy of myself""" diff --git a/korman/exporter/mesh.py b/korman/exporter/mesh.py index dff3e60..9b0ce42 100644 --- a/korman/exporter/mesh.py +++ b/korman/exporter/mesh.py @@ -298,9 +298,7 @@ class MeshConverter: lm = bo.plasma_modifiers.lightmap if lm.enabled: print(" Baking lightmap...") - print("====") bpy.ops.object.plasma_lightmap_autobake(light_group=lm.light_group) - print("====") else: for vcol_layer in bo.data.vertex_colors: name = vcol_layer.name.lower() diff --git a/korman/operators/op_lightmap.py b/korman/operators/op_lightmap.py index 95d1829..12ca02c 100644 --- a/korman/operators/op_lightmap.py +++ b/korman/operators/op_lightmap.py @@ -37,6 +37,7 @@ class _LightingOperator: toggle.track(render, "use_envmaps", False) toggle.track(render, "use_raytrace", True) toggle.track(render, "bake_type", "FULL") + toggle.track(render, "use_bake_clear", True) def _generate_lightgroups(self, mesh, user_lg=None): """Makes a new light group for the baking process that excludes all Plasma RT lamps""" @@ -87,13 +88,32 @@ class LightmapAutobakeOperator(_LightingOperator, bpy.types.Operator): def __init__(self): super().__init__() + def _associate_image_with_uvtex(self, uvtex, im): + # Associate the image with all the new UVs + # NOTE: no toggle here because it's the artist's problem if they are looking at our + # super swagalicious LIGHTMAPGEN uvtexture... + for i in uvtex.data: + i.image = im + + def _get_base_uvtex(self, mesh, modifier): + if modifier.uv_map: + return mesh.uv_textures[modifier.uv_map] + for i in mesh.uv_textures: + if i.name != "LIGHTMAPGEN": + return i + return None + def execute(self, context): + obj = context.active_object + mesh = obj.data + modifier = obj.plasma_modifiers.lightmap + uv_textures = mesh.uv_textures + with GoodNeighbor() as toggle: # We need to ensure that we bake onto the "BlahObject_LIGHTMAPGEN" image - obj = context.active_object data_images = bpy.data.images - im_name = "{}_LIGHTMAPGEN".format(obj.name) - size = obj.plasma_modifiers.lightmap.resolution + im_name = "{}_LIGHTMAPGEN.png".format(obj.name) + size = modifier.resolution im = data_images.get(im_name) if im is None: @@ -104,36 +124,42 @@ class LightmapAutobakeOperator(_LightingOperator, bpy.types.Operator): data_images.remove(im) im = data_images.new(im_name, width=size, height=size) - # This just wraps Blender's internal lightmap UV whatchacallit... - # We want to ensure that we use the UV Layer "LIGHTMAPGEN" and fetch the size from - # the lightmap modifier. What fun... - mesh = context.active_object.data - - # Search for LIGHTMAPGEN - for i in mesh.uv_textures: - if i.name == "LIGHTMAPGEN": - uvtex = i - break - else: - toggle.track(i, "active_render", False) + # If there is a cached LIGHTMAPGEN uvtexture, nuke it + uvtex = uv_textures.get("LIGHTMAPGEN", None) + if uvtex is not None: + uv_textures.remove(uvtex) + + # Originally, we used the lightmap unpack UV operator to make our UV texture, however, + # this tended to create sharp edges. There was already a discussion about this on the + # Guild of Writers forum, so I'm implementing a code version of dendwaler's process, + # as detailed here: http://forum.guildofwriters.org/viewtopic.php?p=62572#p62572 + uv_base = self._get_base_uvtex(mesh, modifier) + if uv_base is not None: + uv_textures.active = uv_base + # this will copy the UVs to the new UV texture + uvtex = uv_textures.new("LIGHTMAPGEN") + uv_textures.active = uvtex + self._associate_image_with_uvtex(uvtex, im) + # here we go... + bpy.ops.object.mode_set(mode="EDIT") + bpy.ops.mesh.select_all(action="SELECT") + bpy.ops.uv.average_islands_scale() + bpy.ops.uv.pack_islands() else: - uvtex = mesh.uv_textures.new("LIGHTMAPGEN") - uvtex.active_render = False - toggle.track(mesh.uv_textures, "active", uvtex) - toggle.track(uvtex, "active_render", True) - - # Now, enter edit mode on this mesh and unwrap. - bpy.ops.object.mode_set(mode="EDIT") - bpy.ops.mesh.select_all(action="SELECT") - bpy.ops.uv.lightmap_pack(PREF_CONTEXT="ALL_FACES", PREF_IMG_PX_SIZE=size) + # same thread, see Sirius's suggestion RE smart unwrap. this seems to yield good + # results in my tests. it will be good enough for quick exports. + uvtex = uv_textures.new("LIGHTMAPGEN") + self._associate_image_with_uvtex(uvtex, im) + bpy.ops.object.mode_set(mode="EDIT") + bpy.ops.mesh.select_all(action="SELECT") + bpy.ops.uv.smart_project() bpy.ops.object.mode_set(mode="OBJECT") - mesh.update() - # Associate the image with all the new UVs - # NOTE: no toggle here because it's the artist's problem if they are looking at our - # super swagalicious LIGHTMAPGEN uvtexture... - for i in mesh.uv_textures.active.data: - i.image = im + # Now, set the new LIGHTMAPGEN uv layer as what we want to render to... + for i in uv_textures: + value = i.name == "LIGHTMAPGEN" + i.active = value + i.active_render = value # Bake settings render = context.scene.render @@ -168,7 +194,7 @@ class LightmapAutobakePreviewOperator(_LightingOperator, bpy.types.Operator): if tex is None: tex = bpy.data.textures.new("LIGHTMAPGEN_PREVIEW", "IMAGE") tex.extension = "CLIP" - tex.image = bpy.data.images["{}_LIGHTMAPGEN".format(context.active_object.name)] + tex.image = bpy.data.images["{}_LIGHTMAPGEN.png".format(context.active_object.name)] return {"FINISHED"} diff --git a/korman/properties/modifiers/render.py b/korman/properties/modifiers/render.py index daf4975..0da8adc 100644 --- a/korman/properties/modifiers/render.py +++ b/korman/properties/modifiers/render.py @@ -39,13 +39,16 @@ class PlasmaLightMapGen(PlasmaModifierProperties): light_group = StringProperty(name="Light Group", description="Group that defines the collection of lights to bake") + uv_map = StringProperty(name="UV Texture", + description="UV Texture used as the basis for the lightmap") + def created(self, obj): self.display_name = "{}_LIGHTMAPGEN".format(obj.name) def export(self, exporter, bo, so): mat_mgr = exporter.mesh.material materials = mat_mgr.get_materials(bo) - lightmap_im = bpy.data.images.get("{}_LIGHTMAPGEN".format(bo.name)) + lightmap_im = bpy.data.images.get("{}_LIGHTMAPGEN.png".format(bo.name)) # Find the stupid UVTex uvw_src = 0 diff --git a/korman/ui/modifiers/render.py b/korman/ui/modifiers/render.py index de88b96..79d53f0 100644 --- a/korman/ui/modifiers/render.py +++ b/korman/ui/modifiers/render.py @@ -18,6 +18,8 @@ import bpy def lightmap(modifier, layout, context): layout.row(align=True).prop(modifier, "quality", expand=True) layout.prop_search(modifier, "light_group", bpy.data, "groups", icon="GROUP") + layout.prop_search(modifier, "uv_map", context.active_object.data, "uv_textures") + operator = layout.operator("object.plasma_lightmap_preview", "Preview Lightmap", icon="RENDER_STILL") operator.light_group = modifier.light_group @@ -27,6 +29,6 @@ def lightmap(modifier, layout, context): # well... It was nice knowing you! tex = bpy.data.textures.get("LIGHTMAPGEN_PREVIEW") if tex is not None: - im_name = "{}_LIGHTMAPGEN".format(context.active_object.name) + im_name = "{}_LIGHTMAPGEN.png".format(context.active_object.name) if tex.image.name == im_name: layout.template_preview(tex, show_buttons=False)