-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblender
More file actions
executable file
·333 lines (273 loc) · 10.6 KB
/
Copy pathblender
File metadata and controls
executable file
·333 lines (273 loc) · 10.6 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python3
"""
blender - Blender command helpers.
Subcommands:
launch [BLENDER_ARGS...] launch Blender; all args are forwarded to
the Blender executable
python [PYTHON_ARGS...] run Blender's bundled Python interpreter with
Blender's addon and module paths pre-set
config [--json] show effective blender configuration
Configuration: set [blender] blender_path and config_path in .bcconfig.
blender_path should point to Blender.app/Contents (macOS).
Run `blender <subcommand> -h` for per-command help.
"""
import argparse
import json
import logging
import os
import subprocess
import sys
from lib.bc_config import load as load_bcconfig
LOG = logging.getLogger(__name__)
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.WARNING)
# ---------------------------------------------------------------------------
# Config schema
# ---------------------------------------------------------------------------
BCCONFIG_SCHEMA = {
"blender": [
("blender_path", "",
"Path to Blender.app/Contents (macOS) or Blender install root"),
("config_path", "",
"Path to Blender config dir (e.g. ~/Library/Application Support/Blender/4.2)"),
("gameplay_bin", "",
"Optional extra tools directory added to PATH before launching Blender"),
],
}
def _schema_for_metadata():
sections = {}
for section, entries in BCCONFIG_SCHEMA.items():
sections[section] = [
{"key": key, "default": default, "description": desc}
for key, default, desc in entries
]
return sections
def bc_metadata():
return {
"schema_version": 1,
"name": "blender",
"summary": "Blender launch and Python helpers",
"command_style": "subcommands",
"config_sections": _schema_for_metadata(),
"subcommands": [
{
"name": "launch",
"summary": "Launch Blender (args forwarded to the Blender executable)",
"safety": "write",
"args": [
{"name": "args", "label": "Blender arguments",
"kind": "path-list", "required": False},
],
"options": [],
"artifacts": [],
},
{
"name": "python",
"summary": "Run Blender's bundled Python interpreter",
"safety": "write",
"args": [
{"name": "args", "label": "Python arguments",
"kind": "path-list", "required": False},
],
"options": [],
"artifacts": [],
},
{
"name": "config",
"summary": "Show effective blender configuration",
"safety": "read",
"args": [],
"options": [
{"name": "json", "flag": "--json", "kind": "boolean",
"label": "Emit as JSON"},
],
"artifacts": [
{"kind": "json", "source": "stdout", "when_option": "json"},
],
},
],
}
# ---------------------------------------------------------------------------
# Config resolution
# ---------------------------------------------------------------------------
def _effective_config():
cfg = {
key: default
for _section, entries in BCCONFIG_SCHEMA.items()
for key, default, _desc in entries
}
cfg["_sources"] = {k: "default" for k in cfg}
bcfg = load_bcconfig()
if bcfg.has_section("blender"):
for key, value in bcfg.items("blender"):
if value:
cfg[key] = value
cfg["_sources"][key] = ".bcconfig"
# Expand user on path values
for key in ("blender_path", "config_path", "gameplay_bin"):
if cfg[key]:
cfg[key] = os.path.expanduser(cfg[key])
return cfg
def _require_blender_path(cfg):
path = cfg.get("blender_path", "").strip()
if not path:
print("error: blender_path is not configured.", file=sys.stderr)
print("Add [blender] blender_path = /path/to/Blender.app/Contents to .bcconfig",
file=sys.stderr)
sys.exit(2)
if not os.path.isdir(path):
print(f"error: blender_path does not exist: {path}", file=sys.stderr)
sys.exit(2)
return path
def _blender_exe(blender_path):
"""Return path to the Blender executable."""
# macOS: Blender.app/Contents/MacOS/blender
macos = os.path.join(blender_path, "MacOS", "blender")
if os.path.isfile(macos):
return macos
# Linux / other: blender in the root
root_exe = os.path.join(blender_path, "blender")
if os.path.isfile(root_exe):
return root_exe
return None
def _blender_version(blender_path):
"""Detect Blender version from Resources/<version>/ directory structure."""
resources = os.path.join(blender_path, "Resources")
if not os.path.isdir(resources):
return None
versions = sorted(
(d for d in os.listdir(resources)
if os.path.isdir(os.path.join(resources, d))
and d.replace(".", "").isdigit()),
reverse=True,
)
return versions[0] if versions else None
def _blender_python_exe(blender_path, version):
"""Locate Blender's bundled Python executable."""
py_dir = os.path.join(blender_path, "Resources", version, "python")
if not os.path.isdir(py_dir):
return None
bin_dir = os.path.join(py_dir, "bin")
if not os.path.isdir(bin_dir):
return None
for entry in sorted(os.listdir(bin_dir), reverse=True):
if entry.startswith("python") and not entry.endswith(".zip"):
candidate = os.path.join(bin_dir, entry)
if os.path.isfile(candidate) and os.access(candidate, os.X_OK):
return candidate
return None
def _blender_pythonpath(blender_path, config_path, version):
"""Build PYTHONPATH for Blender's addon / module directories."""
bl_scripts = os.path.join(blender_path, "Resources", version, "scripts")
parts = []
if config_path:
for sub in (
"scripts/addons_contrib",
"scripts/addons",
"scripts/modules",
"scripts/startup",
"scripts/addons/modules",
):
parts.append(os.path.join(config_path, sub))
for sub in (
"addons_contrib",
"addons",
"modules",
"startup",
"addons/modules",
):
parts.append(os.path.join(bl_scripts, sub))
return ":".join(parts)
# ---------------------------------------------------------------------------
# Subcommand handlers
# ---------------------------------------------------------------------------
def cmd_launch(args):
cfg = _effective_config()
blender_path = _require_blender_path(cfg)
exe = _blender_exe(blender_path)
if not exe:
print(f"error: Blender executable not found under: {blender_path}", file=sys.stderr)
return 2
env = os.environ.copy()
gameplay_bin = cfg.get("gameplay_bin", "").strip()
if gameplay_bin and os.path.isdir(gameplay_bin):
env["PATH"] = env.get("PATH", "") + os.pathsep + gameplay_bin
argv = [exe] + args.args
LOG.info("$ %s", " ".join(argv))
os.execve(exe, argv, env)
def cmd_python(args):
cfg = _effective_config()
blender_path = _require_blender_path(cfg)
config_path = cfg.get("config_path", "").strip()
version = _blender_version(blender_path)
if not version:
print(f"error: could not detect Blender version under: {blender_path}/Resources",
file=sys.stderr)
return 2
py_exe = _blender_python_exe(blender_path, version)
if not py_exe:
print(f"error: Blender Python not found (version {version})", file=sys.stderr)
return 2
env = os.environ.copy()
env["PYTHONPATH"] = _blender_pythonpath(blender_path, config_path, version)
argv = [py_exe] + args.args
LOG.info("$ %s", " ".join(argv))
os.execve(py_exe, argv, env)
def cmd_config(args):
cfg = _effective_config()
blender_path = cfg.get("blender_path", "")
version = _blender_version(blender_path) if blender_path else None
exe = _blender_exe(blender_path) if blender_path else None
py_exe = _blender_python_exe(blender_path, version) if (blender_path and version) else None
view = {k: v for k, v in cfg.items() if k != "_sources"}
view["version_detected"] = version or ""
view["blender_exe"] = exe or ""
view["python_exe"] = py_exe or ""
if args.json:
print(json.dumps({"config": view, "sources": cfg.get("_sources", {})},
indent=2, sort_keys=True))
return 0
sources = cfg.get("_sources", {})
print("Effective [blender] configuration:")
print()
width = max((len(k) for k in view), default=10)
for key in sorted(view):
src = sources.get(key, "")
value = view[key] or "(unset)"
suffix = f" [{src}]" if src else ""
print(f" {key:<{width}} {value}{suffix}")
return 0
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
def _build_parser():
parser = argparse.ArgumentParser(
prog="blender",
description="Blender launch and Python helpers.",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
subs = parser.add_subparsers(dest="subcommand", metavar="SUBCOMMAND")
p = subs.add_parser("launch", help="launch Blender (all args forwarded to blender)")
p.add_argument("args", nargs=argparse.REMAINDER, help="Blender arguments")
p.set_defaults(handler=cmd_launch)
p = subs.add_parser("python",
help="run Blender's bundled Python with Blender paths pre-set")
p.add_argument("args", nargs=argparse.REMAINDER, help="Python arguments")
p.set_defaults(handler=cmd_python)
p = subs.add_parser("config", help="show effective blender configuration")
p.add_argument("--json", action="store_true", help="emit as JSON")
p.set_defaults(handler=cmd_config)
return parser
def main(argv=None):
argv = list(sys.argv[1:] if argv is None else argv)
if argv == ["--bc-metadata"]:
print(json.dumps(bc_metadata(), indent=2, sort_keys=True))
return 0
parser = _build_parser()
args = parser.parse_args(argv)
handler = getattr(args, "handler", None)
if handler is None:
parser.print_help()
return 1
return handler(args) or 0
if __name__ == "__main__":
sys.exit(main())