Fix 104401: Import .ase files with swatch groups

The current version of Import Palettes throws a `KeyError` when importing .ase files that have swatch groups in them.

This patch accounts for these.

Co-authored-by: blastframe <kevin@blastframe.com>
Pull Request #104405
This commit is contained in:
Kevin C. Burke
2023-02-13 10:45:14 +01:00
committed by Antonio Vazquez
parent 69beaa0e43
commit 35d72c2da0
2 changed files with 38 additions and 26 deletions

View File

@ -2,8 +2,8 @@
bl_info = {
"name": "Import Palettes",
"author": "Antonio Vazquez",
"version": (1, 0, 0),
"author": "Antonio Vazquez, Kevin C. Burke (@blastframe)",
"version": (1, 0, 1),
"blender": (2, 81, 6),
"location": "File > Import",
"description": "Import Palettes",

View File

@ -99,6 +99,31 @@ def parse(filename):
return [c for c in parse_chunk(data)]
def create_color(data):
valid = False
color = [0, 0, 0]
val = data['values']
if data['mode'] == 'RGB':
valid = True
color[0] = val[0]
color[1] = val[1]
color[2] = val[2]
elif data['mode'] == 'Gray':
valid = True
color[0] = val[0]
color[1] = val[0]
color[2] = val[0]
elif data['mode'] == 'CMYK':
valid = True
color[0] = (1.0 - val[0]) * (1.0 - val[3])
color[1] = (1.0 - val[1]) * (1.0 - val[3])
color[2] = (1.0 - val[2]) * (1.0 - val[3])
if valid:
return color
def load(context, filepath):
output = parse(filepath)
@ -107,33 +132,20 @@ def load(context, filepath):
pal = None
for elm in output:
valid = False
data = elm['data']
color = [0, 0, 0]
val = data['values']
colors = []
if data['mode'] == 'RGB':
valid = True
color[0] = val[0]
color[1] = val[1]
color[2] = val[2]
elif data['mode'] == 'Gray':
valid = True
color[0] = val[0]
color[1] = val[0]
color[2] = val[0]
elif data['mode'] == 'CMYK':
valid = True
color[0] = (1.0 - val[0]) * (1.0 - val[3])
color[1] = (1.0 - val[1]) * (1.0 - val[3])
color[2] = (1.0 - val[2]) * (1.0 - val[3])
if "data" in elm:
colors.append(create_color(elm['data']))
# Create palette color
if valid:
# Create Palette
if pal is None:
pal = bpy.data.palettes.new(name=filename)
if "swatches" in elm:
for swatch in elm['swatches']:
colors.append(create_color(swatch["data"]))
# Create Palette
if pal is None:
pal = bpy.data.palettes.new(name=filename)
for color in colors:
# Create Color
col = pal.colors.new()
col.color[0] = color[0]