Skip to content

Commit

Permalink
stable bake working.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonNordon4 committed Sep 12, 2024
1 parent a293687 commit 29d7704
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
1 change: 0 additions & 1 deletion entry_lightmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@

lightmapper.__init__.register()


68 changes: 32 additions & 36 deletions lightmapper/lightmapper_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ def _select_correct_uv(self, mesh_objects):

def _create_bakeable_object(self, mesh_objects):
""" Create a combined mesh from the selected objects, including UVs and materials. """
bpy.ops.object.select_all(action='DESELECT')

# Create a new mesh
new_mesh = bpy.data.meshes.new(name="BakeableMesh")

Expand Down Expand Up @@ -185,13 +183,7 @@ def _create_bakeable_object(self, mesh_objects):
new_object.data.update()


# Select the new object and make active.
new_object.select_set(True)
bpy.context.view_layer.objects.active = new_object

# Ensure the lightmap is selected, as that's the UV we're baking to.
new_object.data.uv_layers[0].active_render = True
new_object.data.uv_layers["Lightmap"].active = True


return new_object

Expand Down Expand Up @@ -219,7 +211,22 @@ def _apply_bake_image(self, bake_object):
img_node.name = self.TMP_IMAGE_NODE_NAME
mat.node_tree.nodes.active = img_node
img_node.image = self.bake_image


def _prepare_object_for_bake(self, mesh_objects, bake_object):
""" Get object context ready for baking, disabling the original objects and setting the bake object as the active object. """
# Ensure the original objects are hidden and unselected
for obj in mesh_objects:
obj.select_set(False)
obj.hide_render

# Ensure the bake object is selected and active, with correct UVs
bake_object.select_set(True)
bpy.context.view_layer.objects.active = bake_object

# Ensure the lightmap is selected, as that's the UV we're baking to.
bake_object.data.uv_layers[0].active_render = True
bake_object.data.uv_layers["Lightmap"].active = True


def _setup_bake_settings(self):
""" Set up bake settings for diffuse lightmap baking. """
Expand All @@ -232,10 +239,10 @@ def _setup_bake_settings(self):
bpy.context.scene.render.bake.use_pass_indirect = True
bpy.context.scene.render.bake.use_pass_color = False

bpy.context.scene.render.bake.use_selected_to_active = True
bpy.context.scene.render.bake.use_cage = True
bpy.context.scene.render.bake.cage_extrusion = 0.05
bpy.context.scene.render.bake.cage_object = None
bpy.context.scene.render.bake.use_selected_to_active = False




def execute(self, context):
# Run the update loop as an iterator.
Expand All @@ -253,30 +260,37 @@ def bake(self, context):

mesh_objects = [obj for obj in context.selected_objects if obj.type == 'MESH']

print(f"Meshes found: {mesh_objects}")

print("Validating meshes...")
if not self._validate_mesh_objects(context, mesh_objects):
yield -1

print("Selecting correct UVs...")
self._select_correct_uv(mesh_objects)

yield 1

print("Selected objects are suitable for lightmapping.")
print("Getting bake object...")

print("Creating bake image 'BakeImage'...")
# 2. Create an image to bake to, and a new bake object to be baked to.
self.bake_image = bpy.data.images.new("BakeImage", width=1024, height=1024)

print("Creating bake object...")
bake_object = self._create_bakeable_object(mesh_objects)
yield 1
print("Apply Bake Image to Bake Object.")
self._apply_bake_image(bake_object)
yield 1
print("Preparing Mesh Objects and Bake Object for Baking...")
self._prepare_object_for_bake(mesh_objects, bake_object)
yield 1
print("Setting bake settings...")
self._setup_bake_settings()


print("Starting bake...")
while bpy.ops.object.bake('INVOKE_DEFAULT', type='DIFFUSE') != {'RUNNING_MODAL'}:
yield 1 # 'INVOKE_DEFAULT' will give us the progress bar.
print("Bake is finished, saving image...")
while not self.bake_image.is_dirty:
yield 1

Expand All @@ -290,9 +304,7 @@ def modal(self, context, event):
return {'CANCELLED'}

if event.type == 'TIMER':
self.tick += 1
result = next(self.bake_iterator)
print(f"Tick: {self.tick}, Result: {result}")
if result == 1:
return {"RUNNING_MODAL"}
if result == -1:
Expand All @@ -304,22 +316,6 @@ def modal(self, context, event):

return {'RUNNING_MODAL'}


def register():
print("Registering lightmapper_operators")
bpy.utils.register_class(LIGHTMAPPER_OT_create_lightmap_uv)
bpy.utils.register_class(LIGHTMAPPER_OT_bake_lightmap)

def unregister():
bpy.utils.unregister_class(LIGHTMAPPER_OT_create_lightmap_uv)
bpy.utils.unregister_class(LIGHTMAPPER_OT_bake_lightmap)

if __name__ == "__main__":
register()


return {'RUNNING_MODAL'}

def cancel(self, context):
print("Modal cancelled")
if self._timer:
Expand Down

0 comments on commit 29d7704

Please sign in to comment.