Import_3ds: Improved distance cue chunk import
[blender-addons.git] / io_import_palette / import_ase.py
blob5b087ecebf90634a0035d745effcfbca38ab604c
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/
7 # With notes from
8 # http://iamacamera.org/default.aspx?id=109 by Carl Camera and
9 # http://www.colourlovers.com/ase.phps by Chris Williams
12 """
13 This script imports a ASE Palette to Blender.
15 Usage:
16 Run this script from "File->Import" menu and then load the desired ASE file.
17 """
19 import bpy
20 import os
21 import struct
24 def parse_chunk(fd):
25 chunk_type = fd.read(2)
26 while chunk_type:
27 if chunk_type == b'\x00\x01':
28 # a single color
29 o = dict_for_chunk(fd)
30 yield o
32 elif chunk_type == b'\xC0\x01':
33 # folder/palette
34 o = dict_for_chunk(fd)
35 o['swatches'] = [x for x in colors(fd)]
36 yield o
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'
41 pass
43 else:
44 # the file is malformed?
45 assert chunk_type in [
46 b'\xC0\x01', b'\x00\x01', b'\xC0\x02', b'\x00\x02']
47 pass
49 chunk_type = fd.read(2)
52 def colors(fd):
53 chunk_type = fd.read(2)
54 while chunk_type in [b'\x00\x01', b'\x00\x02']:
55 d = dict_for_chunk(fd)
56 yield d
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:]
69 output = {
70 'name': str(title),
71 'type': 'Color Group' # default to color group
74 if color_data:
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]
83 output.update({
84 'data': {
85 'mode': color_mode.decode('utf-8'),
86 'values': color_values
88 'type': str(swatch_type)
91 return output
94 def parse(filename):
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):
106 valid = False
107 color = [0, 0, 0]
108 val = data['values']
110 if data['mode'] == 'RGB':
111 valid = True
112 color[0] = val[0]
113 color[1] = val[1]
114 color[2] = val[2]
115 elif data['mode'] == 'Gray':
116 valid = True
117 color[0] = val[0]
118 color[1] = val[0]
119 color[2] = val[0]
120 elif data['mode'] == 'CMYK':
121 valid = True
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])
126 if valid:
127 return color
129 def load(context, filepath):
130 output = parse(filepath)
132 (path, filename) = os.path.split(filepath)
134 pal = None
136 for elm in output:
137 colors = []
139 if "data" in elm:
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"]))
146 # Create Palette
147 if pal is None:
148 pal = bpy.data.palettes.new(name=filename)
150 for color in colors:
151 # Create Color
152 col = pal.colors.new()
153 col.color[0] = color[0]
154 col.color[1] = color[1]
155 col.color[2] = color[2]
157 return {'FINISHED'}