From 3756330ea3fa4e8cf8479d2d26789c65992d21f9 Mon Sep 17 00:00:00 2001 From: nan Date: Wed, 8 Jul 2026 22:43:46 +0800 Subject: [PATCH] feat: Implement CLI command registration for bioxelnodes_import_worker and refactor io_worker for improved module handling --- src/bioxelnodes/__init__.py | 22 +++++++++++++++++++ src/bioxelnodes/operators/io.py | 20 +++++++++++++----- src/bioxelnodes/operators/io_worker.py | 29 +++++++++----------------- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/bioxelnodes/__init__.py b/src/bioxelnodes/__init__.py index d7046f0..01735ff 100644 --- a/src/bioxelnodes/__init__.py +++ b/src/bioxelnodes/__init__.py @@ -9,6 +9,8 @@ from . import menus auto_load.init() +CLI_COMMAND_NAME = "bioxelnodes_import_worker" + def register(): pcoll = previews.new() @@ -41,9 +43,11 @@ def register(): auto_load.register() menus.add() + register_cli_commands() def unregister(): + unregister_cli_commands() menus.remove() auto_load.unregister() @@ -56,3 +60,21 @@ def unregister(): for pcoll in PREVIEW_COLLECTIONS.values(): previews.remove(pcoll) PREVIEW_COLLECTIONS.clear() + + +def import_worker_cli(args): + from .operators import io_worker + + return io_worker.main(args) + + +def register_cli_commands(): + unregister_cli_commands() + bpy.utils.register_cli_command(CLI_COMMAND_NAME, import_worker_cli) + + +def unregister_cli_commands(): + try: + bpy.utils.unregister_cli_command(CLI_COMMAND_NAME) + except Exception: + pass diff --git a/src/bioxelnodes/operators/io.py b/src/bioxelnodes/operators/io.py index b930507..34099fa 100644 --- a/src/bioxelnodes/operators/io.py +++ b/src/bioxelnodes/operators/io.py @@ -61,6 +61,14 @@ def remove_progress_bar_safe(): pass +def get_addon_module_name(): + package_name = __package__ or "bioxelnodes.operators" + suffix = ".operators" + if package_name.endswith(suffix): + return package_name[: -len(suffix)] + return package_name + + def start_worker_process(owner, command: str, payload: dict): job_dir = Path(tempfile.mkdtemp(prefix="bioxel_import_", dir=str(get_cache_dir()))) config_path = job_dir / "config.json" @@ -69,9 +77,11 @@ def start_worker_process(owner, command: str, payload: dict): cancel_path = job_dir / "cancel" log_path = job_dir / "worker.log" + addon_name = get_addon_module_name() config = { **payload, "command": command, + "addon_name": addon_name, "progress_path": str(progress_path), "result_path": str(result_path), "cancel_path": str(cancel_path), @@ -79,14 +89,14 @@ def start_worker_process(owner, command: str, payload: dict): write_worker_json(config_path, config) write_worker_json(progress_path, {"factor": 0.0, "text": "Starting..."}) - worker_path = Path(__file__).with_name("io_worker.py") + print(f"Starting Bioxel worker with addon module: {addon_name}") cmd = [ bpy.app.binary_path, "--background", - "--factory-startup", - "--python", - str(worker_path), - "--", + "--addons", + addon_name, + "--command", + "bioxelnodes_import_worker", str(config_path), ] diff --git a/src/bioxelnodes/operators/io_worker.py b/src/bioxelnodes/operators/io_worker.py index 31e090d..bb84045 100644 --- a/src/bioxelnodes/operators/io_worker.py +++ b/src/bioxelnodes/operators/io_worker.py @@ -1,23 +1,13 @@ import json -import sys import traceback from pathlib import Path import numpy as np import transforms3d -if __package__: - from ..bioxel.layer import Layer - from ..bioxel.parse import parse_volumetric_data - from ..layer import save_layers_to_cache -else: - PACKAGE_PARENT = Path(__file__).resolve().parents[2] - if str(PACKAGE_PARENT) not in sys.path: - sys.path.insert(0, str(PACKAGE_PARENT)) - - from bioxelnodes.bioxel.layer import Layer - from bioxelnodes.bioxel.parse import parse_volumetric_data - from bioxelnodes.layer import save_layers_to_cache +from ..bioxel.layer import Layer +from ..bioxel.parse import parse_volumetric_data +from ..layer import save_layers_to_cache def get_layer_shape(bioxel_size: float, orig_shape: tuple, orig_spacing: tuple): @@ -229,8 +219,12 @@ def build_layers(config, progress_path: Path, cancel_path: Path): return {"cache_infos": cache_infos, "added_ids": [item["id"] for item in cache_infos]} -def main(): - config_path = Path(sys.argv[-1]) +def main(args): + if not args: + print("Usage: blender --command bioxelnodes_import_worker ") + return 2 + + config_path = Path(args[0]) config = json.loads(config_path.read_text(encoding="utf-8")) progress_path = Path(config["progress_path"]) result_path = Path(config["result_path"]) @@ -257,7 +251,4 @@ def main(): "traceback": traceback.format_exc(), }, ) - - -if __name__ == "__main__": - main() + return 0