diff --git a/addon/bitsquid/__init__.py b/addon/bitsquid/__init__.py index b67647d..04538d6 100644 --- a/addon/bitsquid/__init__.py +++ b/addon/bitsquid/__init__.py @@ -81,7 +81,7 @@ class SCENE_PT_bitsquid(Panel): class BitsquidObjectSettings(PropertyGroup): - unit_filepath: StringProperty( + filepath: StringProperty( name="Unit File Path", description="The directory to store the .unit file in. Needs to be within the project root.", default="//", @@ -133,13 +133,13 @@ class OBJECT_PT_bitsquid(Panel): layout.use_property_decorate = False bitsquid = object.bitsquid - layout.prop(bitsquid, "unit_filepath", text="Unit File Path") + layout.prop(bitsquid, "filepath", text="Unit File Path") layout.prop(bitsquid, "export_materials", text="Export materials") layout.operator("object.bitsquid_export_unit", text="Export .unit") class BitsquidMaterialSettings(PropertyGroup): - material_filepath: StringProperty( + filepath: StringProperty( name="Material File Path", description="The directory to store the .material file in. Needs to be within the project root.", default="//", @@ -179,7 +179,7 @@ class MATERIAL_PT_bitsquid(Panel): layout.use_property_decorate = False bitsquid = context.material.bitsquid - layout.prop(bitsquid, "material_filepath", text="Material File Path") + layout.prop(bitsquid, "filepath", text="Material File Path") layout.operator("object.bitsquid_export_material", text="Export .material") diff --git a/addon/bitsquid/material/export.py b/addon/bitsquid/material/export.py index e32276a..7e4ec97 100644 --- a/addon/bitsquid/material/export.py +++ b/addon/bitsquid/material/export.py @@ -17,6 +17,7 @@ import os import bpy from bitsquid import step +from bpy_extras import io_utils template = """ @@ -82,20 +83,29 @@ variables = { """ -def build_filepath(material): - dir = bpy.path.abspath(material.bitsquid.material_filepath) - return os.path.join(dir, "{}.material".format(material.name)) +def build_filepath(context, material, mode='ABSOLUTE'): + project_root = bpy.path.abspath(context.scene.bitsquid.project_root) + base_src = os.path.dirname(context.blend_data.filepath) + print(base_src, bpy.path.abspath(project_root)) + filename = "{}.material".format(material.name) + return io_utils.path_reference( + os.path.join(material.bitsquid.filepath, filename), + base_src=base_src, + base_dst=project_root, + mode=mode, + ) def save(self, context, material): - filepath = build_filepath(material) + filepath = build_filepath(context, material) self.report({'INFO'}, "Saving material to " + filepath) namespace = { 'material': material, } - content = step.Template(template).expand(namespace) + content = step.Template(template, strip=False).expand(namespace) - self.report({'INFO'}, content) + with open(filepath, "w", encoding="utf8", newline="\n") as f: + f.write(content) return {'FINISHED'} diff --git a/addon/bitsquid/unit/export.py b/addon/bitsquid/unit/export.py index 4e9f64f..268622c 100644 --- a/addon/bitsquid/unit/export.py +++ b/addon/bitsquid/unit/export.py @@ -16,10 +16,11 @@ import os import bpy -from bpy_extras import io_utils from bitsquid import step +from bpy_extras import io_utils from bitsquid.material import export as material_export + template = """ %if len(materials) > 0: materials = { @@ -43,28 +44,39 @@ renderables = { """ -def build_filepath(object): - dir = bpy.path.abspath(object.bitsquid.unit_filepath) - return os.path.join(dir, "{}.unit".format(object.name)) +def build_filepath(context, object, mode='ABSOLUTE'): + project_root = bpy.path.abspath(context.scene.bitsquid.project_root) + base_src = os.path.dirname(context.blend_data.filepath) + print(base_src, bpy.path.abspath(project_root)) + filename = "{}.unit".format(object.name) + return io_utils.path_reference( + os.path.join(object.bitsquid.filepath, filename), + base_src=base_src, + base_dst=project_root, + mode=mode, + ) def save(self, context, object): - filepath = build_filepath(object) + filepath = build_filepath(context, object) self.report({'INFO'}, "Saving unit to " + filepath) materials = [] for material_slot in object.material_slots.values(): + material = material_slot.material + path = material_export.build_filepath(context, material, 'RELATIVE') materials.append({ - 'name': material_slot.name, - 'path': material_export.build_filepath(material_slot.material), + 'name': material.name, + 'path': path, }) namespace = { 'object': object, 'materials': materials, } - content = step.Template(template).expand(namespace) + content = step.Template(template, strip=False).expand(namespace) - self.report({'INFO'}, content) + with open(filepath, "w", encoding="utf8", newline="\n") as f: + f.write(content) return {'FINISHED'}