Cleanup: autopep8 for 3DS i/o
[blender-addons.git] / io_import_palette / import_ase.py
blob460624e22a635b88613f4fa6b06a7e237857ec4c
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 create_color(data):
104 valid = False
105 color = [0, 0, 0]
106 val = data['values']
108 if data['mode'] == 'RGB':
109 valid = True
110 color[0] = val[0]
111 color[1] = val[1]
112 color[2] = val[2]
113 elif data['mode'] == 'Gray':
114 valid = True
115 color[0] = val[0]
116 color[1] = val[0]
117 color[2] = val[0]
118 elif data['mode'] == 'CMYK':
119 valid = True
120 color[0] = (1.0 - val[0]) * (1.0 - val[3])
121 color[1] = (1.0 - val[1]) * (1.0 - val[3])
122 color[2] = (1.0 - val[2]) * (1.0 - val[3])
124 if valid:
125 return color
127 def load(context, filepath):
128 output = parse(filepath)
130 (path, filename) = os.path.split(filepath)
132 pal = None
134 for elm in output:
135 colors = []
137 if "data" in elm:
138 colors.append(create_color(elm['data']))
140 if "swatches" in elm:
141 for swatch in elm['swatches']:
142 colors.append(create_color(swatch["data"]))
144 # Create Palette
145 if pal is None:
146 pal = bpy.data.palettes.new(name=filename)
148 for color in colors:
149 # Create Color
150 col = pal.colors.new()
151 col.color[0] = color[0]
152 col.color[1] = color[1]
153 col.color[2] = color[2]
155 return {'FINISHED'}