Skip to content

Commit

Permalink
fix: "blender" dev_env not working (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcherng authored Dec 21, 2024
1 parent 32ec33f commit 29327f1
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions plugin/dev_environment/impl/blender.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,28 @@ def handle_(self, *, settings: DottedDict) -> None:
@classmethod
def find_paths(cls, settings: DottedDict) -> list[str]:
with tempfile.TemporaryDirectory() as tmpdir:
filepath = Path(tmpdir) / "print_sys_path.py"
filepath.write_text(
R"""
import sys
dumped_result = Path(tmpdir) / "sys_path.json"
dumper_path = Path(tmpdir) / "sys_path_dumper.py"
dumper_path.write_text(
Rf"""
import bpy
import json
json.dump({"executable": sys.executable, "paths": sys.path}, sys.stdout)
exit(0)
import sys
with open(R"{dumped_result}", "w", encoding="utf-8") as f:
json.dump({{"executable": sys.executable, "paths": sys.path}}, f)
bpy.ops.wm.quit_blender()
""".strip(),
encoding="utf-8",
)
args = (
cls.get_dev_environment_subsetting(settings, "binary"),
"--background",
"--python",
str(filepath),
str(dumper_path),
)
result = run_shell_command(args, shell=False)

if not result or result[2] != 0:
raise RuntimeError(f"Failed to run command: {args}")

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

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

0 comments on commit 29327f1

Please sign in to comment.