From fe9ad74fd5cee7ab074dc1b17dd7c440a506339a Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 4 Dec 2023 15:37:29 +0500 Subject: [PATCH] Use paths relative to .ifc instead of .blend choosing texture paths Texture path file dialog is also filtering image files now. Removed subtype from `FILE_PATH` from all related props since `layout.prop()` for those props starts file dialog that cannot return path relative to .ifc. It can return path relative to .blend - though it can be processed in props update to convert to relative to .ifc, this doesn't allow setting relative paths if .blend is not saved. Therefore, I've created a separate operator. add --- .../blenderbim/bim/module/style/__init__.py | 1 + .../blenderbim/bim/module/style/operator.py | 40 +++++++++++++++++++ .../blenderbim/bim/module/style/prop.py | 5 --- .../blenderbim/bim/module/style/ui.py | 7 ++-- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/style/__init__.py b/src/blenderbim/blenderbim/bim/module/style/__init__.py index 03660a6502..c63d33ed0c 100644 --- a/src/blenderbim/blenderbim/bim/module/style/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/style/__init__.py @@ -25,6 +25,7 @@ classes = ( operator.AddStyle, operator.BrowseExternalStyle, operator.ClearTextureMapPath, + operator.ChooseTextureMapPath, operator.DisableAddingPresentationStyle, operator.DisableEditingExternalStyle, operator.DisableEditingStyle, diff --git a/src/blenderbim/blenderbim/bim/module/style/operator.py b/src/blenderbim/blenderbim/bim/module/style/operator.py index e36abe34f5..f94840013c 100644 --- a/src/blenderbim/blenderbim/bim/module/style/operator.py +++ b/src/blenderbim/blenderbim/bim/module/style/operator.py @@ -410,6 +410,46 @@ class SelectByStyle(bpy.types.Operator, tool.Ifc.Operator): core.select_by_style(tool.Style, tool.Spatial, style=tool.Ifc.get().by_id(self.style)) +class ChooseTextureMapPath(bpy.types.Operator): + bl_idname = "bim.choose_texture_map_path" + bl_label = "Choose Texture Map Path" + bl_options = {"REGISTER", "UNDO"} + texture_map_prop: bpy.props.StringProperty(default="") + + use_relative_path: bpy.props.BoolProperty(name="Use Relative Path", default=True) + filepath: bpy.props.StringProperty( + name="File Path", description="Filepath used to import from", maxlen=1024, default="", subtype="FILE_PATH" + ) + filter_image: bpy.props.BoolProperty(default=True, options={"HIDDEN", "SKIP_SAVE"}) + filter_folder: bpy.props.BoolProperty(default=True, options={"HIDDEN", "SKIP_SAVE"}) + + def invoke(self, context, event): + context.window_manager.fileselect_add(self) + return {"RUNNING_MODAL"} + + @classmethod + def poll(cls, context): + poll = getattr(context, "material", None) + if not poll: + cls.poll_message_set("Select a material") + return poll + + def execute(self, context): + if not self.texture_map_prop: + self.report({"ERROR"}, "Provide a texture map") + return {"CANCELLED"} + + abs_path = Path(self.filepath) + if self.use_relative_path: + image_filepath = abs_path.relative_to(Path(tool.Ifc.get_path()).parent) + else: + image_filepath = abs_path + + props = context.material.BIMStyleProperties + setattr(props, self.texture_map_prop, image_filepath.as_posix()) + return {"FINISHED"} + + class ClearTextureMapPath(bpy.types.Operator): bl_idname = "bim.clear_texture_map_path" bl_label = "Clear Texture Map Path" diff --git a/src/blenderbim/blenderbim/bim/module/style/prop.py b/src/blenderbim/blenderbim/bim/module/style/prop.py index d6a99096dd..5995189aba 100644 --- a/src/blenderbim/blenderbim/bim/module/style/prop.py +++ b/src/blenderbim/blenderbim/bim/module/style/prop.py @@ -269,21 +269,18 @@ class BIMStyleProperties(PropertyGroup): name="NormalMap", maxlen=1024, default="", - subtype="FILE_PATH", update=update_shader_graph, ) emissive_path: bpy.props.StringProperty( name="Emissive", maxlen=1024, default="", - subtype="FILE_PATH", update=update_shader_graph, ) metallic_roughness_path: bpy.props.StringProperty( name="Metallic/Roughness", maxlen=1024, default="", - subtype="FILE_PATH", update=update_shader_graph, description="Green Channel = Roughness,\nBlue Channel = Metallic", ) @@ -291,7 +288,6 @@ class BIMStyleProperties(PropertyGroup): name="Diffuse", maxlen=1024, default="", - subtype="FILE_PATH", update=update_shader_graph, ) occlusion_path: bpy.props.StringProperty( @@ -299,6 +295,5 @@ class BIMStyleProperties(PropertyGroup): description="Note that occlusion isn't actually used in Blender shader, we're just storing the data", maxlen=1024, default="", - subtype="FILE_PATH", update=update_shader_graph, ) diff --git a/src/blenderbim/blenderbim/bim/module/style/ui.py b/src/blenderbim/blenderbim/bim/module/style/ui.py index d4eea94b07..d96d0a5376 100644 --- a/src/blenderbim/blenderbim/bim/module/style/ui.py +++ b/src/blenderbim/blenderbim/bim/module/style/ui.py @@ -417,15 +417,14 @@ class BIM_PT_STYLE_GRAPH(Panel): def add_texture_path(path_name): row = layout.row(align=True) row.prop(props, path_name) - op = row.operator("bim.clear_texture_map_path", text="", icon="X") - op.texture_map_prop = path_name + op_path = row.operator("bim.choose_texture_map_path", text="", icon="FILEBROWSER") + op_clear = row.operator("bim.clear_texture_map_path", text="", icon="X") + op_path.texture_map_prop = op_clear.texture_map_prop = path_name layout.separator() texture_maps = TEXTURE_MAPS_BY_METHODS.get(props.reflectance_method, []) if not texture_maps: return - if not bpy.data.filepath: - layout.label(text="Save .blend file to keep relative paths", icon="ERROR") layout.prop(props, "uv_mode") layout.label(text="Texture Maps:")