From 356b7e8690c5a9fb7a7c27d4754b357f8d212073 Mon Sep 17 00:00:00 2001 From: SimonNordon4 Date: Wed, 11 Sep 2024 16:32:33 +1000 Subject: [PATCH] Modal is functional. --- lightmapper/lightmapper_operators.py | 53 +++++++++++++++++----------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/lightmapper/lightmapper_operators.py b/lightmapper/lightmapper_operators.py index 1511bed..7a9a5ce 100644 --- a/lightmapper/lightmapper_operators.py +++ b/lightmapper/lightmapper_operators.py @@ -34,14 +34,15 @@ def execute(self, context): return {'FINISHED'} - - class LIGHTMAPPER_OT_bake_lightmap(bpy.types.Operator): bl_idname = "lightmapper.bake_lightmap" bl_label = "Bake Lightmap" bl_description = "Bake lightmap for selected objects" bl_options = {'REGISTER', 'UNDO'} - + + _timer = None + bake_image = None + def _setup_bake_settings(self): """ Set up bake settings for diffuse lightmap baking. """ bpy.context.scene.render.engine = 'CYCLES' @@ -54,40 +55,50 @@ def _setup_bake_settings(self): bpy.context.scene.render.bake.use_pass_color = False bpy.context.scene.render.bake.use_selected_to_active = False - + def execute(self, context): self._setup_bake_settings() - # check a mesh is selected + + # Check if a mesh is selected if not any(obj.type == 'MESH' for obj in context.selected_objects): self.report({'ERROR'}, "No mesh objects selected.") return {'CANCELLED'} - # create a new image to bake - bake_image = bpy.data.images.new("BakeImage", width=1024, height=1024) - - # get the first material in the object being baked, create an image node with our new image and make it active + # Create a new image to bake + self.bake_image = bpy.data.images.new("BakeImage", width=1024, height=1024) + + # Get the first material in the object being baked, create an image node with our new image and make it active obj = context.selected_objects[0] mat = obj.material_slots[0].material node_tree = mat.node_tree nodes = node_tree.nodes image_node = nodes.new(type='ShaderNodeTexImage') - image_node.image = bake_image + image_node.image = self.bake_image node_tree.nodes.active = image_node - + print("Baking started!") - #bake object - bpy.ops.object.bake('INVOKE_DEFAULT', type='DIFFUSE') - - print("Baking finished!") - return {'FINISHED'} + # Start modal timer to check for bake completion + self._timer = context.window_manager.event_timer_add(0.5, window=context.window) # Check every 0.5 seconds + context.window_manager.modal_handler_add(self) + bpy.ops.object.bake('INVOKE_DEFAULT', type='DIFFUSE') - - - + return {'RUNNING_MODAL'} + + def modal(self, context, event): + if event.type == 'TIMER': + # Check if the image has been modified (baking should make the image 'dirty') + if self.bake_image.is_dirty: + print("Baking finished!") + context.window_manager.event_timer_remove(self._timer) + return {'FINISHED'} + + return {'RUNNING_MODAL'} -from bpy.utils import register_class, unregister_class -from .lightmapper_properties import LIGHTMAPPER_PT_properties + def cancel(self, context): + if self._timer: + context.window_manager.event_timer_remove(self._timer) + return {'CANCELLED'} def register(): print("Registering lightmapper_operators")