From 4676bac5926b7ebc9cee589ef11149b0f4951a88 Mon Sep 17 00:00:00 2001 From: SimonNordon4 Date: Thu, 12 Sep 2024 10:54:57 +1000 Subject: [PATCH] denoising operation succesful. --- lightmapper/lightmapper_operators.py | 48 ++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/lightmapper/lightmapper_operators.py b/lightmapper/lightmapper_operators.py index c7d8a4d..11d1d9c 100644 --- a/lightmapper/lightmapper_operators.py +++ b/lightmapper/lightmapper_operators.py @@ -181,10 +181,6 @@ def _create_bakeable_object(self, mesh_objects): # Ensure the new object has all materials correctly assigned new_object.data.update() - - - - return new_object def _get_empty_material(self): @@ -217,7 +213,7 @@ def _prepare_object_for_bake(self, mesh_objects, bake_object): # Ensure the original objects are hidden and unselected for obj in mesh_objects: obj.select_set(False) - obj.hide_render + obj.hide_render = True # Ensure the bake object is selected and active, with correct UVs bake_object.select_set(True) @@ -241,8 +237,37 @@ def _setup_bake_settings(self): bpy.context.scene.render.bake.use_selected_to_active = False - - + def _setup_compositor_for_denoising(self): + # Enable use_nodes for the current scene + bpy.context.scene.use_nodes = True + tree = bpy.context.scene.node_tree + + # Clear default nodes + for node in tree.nodes: + tree.nodes.remove(node) + + # Create input image node + input_node = tree.nodes.new(type='CompositorNodeImage') + input_node.image = self.bake_image + + # Create denoise node + denoise_node = tree.nodes.new(type='CompositorNodeDenoise') + + # Create output node + output_node = tree.nodes.new(type='CompositorNodeOutputFile') + output_node.format.file_format = 'HDR' + output_node.format.color_depth = '32' + output_node.base_path = "//denoised_lightmap" + output_node.file_slots[0].path = "denoised_lightmap_####.hdr" + + # Link nodes + tree.links.new(input_node.outputs[0], denoise_node.inputs[0]) + tree.links.new(denoise_node.outputs[0], output_node.inputs[0]) + + def _render_denoised_image(self): + # Run the compositor + bpy.ops.render.render(write_still=True) + def execute(self, context): # Run the update loop as an iterator. @@ -295,6 +320,15 @@ def bake(self, context): yield 1 print("Bake complete.") + print("Setting up compositor for denoising...") + self._setup_compositor_for_denoising() + yield 1 + bpy.ops.render.render(write_still=False) + while bpy.ops.render.render() == {'RUNNING_MODAL'}: + yield 1 + print("Denoising complete.") + print("Saving image...") + self._render_denoised_image() yield 0