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.
This commit is contained in:
Andrej730
2023-11-22 11:55:07 +05:00
parent c7543ff48d
commit df2a50b335
4 changed files with 40 additions and 23 deletions
@@ -16,6 +16,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>. # along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
import os
import bpy
import bcf import bcf
import bcf.v2.bcfxml import bcf.v2.bcfxml
@@ -23,12 +25,31 @@ import bcf.v2.bcfxml
class BcfStore: class BcfStore:
bcfxml = None bcfxml = None
@staticmethod @classmethod
def get_bcfxml(): def get_bcfxml(cls):
if not BcfStore.bcfxml: if not cls.bcfxml:
BcfStore.bcfxml = bcf.v2.bcfxml.BcfXml() bcf_filepath = bpy.context.scene.BCFProperties.bcf_file
return BcfStore.bcfxml 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 @classmethod
def set(cls, bcfxml): def set(cls, bcfxml, filepath):
cls.bcfxml = bcfxml 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, "")
@@ -42,11 +42,9 @@ class NewBcfProject(bpy.types.Operator):
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
def execute(self, context): def execute(self, context):
context.scene.BCFProperties.is_loaded = False
bcfxml = bcf.v2.bcfxml.BcfXml.create_new("New Project") bcfxml = bcf.v2.bcfxml.BcfXml.create_new("New Project")
bcfstore.BcfStore.set(bcfxml) bcfstore.BcfStore.set(bcfxml, "")
bpy.ops.bim.load_bcf_project() bpy.ops.bim.load_bcf_project()
context.scene.BCFProperties.is_loaded = True
return {"FINISHED"} return {"FINISHED"}
@@ -59,11 +57,10 @@ class LoadBcfProject(bpy.types.Operator):
def execute(self, context): def execute(self, context):
if self.filepath: if self.filepath:
bcfstore.BcfStore.bcfxml = bcf.bcfxml.load(self.filepath) bcfstore.BcfStore.set_by_filepath(self.filepath)
bcfxml = bcfstore.BcfStore.get_bcfxml() bcfxml = bcfstore.BcfStore.get_bcfxml()
context.scene.BCFProperties.name = bcfxml.project.name context.scene.BCFProperties.name = bcfxml.project.name
bpy.ops.bim.load_bcf_topics() bpy.ops.bim.load_bcf_topics()
context.scene.BCFProperties.is_loaded = True
return {"FINISHED"} return {"FINISHED"}
def invoke(self, context, event): def invoke(self, context, event):
@@ -77,8 +74,7 @@ class UnloadBcfProject(bpy.types.Operator):
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
def execute(self, context): def execute(self, context):
bcfstore.BcfStore.set(None) bcfstore.BcfStore.unload_bcfxml()
context.scene.BCFProperties.is_loaded = False
return {"FINISHED"} return {"FINISHED"}
@@ -267,6 +263,7 @@ class SaveBcfProject(bpy.types.Operator):
def execute(self, context): def execute(self, context):
bcfxml = bcfstore.BcfStore.get_bcfxml() bcfxml = bcfstore.BcfStore.get_bcfxml()
bcfxml.save(self.filepath) bcfxml.save(self.filepath)
bcfstore.BcfStore.set(bcfxml, self.filepath)
return {"FINISHED"} return {"FINISHED"}
def invoke(self, context, event): def invoke(self, context, event):
@@ -41,37 +41,37 @@ def purge():
def updateBcfReferenceLink(self, context): def updateBcfReferenceLink(self, context):
if context.scene.BCFProperties.is_loaded: if bcfstore.BcfStore.get_bcfxml():
bpy.ops.bim.edit_bcf_reference_links() bpy.ops.bim.edit_bcf_reference_links()
def updateBcfLabel(self, context): def updateBcfLabel(self, context):
if context.scene.BCFProperties.is_loaded: if bcfstore.BcfStore.get_bcfxml():
bpy.ops.bim.edit_bcf_labels() bpy.ops.bim.edit_bcf_labels()
def updateBcfProjectName(self, context): def updateBcfProjectName(self, context):
if self.is_loaded: if bcfstore.BcfStore.get_bcfxml():
bpy.ops.bim.edit_bcf_project_name() bpy.ops.bim.edit_bcf_project_name()
def updateBcfAuthor(self, context): def updateBcfAuthor(self, context):
if self.is_loaded: if bcfstore.BcfStore.get_bcfxml():
bpy.ops.bim.edit_bcf_author() bpy.ops.bim.edit_bcf_author()
def updateBcfTopicName(self, context): def updateBcfTopicName(self, context):
if context.scene.BCFProperties.is_loaded: if bcfstore.BcfStore.get_bcfxml():
bpy.ops.bim.edit_bcf_topic_name() bpy.ops.bim.edit_bcf_topic_name()
def updateBcfTopicIsEditable(self, context): 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() bpy.ops.bim.edit_bcf_topic()
def updateBcfCommentIsEditable(self, context): 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) bpy.ops.bim.edit_bcf_comment(comment_guid=self.name)
@@ -151,7 +151,7 @@ class BcfTopic(PropertyGroup):
class BCFProperties(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) comment_text_width: IntProperty(name="Comment Text Width", default=40)
name: StringProperty(default="", name="Project Name", update=updateBcfProjectName) name: StringProperty(default="", name="Project Name", update=updateBcfProjectName)
author: StringProperty(default="john@doe.com", name="Author Email", update=updateBcfAuthor) author: StringProperty(default="john@doe.com", name="Author Email", update=updateBcfAuthor)
@@ -39,8 +39,7 @@ class BIM_PT_bcf(Panel):
scene = context.scene scene = context.scene
props = scene.BCFProperties props = scene.BCFProperties
if not bcfstore.BcfStore.get_bcfxml():
if not props.is_loaded:
row = layout.row(align=True) row = layout.row(align=True)
row.operator("bim.new_bcf_project", text="New Project") row.operator("bim.new_bcf_project", text="New Project")
row.operator("bim.load_bcf_project", text="Load Project") row.operator("bim.load_bcf_project", text="Load Project")