1
Fork 0

feat: Implement file writing for exports

Signed-off-by: Lucas Schwiderski <lucas@lschwiderski.de>
This commit is contained in:
Lucas Schwiderski 2021-04-04 20:20:44 +02:00
parent 8b3ae73bce
commit 42d309a6c2
Signed by: lucas
GPG key ID: AA12679AAA6DF4D8
3 changed files with 41 additions and 19 deletions

View file

@ -81,7 +81,7 @@ class SCENE_PT_bitsquid(Panel):
class BitsquidObjectSettings(PropertyGroup): class BitsquidObjectSettings(PropertyGroup):
unit_filepath: StringProperty( filepath: StringProperty(
name="Unit File Path", name="Unit File Path",
description="The directory to store the .unit file in. Needs to be within the project root.", description="The directory to store the .unit file in. Needs to be within the project root.",
default="//", default="//",
@ -133,13 +133,13 @@ class OBJECT_PT_bitsquid(Panel):
layout.use_property_decorate = False layout.use_property_decorate = False
bitsquid = object.bitsquid 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.prop(bitsquid, "export_materials", text="Export materials")
layout.operator("object.bitsquid_export_unit", text="Export .unit") layout.operator("object.bitsquid_export_unit", text="Export .unit")
class BitsquidMaterialSettings(PropertyGroup): class BitsquidMaterialSettings(PropertyGroup):
material_filepath: StringProperty( filepath: StringProperty(
name="Material File Path", name="Material File Path",
description="The directory to store the .material file in. Needs to be within the project root.", description="The directory to store the .material file in. Needs to be within the project root.",
default="//", default="//",
@ -179,7 +179,7 @@ class MATERIAL_PT_bitsquid(Panel):
layout.use_property_decorate = False layout.use_property_decorate = False
bitsquid = context.material.bitsquid 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") layout.operator("object.bitsquid_export_material", text="Export .material")

View file

@ -17,6 +17,7 @@
import os import os
import bpy import bpy
from bitsquid import step from bitsquid import step
from bpy_extras import io_utils
template = """ template = """
@ -82,20 +83,29 @@ variables = {
""" """
def build_filepath(material): def build_filepath(context, material, mode='ABSOLUTE'):
dir = bpy.path.abspath(material.bitsquid.material_filepath) project_root = bpy.path.abspath(context.scene.bitsquid.project_root)
return os.path.join(dir, "{}.material".format(material.name)) 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): def save(self, context, material):
filepath = build_filepath(material) filepath = build_filepath(context, material)
self.report({'INFO'}, "Saving material to " + filepath) self.report({'INFO'}, "Saving material to " + filepath)
namespace = { namespace = {
'material': material, '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'} return {'FINISHED'}

View file

@ -16,10 +16,11 @@
import os import os
import bpy import bpy
from bpy_extras import io_utils
from bitsquid import step from bitsquid import step
from bpy_extras import io_utils
from bitsquid.material import export as material_export from bitsquid.material import export as material_export
template = """ template = """
%if len(materials) > 0: %if len(materials) > 0:
materials = { materials = {
@ -43,28 +44,39 @@ renderables = {
""" """
def build_filepath(object): def build_filepath(context, object, mode='ABSOLUTE'):
dir = bpy.path.abspath(object.bitsquid.unit_filepath) project_root = bpy.path.abspath(context.scene.bitsquid.project_root)
return os.path.join(dir, "{}.unit".format(object.name)) 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): def save(self, context, object):
filepath = build_filepath(object) filepath = build_filepath(context, object)
self.report({'INFO'}, "Saving unit to " + filepath) self.report({'INFO'}, "Saving unit to " + filepath)
materials = [] materials = []
for material_slot in object.material_slots.values(): for material_slot in object.material_slots.values():
material = material_slot.material
path = material_export.build_filepath(context, material, 'RELATIVE')
materials.append({ materials.append({
'name': material_slot.name, 'name': material.name,
'path': material_export.build_filepath(material_slot.material), 'path': path,
}) })
namespace = { namespace = {
'object': object, 'object': object,
'materials': materials, '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'} return {'FINISHED'}