feat: Implement CLI command registration for bioxelnodes_import_worker and refactor io_worker for improved module handling

This commit is contained in:
2026-07-08 22:43:46 +08:00
parent 433cfa0180
commit 3756330ea3
3 changed files with 47 additions and 24 deletions

View File

@@ -9,6 +9,8 @@ from . import menus
auto_load.init() auto_load.init()
CLI_COMMAND_NAME = "bioxelnodes_import_worker"
def register(): def register():
pcoll = previews.new() pcoll = previews.new()
@@ -41,9 +43,11 @@ def register():
auto_load.register() auto_load.register()
menus.add() menus.add()
register_cli_commands()
def unregister(): def unregister():
unregister_cli_commands()
menus.remove() menus.remove()
auto_load.unregister() auto_load.unregister()
@@ -56,3 +60,21 @@ def unregister():
for pcoll in PREVIEW_COLLECTIONS.values(): for pcoll in PREVIEW_COLLECTIONS.values():
previews.remove(pcoll) previews.remove(pcoll)
PREVIEW_COLLECTIONS.clear() 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

View File

@@ -61,6 +61,14 @@ def remove_progress_bar_safe():
pass 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): def start_worker_process(owner, command: str, payload: dict):
job_dir = Path(tempfile.mkdtemp(prefix="bioxel_import_", dir=str(get_cache_dir()))) job_dir = Path(tempfile.mkdtemp(prefix="bioxel_import_", dir=str(get_cache_dir())))
config_path = job_dir / "config.json" config_path = job_dir / "config.json"
@@ -69,9 +77,11 @@ def start_worker_process(owner, command: str, payload: dict):
cancel_path = job_dir / "cancel" cancel_path = job_dir / "cancel"
log_path = job_dir / "worker.log" log_path = job_dir / "worker.log"
addon_name = get_addon_module_name()
config = { config = {
**payload, **payload,
"command": command, "command": command,
"addon_name": addon_name,
"progress_path": str(progress_path), "progress_path": str(progress_path),
"result_path": str(result_path), "result_path": str(result_path),
"cancel_path": str(cancel_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(config_path, config)
write_worker_json(progress_path, {"factor": 0.0, "text": "Starting..."}) 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 = [ cmd = [
bpy.app.binary_path, bpy.app.binary_path,
"--background", "--background",
"--factory-startup", "--addons",
"--python", addon_name,
str(worker_path), "--command",
"--", "bioxelnodes_import_worker",
str(config_path), str(config_path),
] ]

View File

@@ -1,23 +1,13 @@
import json import json
import sys
import traceback import traceback
from pathlib import Path from pathlib import Path
import numpy as np import numpy as np
import transforms3d import transforms3d
if __package__: from ..bioxel.layer import Layer
from ..bioxel.layer import Layer from ..bioxel.parse import parse_volumetric_data
from ..bioxel.parse import parse_volumetric_data from ..layer import save_layers_to_cache
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
def get_layer_shape(bioxel_size: float, orig_shape: tuple, orig_spacing: tuple): 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]} return {"cache_infos": cache_infos, "added_ids": [item["id"] for item in cache_infos]}
def main(): def main(args):
config_path = Path(sys.argv[-1]) if not args:
print("Usage: blender --command bioxelnodes_import_worker <config_path>")
return 2
config_path = Path(args[0])
config = json.loads(config_path.read_text(encoding="utf-8")) config = json.loads(config_path.read_text(encoding="utf-8"))
progress_path = Path(config["progress_path"]) progress_path = Path(config["progress_path"])
result_path = Path(config["result_path"]) result_path = Path(config["result_path"])
@@ -257,7 +251,4 @@ def main():
"traceback": traceback.format_exc(), "traceback": traceback.format_exc(),
}, },
) )
return 0
if __name__ == "__main__":
main()