Add support for JASC-PAL format #1

Merged
Nika Kutsniashvili merged 1 commits from Wuerfel21/io_import_palette:patch-jascpal into main 2024-09-19 15:37:37 +02:00
2 changed files with 65 additions and 0 deletions

View File

@ -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,
)

45
source/import_jascpal.py Normal file
View File

@ -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'}