1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # This ASE conversion uses code from Marcos A Ojeda http://generic.cx/
6 # http://iamacamera.org/default.aspx?id=109 by Carl Camera and
7 # http://www.colourlovers.com/ase.phps by Chris Williams
11 This script imports a ASE Palette to Blender.
14 Run this script from "File->Import" menu and then load the desired ASE file.
23 chunk_type
= fd
.read(2)
25 if chunk_type
== b
'\x00\x01':
27 o
= dict_for_chunk(fd
)
30 elif chunk_type
== b
'\xC0\x01':
32 o
= dict_for_chunk(fd
)
33 o
['swatches'] = [x
for x
in colors(fd
)]
36 elif chunk_type
== b
'\xC0\x02':
37 # this signals the end of a folder
38 assert fd
.read(4) == b
'\x00\x00\x00\x00'
42 # the file is malformed?
43 assert chunk_type
in [
44 b
'\xC0\x01', b
'\x00\x01', b
'\xC0\x02', b
'\x00\x02']
47 chunk_type
= fd
.read(2)
51 chunk_type
= fd
.read(2)
52 while chunk_type
in [b
'\x00\x01', b
'\x00\x02']:
53 d
= dict_for_chunk(fd
)
55 chunk_type
= fd
.read(2)
56 fd
.seek(-2, os
.SEEK_CUR
)
59 def dict_for_chunk(fd
):
60 chunk_length
= struct
.unpack(">I", fd
.read(4))[0]
61 data
= fd
.read(chunk_length
)
63 title_length
= (struct
.unpack(">H", data
[:2])[0]) * 2
64 title
= data
[2:2 + title_length
].decode("utf-16be").strip('\0')
65 color_data
= data
[2 + title_length
:]
69 'type': 'Color Group' # default to color group
73 fmt
= {b
'RGB': '!fff', b
'Gray': '!f', b
'CMYK': '!ffff', b
'LAB': '!fff'}
74 color_mode
= struct
.unpack("!4s", color_data
[:4])[0].strip()
75 color_values
= list(struct
.unpack(fmt
[color_mode
], color_data
[4:-2]))
77 color_types
= ['Global', 'Spot', 'Process']
78 swatch_type_index
= struct
.unpack(">h", color_data
[-2:])[0]
79 swatch_type
= color_types
[swatch_type_index
]
83 'mode': color_mode
.decode('utf-8'),
84 'values': color_values
86 'type': str(swatch_type
)
93 with
open(filename
, "rb") as data
:
94 header
, v_major
, v_minor
, chunk_count
= struct
.unpack("!4sHHI", data
.read(12))
96 assert header
== b
"ASEF"
97 assert (v_major
, v_minor
) == (1, 0)
99 return [c
for c
in parse_chunk(data
)]
102 def load(context
, filepath
):
103 output
= parse(filepath
)
105 (path
, filename
) = os
.path
.split(filepath
)
115 if data
['mode'] == 'RGB':
120 elif data
['mode'] == 'Gray':
125 elif data
['mode'] == 'CMYK':
127 color
[0] = (1.0 - val
[0]) * (1.0 - val
[3])
128 color
[1] = (1.0 - val
[1]) * (1.0 - val
[3])
129 color
[2] = (1.0 - val
[2]) * (1.0 - val
[3])
131 # Create palette color
135 pal
= bpy
.data
.palettes
.new(name
=filename
)
138 col
= pal
.colors
.new()
139 col
.color
[0] = color
[0]
140 col
.color
[1] = color
[1]
141 col
.color
[2] = color
[2]