From 4486964418f7605965771cbc6d3d684d53b75ac3 Mon Sep 17 00:00:00 2001 From: ethan Date: Wed, 8 Jan 2025 16:48:35 -0500 Subject: [PATCH] Stability adjustments Found some bugs in deeper testing --- __init__.py | 22 ++++++++++++++++++++++ baker.py | 24 ++++++++++++++++-------- blender_manifest.toml | 2 +- operators/core.py | 19 ++++++++++++------- ui.py | 10 ++++------ 5 files changed, 55 insertions(+), 22 deletions(-) diff --git a/__init__.py b/__init__.py index 239061e..0c5ec60 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,25 @@ import importlib +import bpy +from bpy.app.handlers import persistent + +from .ui import register_baker_panels + + +######################### +# HANDLERS +######################### + + +@persistent +def load_post_handler(_dummy): + register_baker_panels() + + +######################### +# REGISTRATION +######################### + module_names = ( "operators.core", @@ -19,6 +39,8 @@ def register(): for mod in modules: mod.register() + bpy.app.handlers.load_post.append(load_post_handler) + def unregister(): for mod in modules: mod.unregister() diff --git a/baker.py b/baker.py index eee5246..a9947bc 100644 --- a/baker.py +++ b/baker.py @@ -40,8 +40,6 @@ def __init__(self): """Called when `bpy.ops.grab_doc.scene_setup` operator is ran. This method is NOT called when created via `CollectionProperty`.""" - gd = bpy.context.scene.gd - self.index = self.get_unique_index(getattr(gd, self.ID)) self.node_input = None self.node_output = None @@ -63,6 +61,14 @@ def __init__(self): name="Export Enabled", default=self.MARMOSET_COMPATIBLE ) + if self.index == -1: + gd = bpy.context.scene.gd + self.index = self.get_unique_index(getattr(gd, self.ID)) + if self.index > 0: + self.node_name = self.get_node_name(self.NAME, self.index+1) + if not self.suffix[-1].isdigit(): + self.suffix = f"{self.suffix}_{self.index+1}" + @staticmethod def get_unique_index(collection: CollectionProperty) -> int: """Get a unique index value based on a given `CollectionProperty`.""" @@ -383,7 +389,7 @@ class Curvature(Baker): VIEW_TRANSFORM = "Standard" MARMOSET_COMPATIBLE = True REQUIRED_SOCKETS = () - OPTIONAL_SOCKETS = Baker.OPTIONAL_SOCKETS + OPTIONAL_SOCKETS = () SUPPORTED_ENGINES = Baker.SUPPORTED_ENGINES[1:] def __init__(self): @@ -562,7 +568,7 @@ class Height(Baker): VIEW_TRANSFORM = "Raw" MARMOSET_COMPATIBLE = True REQUIRED_SOCKETS = () - OPTIONAL_SOCKETS = Baker.OPTIONAL_SOCKETS + OPTIONAL_SOCKETS = () SUPPORTED_ENGINES = Baker.SUPPORTED_ENGINES[:-1] def setup(self) -> None: @@ -792,7 +798,7 @@ class Roughness(Baker): VIEW_TRANSFORM = "Raw" MARMOSET_COMPATIBLE = False REQUIRED_SOCKETS = (NAME,) - OPTIONAL_SOCKETS = Baker.OPTIONAL_SOCKETS + OPTIONAL_SOCKETS = () SUPPORTED_ENGINES = Baker.SUPPORTED_ENGINES[:-1] def node_setup(self): @@ -832,7 +838,7 @@ class Color(Baker): VIEW_TRANSFORM = "Standard" MARMOSET_COMPATIBLE = False REQUIRED_SOCKETS = (NAME,) - OPTIONAL_SOCKETS = Baker.OPTIONAL_SOCKETS + OPTIONAL_SOCKETS = () SUPPORTED_ENGINES = Baker.SUPPORTED_ENGINES[:-1] def node_setup(self): @@ -863,7 +869,7 @@ class Emissive(Baker): VIEW_TRANSFORM = "Standard" MARMOSET_COMPATIBLE = False REQUIRED_SOCKETS = ("Emission Color", "Emission Strength") - OPTIONAL_SOCKETS = Baker.OPTIONAL_SOCKETS + OPTIONAL_SOCKETS = () SUPPORTED_ENGINES = Baker.SUPPORTED_ENGINES[:-1] def node_setup(self): @@ -883,6 +889,8 @@ def node_setup(self): links = self.node_tree.links links.new(emission.inputs["Color"], self.node_input.outputs["Emission Color"]) + links.new(emission.inputs["Strength"], + self.node_input.outputs["Emission Strength"]) links.new(self.node_output.inputs["Shader"], emission.outputs["Emission"]) @@ -899,7 +907,7 @@ class Metallic(Baker): VIEW_TRANSFORM = "Raw" MARMOSET_COMPATIBLE = False REQUIRED_SOCKETS = (NAME,) - OPTIONAL_SOCKETS = Baker.OPTIONAL_SOCKETS + OPTIONAL_SOCKETS = () SUPPORTED_ENGINES = Baker.SUPPORTED_ENGINES[:-1] def node_setup(self): diff --git a/blender_manifest.toml b/blender_manifest.toml index bb513a7..97d3441 100644 --- a/blender_manifest.toml +++ b/blender_manifest.toml @@ -1,7 +1,7 @@ schema_version = "1.0.0" id = "GrabDoc" -version = "2.0.1" +version = "2.0.0" name = "GrabDoc" tagline = "A trim & tileable baker for Blender" maintainer = "Ethan Simon-Law " diff --git a/operators/core.py b/operators/core.py index da4efd9..bb10e76 100644 --- a/operators/core.py +++ b/operators/core.py @@ -88,7 +88,6 @@ def execute(self, context: Context): for baker_prop in get_baker_collections(): baker_prop.clear() baker = baker_prop.add() - baker.__init__() # pylint: disable=C2801 register_baker_panels() scene_setup(self, context) @@ -124,7 +123,6 @@ class GRABDOC_OT_baker_add(Operator): def execute(self, context: Context): baker_prop = getattr(context.scene.gd, self.map_type) baker = baker_prop.add() - baker.__init__() # pylint: disable=C2801 register_baker_panels() baker.node_setup() return {'FINISHED'} @@ -144,7 +142,10 @@ def execute(self, context: Context): baker = get_baker_by_index(baker_prop, self.baker_index) if baker.node_tree: bpy.data.node_groups.remove(baker.node_tree) - baker_prop.remove(self.baker_index) + for idx, bake_map in enumerate(baker_prop): + if bake_map.index == baker.index: + baker_prop.remove(idx) + break register_baker_panels() return {'FINISHED'} @@ -415,7 +416,7 @@ class GRABDOC_OT_baker_preview(Operator): """Preview the selected bake map type""" bl_idname = "grab_doc.baker_preview" bl_label = "" - bl_options = {'REGISTER', 'INTERNAL'} + bl_options = {'REGISTER', 'UNDO', 'INTERNAL'} baker_index: IntProperty() map_type: StringProperty() @@ -491,9 +492,9 @@ def execute(self, context: Context): # TODO: Necessary to save? self.saved_properties['bpy.context.scene.gd.engine'] = gd.engine - gd.preview_state = True - gd.preview_map_type = self.map_type - gd.engine = 'grabdoc' + gd.preview_state = True + gd.preview_map_type = self.map_type + gd.engine = 'grabdoc' self.last_ob_amount = len(bpy.data.objects) self.disable_binds = not self.user_preferences.disable_preview_binds @@ -549,6 +550,10 @@ class GRABDOC_OT_baker_preview_export(Operator): @classmethod def poll(cls, context: Context) -> bool: + gd = context.scene.gd + if gd.filepath == "//" and not bpy.data.filepath: + cls.poll_message_set("Relative export path but file not saved") + return False return not GRABDOC_OT_baker_export_single.poll(context) def execute(self, context: Context): diff --git a/ui.py b/ui.py index f8612fa..d43c53c 100644 --- a/ui.py +++ b/ui.py @@ -302,7 +302,7 @@ def draw_header(self, context: Context): row2.prop(self.baker, 'enabled', text="") preview = row2.operator("grab_doc.baker_preview", text=text) preview.map_type = self.baker.ID - preview.baker_index = index + preview.baker_index = self.baker.index row2.operator("grab_doc.baker_export_single", text="", icon='RENDER_STILL').map_type = self.baker.ID @@ -312,7 +312,7 @@ def draw_header(self, context: Context): return remove = row.operator("grab_doc.baker_remove", text="", icon='TRASH') remove.map_type = self.baker.ID - remove.baker_index = index + remove.baker_index = self.baker.index def draw(self, context: Context): self.baker.draw(context, self.layout) @@ -329,13 +329,11 @@ def create_baker_panels(): baker_classes = [] for baker_prop in get_baker_collections(): for baker in baker_prop: + if baker.index == -1: + baker.__init__() # pylint: disable=C2801 class_name = f"GRABDOC_PT_{baker.ID}_{baker.index}" panel_cls = type(class_name, (BakerPanel,), {}) panel_cls.baker = baker - if baker.index > 0: - baker.node_name = baker.get_node_name(baker.NAME, baker.index+1) - if not baker.suffix[-1].isdigit(): - baker.suffix = f"{baker.suffix}_{baker.index+1}" baker_classes.append(panel_cls) return baker_classes