Browse Source

Ensure all the things bake...

Yes, this fixes more "no valid image" bugs :/
pull/8/head
Adam Johnson 10 years ago
parent
commit
4604029050
  1. 13
      korman/helpers.py
  2. 18
      korman/operators/op_lightmap.py

13
korman/helpers.py

@ -32,20 +32,19 @@ class GoodNeighbor:
setattr(cls, attr, value) setattr(cls, attr, value)
bpy.context.scene.update() bpy.context.scene.update()
def ensure_object_can_bake(bo, toggle):
def enter_edit_mode(bo, toggle): """Ensures that we can use Blender's baking operators on this object. Side effect: also ensures
"""Ensures an object is placed in edit mode""" that the object will enter edit mode when requested."""
scene = bpy.context.scene scene = bpy.context.scene
toggle.track(scene, "layers", bo.layers) toggle.track(scene, "layers", bo.layers)
toggle.track(bo, "hide", False)
toggle.track(bo, "hide_render", False)
toggle.track(bo, "hide_select", False)
scene.update() scene.update()
bpy.ops.object.mode_set(mode="EDIT")
def ensure_power_of_two(value): def ensure_power_of_two(value):
return pow(2, math.floor(math.log(value, 2))) return pow(2, math.floor(math.log(value, 2)))
def exit_edit_mode():
bpy.ops.object.mode_set(mode="OBJECT")
def make_active_selection(bo): def make_active_selection(bo):
"""Selects a single Blender Object and makes it active""" """Selects a single Blender Object and makes it active"""
for i in bpy.data.objects: for i in bpy.data.objects:

18
korman/operators/op_lightmap.py

@ -129,6 +129,10 @@ class LightmapAutobakeOperator(_LightingOperator, bpy.types.Operator):
if uvtex is not None: if uvtex is not None:
uv_textures.remove(uvtex) uv_textures.remove(uvtex)
# Make sure the object can be baked to. NOTE this also makes sure we can enter edit mode
# TROLLING LOL LOL LOL
ensure_object_can_bake(obj, toggle)
# Originally, we used the lightmap unpack UV operator to make our UV texture, however, # 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 # 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, # Guild of Writers forum, so I'm implementing a code version of dendwaler's process,
@ -141,7 +145,7 @@ class LightmapAutobakeOperator(_LightingOperator, bpy.types.Operator):
uv_textures.active = uvtex uv_textures.active = uvtex
self._associate_image_with_uvtex(uvtex, im) self._associate_image_with_uvtex(uvtex, im)
# here we go... # here we go...
enter_edit_mode(obj, toggle) bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="SELECT") bpy.ops.mesh.select_all(action="SELECT")
bpy.ops.uv.average_islands_scale() bpy.ops.uv.average_islands_scale()
bpy.ops.uv.pack_islands() bpy.ops.uv.pack_islands()
@ -150,10 +154,10 @@ class LightmapAutobakeOperator(_LightingOperator, bpy.types.Operator):
# results in my tests. it will be good enough for quick exports. # results in my tests. it will be good enough for quick exports.
uvtex = uv_textures.new("LIGHTMAPGEN") uvtex = uv_textures.new("LIGHTMAPGEN")
self._associate_image_with_uvtex(uvtex, im) self._associate_image_with_uvtex(uvtex, im)
enter_edit_mode(obj, toggle) bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="SELECT") bpy.ops.mesh.select_all(action="SELECT")
bpy.ops.uv.smart_project() bpy.ops.uv.smart_project()
exit_edit_mode() bpy.ops.object.mode_set(mode="OBJECT")
# Now, set the new LIGHTMAPGEN uv layer as what we want to render to... # Now, set the new LIGHTMAPGEN uv layer as what we want to render to...
for i in uv_textures: for i in uv_textures:
@ -209,7 +213,8 @@ class VertexColorLightingOperator(_LightingOperator, bpy.types.Operator):
def execute(self, context): def execute(self, context):
with GoodNeighbor() as toggle: with GoodNeighbor() as toggle:
mesh = context.active_object.data obj = context.active_object
mesh = obj.data
vcols = mesh.vertex_colors vcols = mesh.vertex_colors
# I have heard tale of some moar "No valid image to bake to" boogs if there is a really # I have heard tale of some moar "No valid image to bake to" boogs if there is a really
@ -217,7 +222,6 @@ class VertexColorLightingOperator(_LightingOperator, bpy.types.Operator):
autocolor = vcols.get("autocolor") autocolor = vcols.get("autocolor")
if autocolor is not None: if autocolor is not None:
vcols.remove(autocolor) vcols.remove(autocolor)
autocolor = vcols.new("autocolor") autocolor = vcols.new("autocolor")
toggle.track(vcols, "active", autocolor) toggle.track(vcols, "active", autocolor)
@ -225,6 +229,7 @@ class VertexColorLightingOperator(_LightingOperator, bpy.types.Operator):
for vcol_layer in mesh.vertex_colors: for vcol_layer in mesh.vertex_colors:
autocol = vcol_layer.name == "autocolor" autocol = vcol_layer.name == "autocolor"
toggle.track(vcol_layer, "active_render", autocol) toggle.track(vcol_layer, "active_render", autocol)
toggle.track(vcol_layer, "active", autocol)
mesh.update() mesh.update()
# Bake settings # Bake settings
@ -232,6 +237,9 @@ class VertexColorLightingOperator(_LightingOperator, bpy.types.Operator):
toggle.track(render, "use_bake_to_vertex_color", True) toggle.track(render, "use_bake_to_vertex_color", True)
self._apply_render_settings(render, toggle) self._apply_render_settings(render, toggle)
# Really and truly make sure we can bake...
ensure_object_can_bake(obj, toggle)
# Bake # Bake
if self._generate_lightgroups(mesh): if self._generate_lightgroups(mesh):
bpy.ops.object.bake_image() bpy.ops.object.bake_image()

Loading…
Cancel
Save