Import_3ds: Improved distance cue node setup
[blender-addons.git] / system_demo_mode / config.py
blobbbd67359e365a6cc0c74330d6160885134104a4d
1 # SPDX-FileCopyrightText: 2011-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 __all__ = (
6 "as_string",
9 import os
12 # -----------------------------------------------------------------------------
13 # Generic Utilities
15 def round_float_32(f):
16 from struct import pack, unpack
17 return unpack("f", pack("f", f))[0]
20 def repr_f32(f):
21 f_round = round_float_32(f)
22 f_str = repr(f)
23 f_str_frac = f_str.partition(".")[2]
24 if not f_str_frac:
25 return f_str
26 for i in range(1, len(f_str_frac)):
27 f_test = round(f, i)
28 f_test_round = round_float_32(f_test)
29 if f_test_round == f_round:
30 return "%.*f" % (i, f_test)
31 return f_str
34 def repr_pretty(v):
35 from pprint import pformat
36 # Float's are originally 32 bit, regular pretty print
37 # shows them with many decimal places (not so pretty).
38 if type(v) is float:
39 return repr_f32(v)
40 return pformat(v)
43 # -----------------------------------------------------------------------------
44 # Configuration Generation
46 def blend_list(path):
47 for dirpath, dirnames, filenames in os.walk(path):
48 # skip '.git'
49 dirnames[:] = [d for d in dirnames if not d.startswith(".")]
50 for filename in filenames:
51 if filename.lower().endswith(".blend"):
52 filepath = os.path.join(dirpath, filename)
53 yield filepath
56 def generate(dirpath, random_order, **kwargs):
57 files = list(blend_list(dirpath))
58 if random_order:
59 import random
60 random.shuffle(files)
61 else:
62 files.sort()
64 config = []
65 for f in files:
66 defaults = kwargs.copy()
67 defaults["file"] = f
68 config.append(defaults)
70 return config, dirpath
73 def as_string(dirpath, random_order, exit, **kwargs):
74 """ Config loader is in demo_mode.py
75 """
76 cfg, dirpath = generate(dirpath, random_order, **kwargs)
78 # hint for reader, can be used if files are not found.
79 cfg_str = [
80 "# generated file\n",
81 "\n",
82 "# edit the search path so other systems may find the files below\n",
83 "# based on name only if the absolute paths cannot be found\n",
84 "# Use '//' for current blend file path.\n",
85 "\n",
86 "search_path = %r\n" % dirpath,
87 "\n",
88 "exit = %r\n" % exit,
89 "\n",
92 # Works, but custom formatting looks better.
93 # `cfg_str.append("config = %s" % pprint.pformat(cfg, indent=4, width=120))`.
95 def dict_as_kw(d):
96 return "dict(%s)" % ", ".join(("%s=%s" % (k, repr_pretty(v))) for k, v in sorted(d.items()))
98 ident = " "
99 cfg_str.append("config = [\n")
100 for cfg_item in cfg:
101 cfg_str.append("%s%s,\n" % (ident, dict_as_kw(cfg_item)))
102 cfg_str.append("%s]\n\n" % ident)
104 return "".join(cfg_str), len(cfg), dirpath