Skip to content

Commit 6666aa3

Browse files
committed
fix: "blender" dev_env not working
sublimelsp/LSP-pyright#377 Signed-off-by: Jack Cherng <jfcherng@gmail.com>
1 parent e396e9a commit 6666aa3

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

plugin/dev_environment/impl/blender.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,28 @@ def handle_(self, *, settings: DottedDict) -> None:
1717
@classmethod
1818
def find_paths(cls, settings: DottedDict) -> list[str]:
1919
with tempfile.TemporaryDirectory() as tmpdir:
20-
filepath = Path(tmpdir) / "print_sys_path.py"
21-
filepath.write_text(
22-
R"""
23-
import sys
20+
dumped_result = Path(tmpdir) / "sys_path.json"
21+
dumper_path = Path(tmpdir) / "sys_path_dumper.py"
22+
dumper_path.write_text(
23+
Rf"""
24+
import bpy
2425
import json
25-
json.dump({"executable": sys.executable, "paths": sys.path}, sys.stdout)
26-
exit(0)
26+
import sys
27+
with open(R"{dumped_result}", "w", encoding="utf-8") as f:
28+
json.dump({{"executable": sys.executable, "paths": sys.path}}, f)
29+
bpy.ops.wm.quit_blender()
2730
""".strip(),
2831
encoding="utf-8",
2932
)
3033
args = (
3134
cls.get_dev_environment_subsetting(settings, "binary"),
3235
"--background",
3336
"--python",
34-
str(filepath),
37+
str(dumper_path),
3538
)
3639
result = run_shell_command(args, shell=False)
3740

38-
if not result or result[2] != 0:
39-
raise RuntimeError(f"Failed to run command: {args}")
40-
41-
# Blender prints a bunch of general information to stdout before printing the output of the python
42-
# script. We want to ignore that initial information. We do that by finding the start of the JSON
43-
# dict. This is a bit hacky and there must be a better way.
44-
if (index := result[0].find('\n{"')) == -1:
45-
raise RuntimeError("Unexpected output when calling blender")
41+
if not result or result[2] != 0:
42+
raise RuntimeError(f"Failed to run command: {args}")
4643

47-
try:
48-
return json.loads(result[0][index:])["paths"]
49-
except json.JSONDecodeError as e:
50-
raise RuntimeError(f"Failed to parse JSON: {e}")
44+
return json.loads(dumped_result.read_bytes())["paths"]

0 commit comments

Comments
 (0)