From d5253a8cbf5f857537df10ebe31389316998bc38 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Wed, 22 Nov 2023 11:55:07 +0500 Subject: [PATCH] store path to bcf in Blender file to reaccess it on Blender restart Previously it was throwing errors if load .bcf and then restart Blender, since it was just trying to reaccess file in BcfStore.bcfxml which is gone after Blender restart. Now we store filepath to bcf in Blender properties to reload it after Blender restart. Removed BCFProperties.is_loaded as it won't be possible to rely on it after Blender restart - after restart it will still indicate that file is loaded and it won't be possible to change it from .poll and .draw if file is gone. --- .../blenderbim/bim/module/bcf/bcfstore.py | 33 +++++++++++++++---- .../blenderbim/bim/module/bcf/operator.py | 11 +++---- .../blenderbim/bim/module/bcf/prop.py | 16 ++++----- .../blenderbim/bim/module/bcf/ui.py | 3 +- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/bcf/bcfstore.py b/src/blenderbim/blenderbim/bim/module/bcf/bcfstore.py index 9e67c770b6..b9eeed333d 100644 --- a/src/blenderbim/blenderbim/bim/module/bcf/bcfstore.py +++ b/src/blenderbim/blenderbim/bim/module/bcf/bcfstore.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU General Public License # along with BlenderBIM Add-on. If not, see . +import os +import bpy import bcf import bcf.v2.bcfxml @@ -23,12 +25,31 @@ import bcf.v2.bcfxml class BcfStore: bcfxml = None - @staticmethod - def get_bcfxml(): - if not BcfStore.bcfxml: - BcfStore.bcfxml = bcf.v2.bcfxml.BcfXml() - return BcfStore.bcfxml + @classmethod + def get_bcfxml(cls): + if not cls.bcfxml: + bcf_filepath = bpy.context.scene.BCFProperties.bcf_file + if not os.path.isabs(bcf_filepath): + bcf_filepath = os.path.abspath(os.path.join(bpy.path.abspath("//"), bcf_filepath)) + if bcf_filepath: + try: + cls.bcfxml = bcf.bcfxml.load(bcf_filepath) + except: + # there will be a plenty of "Permission denied" errors + # as many poll() methods will try to access the bcfxml simultaneously + # the first time it's loaded + pass + return cls.bcfxml @classmethod - def set(cls, bcfxml): + def set(cls, bcfxml, filepath): cls.bcfxml = bcfxml + bpy.context.scene.BCFProperties.bcf_file = filepath + + @classmethod + def set_by_filepath(cls, filepath): + cls.set(None, filepath) + + @classmethod + def unload_bcfxml(cls): + cls.set(None, "") diff --git a/src/blenderbim/blenderbim/bim/module/bcf/operator.py b/src/blenderbim/blenderbim/bim/module/bcf/operator.py index 44fd7d548b..990d756e39 100644 --- a/src/blenderbim/blenderbim/bim/module/bcf/operator.py +++ b/src/blenderbim/blenderbim/bim/module/bcf/operator.py @@ -42,11 +42,9 @@ class NewBcfProject(bpy.types.Operator): bl_options = {"REGISTER", "UNDO"} def execute(self, context): - context.scene.BCFProperties.is_loaded = False bcfxml = bcf.v2.bcfxml.BcfXml.create_new("New Project") - bcfstore.BcfStore.set(bcfxml) + bcfstore.BcfStore.set(bcfxml, "") bpy.ops.bim.load_bcf_project() - context.scene.BCFProperties.is_loaded = True return {"FINISHED"} @@ -59,11 +57,10 @@ class LoadBcfProject(bpy.types.Operator): def execute(self, context): if self.filepath: - bcfstore.BcfStore.bcfxml = bcf.bcfxml.load(self.filepath) + bcfstore.BcfStore.set_by_filepath(self.filepath) bcfxml = bcfstore.BcfStore.get_bcfxml() context.scene.BCFProperties.name = bcfxml.project.name bpy.ops.bim.load_bcf_topics() - context.scene.BCFProperties.is_loaded = True return {"FINISHED"} def invoke(self, context, event): @@ -77,8 +74,7 @@ class UnloadBcfProject(bpy.types.Operator): bl_options = {"REGISTER", "UNDO"} def execute(self, context): - bcfstore.BcfStore.set(None) - context.scene.BCFProperties.is_loaded = False + bcfstore.BcfStore.unload_bcfxml() return {"FINISHED"} @@ -267,6 +263,7 @@ class SaveBcfProject(bpy.types.Operator): def execute(self, context): bcfxml = bcfstore.BcfStore.get_bcfxml() bcfxml.save(self.filepath) + bcfstore.BcfStore.set(bcfxml, self.filepath) return {"FINISHED"} def invoke(self, context, event): diff --git a/src/blenderbim/blenderbim/bim/module/bcf/prop.py b/src/blenderbim/blenderbim/bim/module/bcf/prop.py index a989fb7082..7dc6f29757 100644 --- a/src/blenderbim/blenderbim/bim/module/bcf/prop.py +++ b/src/blenderbim/blenderbim/bim/module/bcf/prop.py @@ -41,37 +41,37 @@ def purge(): def updateBcfReferenceLink(self, context): - if context.scene.BCFProperties.is_loaded: + if bcfstore.BcfStore.get_bcfxml(): bpy.ops.bim.edit_bcf_reference_links() def updateBcfLabel(self, context): - if context.scene.BCFProperties.is_loaded: + if bcfstore.BcfStore.get_bcfxml(): bpy.ops.bim.edit_bcf_labels() def updateBcfProjectName(self, context): - if self.is_loaded: + if bcfstore.BcfStore.get_bcfxml(): bpy.ops.bim.edit_bcf_project_name() def updateBcfAuthor(self, context): - if self.is_loaded: + if bcfstore.BcfStore.get_bcfxml(): bpy.ops.bim.edit_bcf_author() def updateBcfTopicName(self, context): - if context.scene.BCFProperties.is_loaded: + if bcfstore.BcfStore.get_bcfxml(): bpy.ops.bim.edit_bcf_topic_name() def updateBcfTopicIsEditable(self, context): - if context.scene.BCFProperties.is_loaded and not self.is_editable: + if bcfstore.BcfStore.get_bcfxml() and not self.is_editable: bpy.ops.bim.edit_bcf_topic() def updateBcfCommentIsEditable(self, context): - if context.scene.BCFProperties.is_loaded and not self.is_editable: + if bcfstore.BcfStore.get_bcfxml() and not self.is_editable: bpy.ops.bim.edit_bcf_comment(comment_guid=self.name) @@ -151,7 +151,7 @@ class BcfTopic(PropertyGroup): class BCFProperties(PropertyGroup): - is_loaded: BoolProperty(name="Is Loaded", default=False) + bcf_file: StringProperty(name="BCF File") comment_text_width: IntProperty(name="Comment Text Width", default=40) name: StringProperty(default="", name="Project Name", update=updateBcfProjectName) author: StringProperty(default="john@doe.com", name="Author Email", update=updateBcfAuthor) diff --git a/src/blenderbim/blenderbim/bim/module/bcf/ui.py b/src/blenderbim/blenderbim/bim/module/bcf/ui.py index 58f3c0a858..9a96f7800d 100644 --- a/src/blenderbim/blenderbim/bim/module/bcf/ui.py +++ b/src/blenderbim/blenderbim/bim/module/bcf/ui.py @@ -39,8 +39,7 @@ class BIM_PT_bcf(Panel): scene = context.scene props = scene.BCFProperties - - if not props.is_loaded: + if not bcfstore.BcfStore.get_bcfxml(): row = layout.row(align=True) row.operator("bim.new_bcf_project", text="New Project") row.operator("bim.load_bcf_project", text="Load Project")