From 16ff00bce699d09cc89e4280a97b097c59e7e475 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 2 Nov 2021 13:02:14 +0200 Subject: [PATCH] Export raw texture data to a separate file --- blender/io_mspgl/datafile.py | 23 ++++++++++++++++++++--- blender/io_mspgl/export_texture.py | 18 ++++++++---------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/blender/io_mspgl/datafile.py b/blender/io_mspgl/datafile.py index 6ed3ea45..b9d0b3af 100644 --- a/blender/io_mspgl/datafile.py +++ b/blender/io_mspgl/datafile.py @@ -1,3 +1,5 @@ +import struct + class Statement: def __init__(self, keyword, *args): self.keyword = keyword @@ -19,7 +21,7 @@ class Statement: s += " false" elif type(a)==str: s += ' "{}"'.format(a) - elif type(a)==Resource: + elif type(a)==Resource or type(a)==RawData: s += ' "{}"'.format(a.name) else: s += " {}".format(a) @@ -43,6 +45,21 @@ class Token: return self.text +class RawData: + def __init__(self, name, data): + self.name = name + self.data = data + + def collect_references(self): + return [] + + def write_to_file(self, fn): + with open(fn, "wb") as out_file: + out_file.write(b"MDR\x01") + out_file.write(struct.pack(">QH", len(self.data), 0)) + out_file.write(self.data) + + class Resource: def __init__(self, name, keyword): self.name = name @@ -56,11 +73,11 @@ class Resource: resources = [] for a in args: - if type(a)==Resource: + if type(a)==Resource or type(a)==RawData: resources.append(a) if not resources: - raise TypeError("create_reference_statement expected a Resource argument, found none") + raise TypeError("create_reference_statement expected a Resource or RawData argument, found none") self.references += [r for r in resources if r not in self.references] return Statement(keyword, *args) diff --git a/blender/io_mspgl/export_texture.py b/blender/io_mspgl/export_texture.py index 3d21cd1a..dc291c9c 100644 --- a/blender/io_mspgl/export_texture.py +++ b/blender/io_mspgl/export_texture.py @@ -2,10 +2,6 @@ import os import base64 import codecs -def encode_pixels(pixels): - from .datafile import Token - return Token(codecs.decode(b"="+base64.b64encode(bytes(pixels))+b"=", "ascii")) - def pixels_to_rgba(pixels): return (int(p*255) for p in pixels) @@ -28,7 +24,7 @@ def pixels_to_gray(pixels): class TextureExporter: def export_texture(self, tex_node, usage='RGB', *, invert_green=False): image = tex_node.image - from .datafile import Resource, Statement, Token + from .datafile import RawData, Resource, Statement, Token tex_res = Resource(image.name+".tex", "texture") tex_res.statements.append(Statement("type", Token("\\2d"))) @@ -58,14 +54,16 @@ class TextureExporter: pixels = tuple(image.pixels) texdata = "" if usage=='RGBA': - texdata = encode_pixels(pixels_to_rgba(pixels)) + texdata = pixels_to_rgba(pixels) elif usage=='GRAY': - texdata = encode_pixels(pixels_to_gray(pixels)) + texdata = pixels_to_gray(pixels) elif invert_green: - texdata = encode_pixels(pixels_to_rgb_invert_green(pixels)) + texdata = pixels_to_rgb_invert_green(pixels) else: - texdata = encode_pixels(pixels_to_rgb(pixels)) - tex_res.statements.append(Statement("raw_data", texdata)) + texdata = pixels_to_rgb(pixels) + + data = RawData(image.name+".mdr", bytes(texdata)) + tex_res.statements.append(tex_res.create_reference_statement("external_data", data)) return tex_res -- 2.45.2