Fix io_anim_camera error exporting cameras with quotes in their name
[blender-addons.git] / io_import_palette / import_ase.py
bloba7a8a9c7f0bb0059b6db38e806dab9b9a91a168f
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
9 # <pep8 compliant>
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 load(context, filepath):
105 output = parse(filepath)
107 (path, filename) = os.path.split(filepath)
109 pal = None
111 for elm in output:
112 valid = False
113 data = elm['data']
114 color = [0, 0, 0]
115 val = data['values']
117 if data['mode'] == 'RGB':
118 valid = True
119 color[0] = val[0]
120 color[1] = val[1]
121 color[2] = val[2]
122 elif data['mode'] == 'Gray':
123 valid = True
124 color[0] = val[0]
125 color[1] = val[0]
126 color[2] = val[0]
127 elif data['mode'] == 'CMYK':
128 valid = True
129 color[0] = (1.0 - val[0]) * (1.0 - val[3])
130 color[1] = (1.0 - val[1]) * (1.0 - val[3])
131 color[2] = (1.0 - val[2]) * (1.0 - val[3])
133 # Create palette color
134 if valid:
135 # Create Palette
136 if pal is None:
137 pal = bpy.data.palettes.new(name=filename)
139 # Create Color
140 col = pal.colors.new()
141 col.color[0] = color[0]
142 col.color[1] = color[1]
143 col.color[2] = color[2]
145 return {'FINISHED'}