glTF exporter: fix division by zero in some specular approximation
[blender-addons.git] / io_import_palette / import_ase.py
blob8f9b5573188e8d6d78e109aaf1857abb343dfa52
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # This ASE conversion uses code from Marcos A Ojeda http://generic.cx/
5 # With notes from
6 # http://iamacamera.org/default.aspx?id=109 by Carl Camera and
7 # http://www.colourlovers.com/ase.phps by Chris Williams
10 """
11 This script imports a ASE Palette to Blender.
13 Usage:
14 Run this script from "File->Import" menu and then load the desired ASE file.
15 """
17 import bpy
18 import os
19 import struct
22 def parse_chunk(fd):
23 chunk_type = fd.read(2)
24 while chunk_type:
25 if chunk_type == b'\x00\x01':
26 # a single color
27 o = dict_for_chunk(fd)
28 yield o
30 elif chunk_type == b'\xC0\x01':
31 # folder/palette
32 o = dict_for_chunk(fd)
33 o['swatches'] = [x for x in colors(fd)]
34 yield o
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'
39 pass
41 else:
42 # the file is malformed?
43 assert chunk_type in [
44 b'\xC0\x01', b'\x00\x01', b'\xC0\x02', b'\x00\x02']
45 pass
47 chunk_type = fd.read(2)
50 def colors(fd):
51 chunk_type = fd.read(2)
52 while chunk_type in [b'\x00\x01', b'\x00\x02']:
53 d = dict_for_chunk(fd)
54 yield d
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:]
67 output = {
68 'name': str(title),
69 'type': 'Color Group' # default to color group
72 if color_data:
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]
81 output.update({
82 'data': {
83 'mode': color_mode.decode('utf-8'),
84 'values': color_values
86 'type': str(swatch_type)
89 return output
92 def parse(filename):
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)
107 pal = None
109 for elm in output:
110 valid = False
111 data = elm['data']
112 color = [0, 0, 0]
113 val = data['values']
115 if data['mode'] == 'RGB':
116 valid = True
117 color[0] = val[0]
118 color[1] = val[1]
119 color[2] = val[2]
120 elif data['mode'] == 'Gray':
121 valid = True
122 color[0] = val[0]
123 color[1] = val[0]
124 color[2] = val[0]
125 elif data['mode'] == 'CMYK':
126 valid = True
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
132 if valid:
133 # Create Palette
134 if pal is None:
135 pal = bpy.data.palettes.new(name=filename)
137 # Create Color
138 col = pal.colors.new()
139 col.color[0] = color[0]
140 col.color[1] = color[1]
141 col.color[2] = color[2]
143 return {'FINISHED'}