]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor handling of texture storage properties in the Blender exporter
authorMikko Rasa <tdb@tdb.fi>
Sun, 25 Sep 2022 16:13:43 +0000 (19:13 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 25 Sep 2022 17:21:57 +0000 (20:21 +0300)
blender/io_mspgl/__init__.py
blender/io_mspgl/export_texture.py
blender/io_mspgl/texture.py [new file with mode: 0644]

index 4b6e2ff4dfc3e9063db926c44cc50621c641b596..c23b60f13dfcfa0664aec7b857215ce283b4fde3 100644 (file)
@@ -10,7 +10,7 @@ files = ("animation", "armature", "context", "datafile", "export",
        "export_animation", "export_armature", "export_camera", "export_light",
        "export_material", "export_mesh", "export_object", "export_scene",
        "export_texture", "material", "mesh", "operators", "properties", "scene",
-       "util")
+       "texture", "util")
 
 if "bpy" in locals():
        import imp
index 8e107a718a3644dba03ffc13393c587470c95f97..2269e27ae175a514670c70a295aaada994228dbb 100644 (file)
@@ -38,9 +38,8 @@ class TextureExporter:
                if tex_node.use_mipmap:
                        tex_res.statements.append(Statement("generate_mipmap", True))
 
-               colorspace = image.colorspace_settings.name
-               if len(channels)==1 and colorspace=='sRGB':
-                       raise Exception("Unsupported configuration on texture {}: Grayscale with sRGB".format(image.name))
+               from .texture import Texture
+               texture = Texture(tex_node, channels)
 
                invert_mask = sum(1<<i for i, c in enumerate(channels) if c[0]=='~')
 
@@ -61,17 +60,10 @@ class TextureExporter:
                if not invert_mask and channels==native_channels:
                        if not tex_node.use_mipmap:
                                tex_res.statements.append(Statement("mipmap_levels", 1))
-                       srgb = "_srgb" if colorspace=='sRGB' else ""
+                       srgb = "_srgb" if texture.srgb else ""
                        tex_res.statements.append(Statement("external_image"+srgb, fn))
                else:
-                       if len(channels)==4:
-                               fmt = 'SRGB8_ALPHA8' if colorspace=='sRGB' else 'RGBA8'
-                       elif len(channels)==1:
-                               fmt = 'LUMINANCE8' if channels[0]=='Y' else 'R8'
-                       else:
-                               fmt = 'SRGB8' if colorspace=='sRGB' else 'RGB8'
-
-                       tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
+                       tex_res.statements.append(Statement("storage", Token(texture.pixelformat), texture.width, texture.height))
 
                        pixels = tuple(image.pixels)
                        texdata = ""
diff --git a/blender/io_mspgl/texture.py b/blender/io_mspgl/texture.py
new file mode 100644 (file)
index 0000000..288c662
--- /dev/null
@@ -0,0 +1,17 @@
+class Texture:
+       def __init__(self, tex_node, channels):
+               self.image = tex_node.image
+
+               self.srgb = self.image.colorspace_settings.name=='sRGB'
+               if len(channels)==1 and self.srgb:
+                       raise Exception("Unsupported configuration on texture {}: Grayscale with sRGB".format(self.image.name))
+
+               if len(channels)==4:
+                       self.pixelformat = 'SRGB8_ALPHA8' if self.srgb else 'RGBA8'
+               elif len(channels)==1:
+                       self.pixelformat = 'LUMINANCE8' if channels[0]=='Y' else 'R8'
+               else:
+                       self.pixelformat = 'SRGB8' if self.srgb else 'RGB8'
+
+               self.width = self.image.size[0]
+               self.height = self.image.size[1]