diff --git a/source/__init__.py b/source/__init__.py index b1be2b0..01aea73 100644 --- a/source/__init__.py +++ b/source/__init__.py @@ -27,6 +27,7 @@ if "bpy" in locals(): else: from . import import_ase from . import import_krita + from . import import_jascpal import bpy from bpy.props import ( @@ -75,15 +76,34 @@ class importKPL(bpy.types.Operator, ImportHelper): def draw(self, context): pass +class ImportJASCPAL(bpy.types.Operator, ImportHelper): + """Load a JASC Palette File""" + bl_idname = "import_jascpal.read" + bl_label = "Import Palette" + bl_options = {'PRESET', 'UNDO'} + + filename_ext = ".pal" + filter_glob: StringProperty( + default="*.pal", + options={'HIDDEN'}, + ) + + def execute(self, context): + return import_jascpal.load(context, self.properties.filepath) + + def draw(self, context): + pass def menu_func_import(self, context): self.layout.operator(importKPL.bl_idname, text="KPL Palette (.kpl)") self.layout.operator(ImportASE.bl_idname, text="ASE Palette (.ase)") + self.layout.operator(ImportJASCPAL.bl_idname, text="JASC/Gale Palette (.pal)") classes = ( ImportASE, importKPL, + ImportJASCPAL, ) diff --git a/source/import_jascpal.py b/source/import_jascpal.py new file mode 100644 index 0000000..1efe1ef --- /dev/null +++ b/source/import_jascpal.py @@ -0,0 +1,45 @@ +# SPDX-FileCopyrightText: 2024 Blender Foundation +# +# SPDX-License-Identifier: GPL-2.0-or-later + + +""" +This script imports a JASC-PAL/GraphicsGale Palette to Blender. + +Usage: +Run this script from "File->Import" menu and then load the desired PAL file. +""" + +import bpy +import os +import struct + + +def load(context, filepath): + (path, filename) = os.path.split(filepath) + + finput = open(filepath) + + line = finput.readline() + assert line.strip() == "JASC-PAL" + line = finput.readline() + assert line.strip() == "0100" + line = finput.readline() + num_colors = int(line.strip()) + assert num_colors >= 1 + + pal = bpy.data.palettes.new(name=filename) + + + for _ in range(num_colors): + line = finput.readline() + values = line.split() + palcol = pal.colors.new() + palcol.color[0] = int(values[0]) / 255.0 + palcol.color[1] = int(values[1]) / 255.0 + palcol.color[2] = int(values[2]) / 255.0 + + finput.close() + + return {'FINISHED'} +