|
|
@ -37,6 +37,7 @@ class _LightingOperator: |
|
|
|
toggle.track(render, "use_envmaps", False) |
|
|
|
toggle.track(render, "use_envmaps", False) |
|
|
|
toggle.track(render, "use_raytrace", True) |
|
|
|
toggle.track(render, "use_raytrace", True) |
|
|
|
toggle.track(render, "bake_type", "FULL") |
|
|
|
toggle.track(render, "bake_type", "FULL") |
|
|
|
|
|
|
|
toggle.track(render, "use_bake_clear", True) |
|
|
|
|
|
|
|
|
|
|
|
def _generate_lightgroups(self, mesh, user_lg=None): |
|
|
|
def _generate_lightgroups(self, mesh, user_lg=None): |
|
|
|
"""Makes a new light group for the baking process that excludes all Plasma RT lamps""" |
|
|
|
"""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): |
|
|
|
def __init__(self): |
|
|
|
super().__init__() |
|
|
|
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): |
|
|
|
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: |
|
|
|
with GoodNeighbor() as toggle: |
|
|
|
# We need to ensure that we bake onto the "BlahObject_LIGHTMAPGEN" image |
|
|
|
# We need to ensure that we bake onto the "BlahObject_LIGHTMAPGEN" image |
|
|
|
obj = context.active_object |
|
|
|
|
|
|
|
data_images = bpy.data.images |
|
|
|
data_images = bpy.data.images |
|
|
|
im_name = "{}_LIGHTMAPGEN".format(obj.name) |
|
|
|
im_name = "{}_LIGHTMAPGEN.png".format(obj.name) |
|
|
|
size = obj.plasma_modifiers.lightmap.resolution |
|
|
|
size = modifier.resolution |
|
|
|
|
|
|
|
|
|
|
|
im = data_images.get(im_name) |
|
|
|
im = data_images.get(im_name) |
|
|
|
if im is None: |
|
|
|
if im is None: |
|
|
@ -104,36 +124,42 @@ class LightmapAutobakeOperator(_LightingOperator, bpy.types.Operator): |
|
|
|
data_images.remove(im) |
|
|
|
data_images.remove(im) |
|
|
|
im = data_images.new(im_name, width=size, height=size) |
|
|
|
im = data_images.new(im_name, width=size, height=size) |
|
|
|
|
|
|
|
|
|
|
|
# This just wraps Blender's internal lightmap UV whatchacallit... |
|
|
|
# If there is a cached LIGHTMAPGEN uvtexture, nuke it |
|
|
|
# We want to ensure that we use the UV Layer "LIGHTMAPGEN" and fetch the size from |
|
|
|
uvtex = uv_textures.get("LIGHTMAPGEN", None) |
|
|
|
# the lightmap modifier. What fun... |
|
|
|
if uvtex is not None: |
|
|
|
mesh = context.active_object.data |
|
|
|
uv_textures.remove(uvtex) |
|
|
|
|
|
|
|
|
|
|
|
# Search for LIGHTMAPGEN |
|
|
|
# Originally, we used the lightmap unpack UV operator to make our UV texture, however, |
|
|
|
for i in mesh.uv_textures: |
|
|
|
# this tended to create sharp edges. There was already a discussion about this on the |
|
|
|
if i.name == "LIGHTMAPGEN": |
|
|
|
# Guild of Writers forum, so I'm implementing a code version of dendwaler's process, |
|
|
|
uvtex = i |
|
|
|
# as detailed here: http://forum.guildofwriters.org/viewtopic.php?p=62572#p62572 |
|
|
|
break |
|
|
|
uv_base = self._get_base_uvtex(mesh, modifier) |
|
|
|
else: |
|
|
|
if uv_base is not None: |
|
|
|
toggle.track(i, "active_render", False) |
|
|
|
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: |
|
|
|
else: |
|
|
|
uvtex = mesh.uv_textures.new("LIGHTMAPGEN") |
|
|
|
# same thread, see Sirius's suggestion RE smart unwrap. this seems to yield good |
|
|
|
uvtex.active_render = False |
|
|
|
# results in my tests. it will be good enough for quick exports. |
|
|
|
toggle.track(mesh.uv_textures, "active", uvtex) |
|
|
|
uvtex = uv_textures.new("LIGHTMAPGEN") |
|
|
|
toggle.track(uvtex, "active_render", True) |
|
|
|
self._associate_image_with_uvtex(uvtex, im) |
|
|
|
|
|
|
|
|
|
|
|
# Now, enter edit mode on this mesh and unwrap. |
|
|
|
|
|
|
|
bpy.ops.object.mode_set(mode="EDIT") |
|
|
|
bpy.ops.object.mode_set(mode="EDIT") |
|
|
|
bpy.ops.mesh.select_all(action="SELECT") |
|
|
|
bpy.ops.mesh.select_all(action="SELECT") |
|
|
|
bpy.ops.uv.lightmap_pack(PREF_CONTEXT="ALL_FACES", PREF_IMG_PX_SIZE=size) |
|
|
|
bpy.ops.uv.smart_project() |
|
|
|
bpy.ops.object.mode_set(mode="OBJECT") |
|
|
|
bpy.ops.object.mode_set(mode="OBJECT") |
|
|
|
mesh.update() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Associate the image with all the new UVs |
|
|
|
# Now, set the new LIGHTMAPGEN uv layer as what we want to render to... |
|
|
|
# NOTE: no toggle here because it's the artist's problem if they are looking at our |
|
|
|
for i in uv_textures: |
|
|
|
# super swagalicious LIGHTMAPGEN uvtexture... |
|
|
|
value = i.name == "LIGHTMAPGEN" |
|
|
|
for i in mesh.uv_textures.active.data: |
|
|
|
i.active = value |
|
|
|
i.image = im |
|
|
|
i.active_render = value |
|
|
|
|
|
|
|
|
|
|
|
# Bake settings |
|
|
|
# Bake settings |
|
|
|
render = context.scene.render |
|
|
|
render = context.scene.render |
|
|
@ -168,7 +194,7 @@ class LightmapAutobakePreviewOperator(_LightingOperator, bpy.types.Operator): |
|
|
|
if tex is None: |
|
|
|
if tex is None: |
|
|
|
tex = bpy.data.textures.new("LIGHTMAPGEN_PREVIEW", "IMAGE") |
|
|
|
tex = bpy.data.textures.new("LIGHTMAPGEN_PREVIEW", "IMAGE") |
|
|
|
tex.extension = "CLIP" |
|
|
|
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"} |
|
|
|
return {"FINISHED"} |
|
|
|
|
|
|
|
|
|
|
|