1 # SPDX-FileCopyrightText: 2019-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # This ASE conversion uses code from Marcos A Ojeda http://generic.cx/
8 # http://iamacamera.org/default.aspx?id=109 by Carl Camera and
9 # http://www.colourlovers.com/ase.phps by Chris Williams
13 This script imports a ASE Palette to Blender.
16 Run this script from "File->Import" menu and then load the desired ASE file.
25 chunk_type
= fd
.read(2)
27 if chunk_type
== b
'\x00\x01':
29 o
= dict_for_chunk(fd
)
32 elif chunk_type
== b
'\xC0\x01':
34 o
= dict_for_chunk(fd
)
35 o
['swatches'] = [x
for x
in colors(fd
)]
38 elif chunk_type
== b
'\xC0\x02':
39 # this signals the end of a folder
40 assert fd
.read(4) == b
'\x00\x00\x00\x00'
44 # the file is malformed?
45 assert chunk_type
in [
46 b
'\xC0\x01', b
'\x00\x01', b
'\xC0\x02', b
'\x00\x02']
49 chunk_type
= fd
.read(2)
53 chunk_type
= fd
.read(2)
54 while chunk_type
in [b
'\x00\x01', b
'\x00\x02']:
55 d
= dict_for_chunk(fd
)
57 chunk_type
= fd
.read(2)
58 fd
.seek(-2, os
.SEEK_CUR
)
61 def dict_for_chunk(fd
):
62 chunk_length
= struct
.unpack(">I", fd
.read(4))[0]
63 data
= fd
.read(chunk_length
)
65 title_length
= (struct
.unpack(">H", data
[:2])[0]) * 2
66 title
= data
[2:2 + title_length
].decode("utf-16be").strip('\0')
67 color_data
= data
[2 + title_length
:]
71 'type': 'Color Group' # default to color group
75 fmt
= {b
'RGB': '!fff', b
'Gray': '!f', b
'CMYK': '!ffff', b
'LAB': '!fff'}
76 color_mode
= struct
.unpack("!4s", color_data
[:4])[0].strip()
77 color_values
= list(struct
.unpack(fmt
[color_mode
], color_data
[4:-2]))
79 color_types
= ['Global', 'Spot', 'Process']
80 swatch_type_index
= struct
.unpack(">h", color_data
[-2:])[0]
81 swatch_type
= color_types
[swatch_type_index
]
85 'mode': color_mode
.decode('utf-8'),
86 'values': color_values
88 'type': str(swatch_type
)
95 with
open(filename
, "rb") as data
:
96 header
, v_major
, v_minor
, chunk_count
= struct
.unpack("!4sHHI", data
.read(12))
98 assert header
== b
"ASEF"
99 assert (v_major
, v_minor
) == (1, 0)
101 return [c
for c
in parse_chunk(data
)]
104 def create_color(data
):
110 if data
['mode'] == 'RGB':
115 elif data
['mode'] == 'Gray':
120 elif data
['mode'] == 'CMYK':
122 color
[0] = (1.0 - val
[0]) * (1.0 - val
[3])
123 color
[1] = (1.0 - val
[1]) * (1.0 - val
[3])
124 color
[2] = (1.0 - val
[2]) * (1.0 - val
[3])
129 def load(context
, filepath
):
130 output
= parse(filepath
)
132 (path
, filename
) = os
.path
.split(filepath
)
140 colors
.append(create_color(elm
['data']))
142 if "swatches" in elm
:
143 for swatch
in elm
['swatches']:
144 colors
.append(create_color(swatch
["data"]))
148 pal
= bpy
.data
.palettes
.new(name
=filename
)
152 col
= pal
.colors
.new()
153 col
.color
[0] = color
[0]
154 col
.color
[1] = color
[1]
155 col
.color
[2] = color
[2]