Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python3
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# <pep8 compliant>
"""
Generates 'userdef_default_theme.c' from a 'userpref.blend' file.
Pass your user preferenes blend file to this script to update the C source file.
eg:
./source/tools/utils/blender_theme_as_c.py ~/.config/blender/2.80/config/userpref.blend
.. or find the latest:
./source/tools/utils/blender_theme_as_c.py $(find ~/.config/blender -name "userpref.blend" | sort | tail -1)
"""
C_SOURCE_HEADER = r'''/*
* Generated by 'source/tools/utils/blender_theme_as_c.py'
*
* Do not hand edit this file!
*/
#include "DNA_userdef_types.h"
#include "BLO_readfile.h"
#ifdef __LITTLE_ENDIAN__
# define RGBA(c) {((c) >> 24) & 0xff, ((c) >> 16) & 0xff, ((c) >> 8) & 0xff, (c) & 0xff}
# define RGB(c) {((c) >> 16) & 0xff, ((c) >> 8) & 0xff, (c) & 0xff}
#else
# define RGBA(c) {(c) & 0xff, ((c) >> 8) & 0xff, ((c) >> 16) & 0xff, ((c) >> 24) & 0xff}
# define RGB(c) {(c) & 0xff, ((c) >> 8) & 0xff, ((c) >> 16) & 0xff}
#endif
'''
# TODO, support arrays properly,
# these variables hardly change so hard code for now.
TARM_WORKAROUND = '''\t\t{
\t\t\t.solid = RGBA(0x9a0000ff),
\t\t\t.select = RGBA(0xbd1111ff),
\t\t\t.active = RGBA(0xf70a0aff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0xf74018ff),
\t\t\t.select = RGBA(0xf66913ff),
\t\t\t.active = RGBA(0xfa9900ff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x1e9109ff),
\t\t\t.select = RGBA(0x59b70bff),
\t\t\t.active = RGBA(0x83ef1dff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x0a3694ff),
\t\t\t.select = RGBA(0x3667dfff),
\t\t\t.active = RGBA(0x5ec1efff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0xa9294eff),
\t\t\t.select = RGBA(0xc1416aff),
\t\t\t.active = RGBA(0xf05d91ff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x430c78ff),
\t\t\t.select = RGBA(0x543aa3ff),
\t\t\t.active = RGBA(0x8764d5ff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x24785aff),
\t\t\t.select = RGBA(0x3c9579ff),
\t\t\t.active = RGBA(0x6fb6abff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x4b707cff),
\t\t\t.select = RGBA(0x6a8691ff),
\t\t\t.active = RGBA(0x9bc2cdff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0xf4c90cff),
\t\t\t.select = RGBA(0xeec236ff),
\t\t\t.active = RGBA(0xf3ff00ff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x1e2024ff),
\t\t\t.select = RGBA(0x484c56ff),
\t\t\t.active = RGBA(0xffffffff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x6f2f6aff),
\t\t\t.select = RGBA(0x9845beff),
\t\t\t.active = RGBA(0xd330d6ff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x6c8e22ff),
\t\t\t.select = RGBA(0x7fb022ff),
\t\t\t.active = RGBA(0xbbef5bff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x8d8d8dff),
\t\t\t.select = RGBA(0xb0b0b0ff),
\t\t\t.active = RGBA(0xdededeff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x834326ff),
\t\t\t.select = RGBA(0x8b5811ff),
\t\t\t.active = RGBA(0xbd6a11ff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x08310eff),
\t\t\t.select = RGBA(0x1c430bff),
\t\t\t.active = RGBA(0x34622bff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x000000ff),
\t\t\t.select = RGBA(0x000000ff),
\t\t\t.active = RGBA(0x000000ff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x000000ff),
\t\t\t.select = RGBA(0x000000ff),
\t\t\t.active = RGBA(0x000000ff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x000000ff),
\t\t\t.select = RGBA(0x000000ff),
\t\t\t.active = RGBA(0x000000ff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x000000ff),
\t\t\t.select = RGBA(0x000000ff),
\t\t\t.active = RGBA(0x000000ff),
\t\t},
\t\t{
\t\t\t.solid = RGBA(0x000000ff),
\t\t\t.select = RGBA(0x000000ff),
\t\t\t.active = RGBA(0x000000ff),
\t\t},
'''
def round_float_32(f):
from struct import pack, unpack
return unpack("f", pack("f", f))[0]
def repr_f32(f):
f_round = round_float_32(f)
f_str = repr(f)
f_str_frac = f_str.partition(".")[2]
if not f_str_frac:
return f_str
for i in range(1, len(f_str_frac)):
f_test = round(f, i)
f_test_round = round_float_32(f_test)
if f_test_round == f_round:
return "%.*f" % (i, f_test)
return f_str
# Avoid maintaining multiple blendfile modules
import os
import sys
sys.path.append(os.path.join(
os.path.dirname(__file__),
"..", "..", "..",
"release", "scripts", "addons", "io_blend_utils", "blend",
))
source_dst = os.path.join(
os.path.dirname(__file__),
"..", "..", "..",
"release", "datafiles", "userdef", "userdef_default_theme.c"
)
del sys
def theme_data(userpref_filename):
import blendfile
blend = blendfile.open_blend(userpref_filename)
u = next((c for c in blend.blocks if c.code == b'USER'), None)
# theme_type = b.sdna_index_from_id[b'bTheme']
t = u.get_pointer((b'themes', b'first'))
t.refine_type(b'bTheme')
return blend, t
def is_ignore_dna_name(name):
if name.startswith(b'_') or name == b'pad':
return True
elif name.startswith(b'pad') and name[3:].isdigit():
return True
else:
return False
def write_member(fw, indent, b, theme, ls):
path_old = ()
for key, value in ls:
key = key if type(key) is tuple else (key,)
path_new = key[:-1]
if tuple(path_new) != tuple(path_old):
if path_old:
p = len(path_old) - 1
while p >= 0 and (p >= len(path_new) or path_new[p] != path_old[p]):
indent = p + 1
fw('\t' * indent)
fw('},\n')
p -= 1
del p
p = 0
for p in range(min(len(path_old), len(path_new))):
if path_old[p] != key[p]:
break
else:
p = p + 1
for i, c in enumerate(path_new[p:]):
indent = p + i + 1
fw('\t' * indent)
attr = c.decode('ascii')
fw(f'.{attr} = {{\n')
# Evil, tarm array workaround.
if key[0] == b'tarm':
if path_old[0] != b'tarm':
fw(TARM_WORKAROUND)
path_old = path_new
continue
if not is_ignore_dna_name(key[-1]):
indent = '\t' * (len(path_new) + 1)
attr = key[-1].decode('ascii')
if isinstance(value, float):
if value != 0.0:
value_repr = repr_f32(value)
fw(f'{indent}.{attr} = {value_repr}f,\n')
elif isinstance(value, int):
if value != 0:
fw(f'{indent}.{attr} = {value},\n')
elif isinstance(value, bytes):
if set(value) != {0}:
if len(value) == 3:
value_repr = "".join(f'{ub:02x}' for ub in value)
fw(f'{indent}.{attr} = RGB(0x{value_repr}),\n')
elif len(value) == 4:
value_repr = "".join(f'{ub:02x}' for ub in value)
fw(f'{indent}.{attr} = RGBA(0x{value_repr}),\n')
else:
value = value.rstrip(b'\x00')
is_ascii = True
for ub in value:
if not (ub >= 32 and ub < 127):
is_ascii = False
break
if is_ascii:
value_repr = value.decode('ascii')
fw(f'{indent}.{attr} = "{value_repr}",\n')
else:
fw(f'{indent}.{attr} = {{{value_repr}}},\n')
else:
fw(f'{indent}.{attr} = {value},\n')
path_old = path_new
def convert_data(blend, theme, f):
fw = f.write
fw(C_SOURCE_HEADER)
fw('const bTheme U_theme_default = {\n')
ls = list(theme.items_recursive_iter(use_nil=False))
write_member(fw, 1, blend, theme, ls)
fw('};\n')
def main():
import sys
blend, theme = theme_data(sys.argv[-1])
with open(source_dst, 'w', encoding='utf-8') as fh:
convert_data(blend, theme, fh)
if __name__ == "__main__":
main()