Skip to content

Commit

Permalink
Modal is functional.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonNordon4 committed Sep 11, 2024
1 parent e03041d commit 356b7e8
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions lightmapper/lightmapper_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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")
Expand Down

0 comments on commit 356b7e8

Please sign in to comment.