Back Blender target with cultcache-py

This commit is contained in:
Meta Vens 2026-06-14 12:36:09 +01:00
commit c22fa31aaa
6 changed files with 170 additions and 35 deletions

View file

@ -29,7 +29,8 @@ broker.
## Surfaces
- `surfaces/unity/Packages/com.gamecult.brokkr`: Unity editor package scaffold.
- `surfaces/blender/brokkr_bridge`: Blender add-on target and debug mirror.
- `surfaces/blender/brokkr_bridge`: Blender add-on target backed by
`cultcache-py`, with optional debug export/import probes.
- `brokkr-daemon`: Rust daemon skeleton that emits Brokkr's provider
advertisement and command policy.

View file

@ -18,6 +18,11 @@ surface projections. Verse-side clients write typed command-intent documents
into the same mirror; Unity watches those intents, mutates the editor, and
publishes receipts plus an updated snapshot.
The Blender add-on uses `cultcache-py` as its local mirror store. Blender writes
typed host snapshots and command receipts through registered CultCache document
definitions, drains command intents from the same store, and only uses loose JSON
files as optional debug/export probes.
## Invariants
- Editor hosts keep editor truth. Brokkr never becomes the canonical scene,
@ -74,6 +79,7 @@ Derived State:
- Unity package settings are adapter configuration, not Verse authority.
- Blender add-on preferences are adapter configuration, not Verse authority.
- Blender JSON debug exports are inspection/import probes, not mirror authority.
- Editor selection and scene summaries are observations until command receipts
confirm an accepted mutation.

View file

@ -12,7 +12,9 @@ import bpy
from .blender_target import (
DEFAULT_BROKER_URI,
DEFAULT_MIRROR_ROOT,
DEFAULT_CULTCACHE_PATH,
DEFAULT_CULTCACHE_PY_SRC,
DEFAULT_DEBUG_MIRROR_ROOT,
PROVIDER_ID,
TOOL_KIND,
BrokkrBlenderTarget,
@ -37,11 +39,25 @@ class BrokkrPreferences(bpy.types.AddonPreferences):
description="CultMesh broker URI for Brokkr",
)
mirror_root: bpy.props.StringProperty(
name="Debug Mirror Root",
cultcache_path: bpy.props.StringProperty(
name="CultCache Store",
subtype="FILE_PATH",
default=DEFAULT_CULTCACHE_PATH,
description="CultCache store used for Blender mirror documents",
)
cultcache_py_src: bpy.props.StringProperty(
name="CultCache Python Source",
subtype="DIR_PATH",
default=DEFAULT_MIRROR_ROOT,
description="Debug export root used until Blender has a Python CultMesh transport",
default=DEFAULT_CULTCACHE_PY_SRC,
description="Path to cultcache-py/src when cultcache-py is not installed in Blender",
)
debug_mirror_root: bpy.props.StringProperty(
name="Debug Export Root",
subtype="DIR_PATH",
default=DEFAULT_DEBUG_MIRROR_ROOT,
description="Optional JSON debug export/import root; CultCache remains the mirror owner",
)
auto_capture: bpy.props.BoolProperty(
@ -53,7 +69,9 @@ class BrokkrPreferences(bpy.types.AddonPreferences):
def draw(self, context):
layout = self.layout
layout.prop(self, "broker_uri")
layout.prop(self, "mirror_root")
layout.prop(self, "cultcache_path")
layout.prop(self, "cultcache_py_src")
layout.prop(self, "debug_mirror_root")
layout.prop(self, "auto_capture")
@ -72,7 +90,7 @@ class BROKKR_PT_status(bpy.types.Panel):
layout.label(text=f"Provider: {PROVIDER_ID}")
layout.label(text=f"Tool: {TOOL_KIND}")
layout.label(text=f"Broker: {prefs.broker_uri}")
layout.label(text=f"Mirror: {adapter.resolve_mirror_root(prefs.mirror_root)}")
layout.label(text=f"Store: {adapter.resolve_cache_path(prefs.cultcache_path)}")
row = layout.row(align=True)
row.operator("brokkr.capture_snapshot", icon="FILE_REFRESH")
@ -97,7 +115,12 @@ class BROKKR_OT_capture_snapshot(bpy.types.Operator):
def execute(self, context):
prefs = context.preferences.addons[__name__].preferences
snapshot = target().publish_snapshot(context, prefs.mirror_root)
snapshot = target().publish_snapshot(
context,
prefs.cultcache_path,
prefs.cultcache_py_src,
prefs.debug_mirror_root,
)
self.report({"INFO"}, f"Brokkr mirrored Blender snapshot: {snapshot['observedAt']}")
return {"FINISHED"}
@ -109,7 +132,12 @@ class BROKKR_OT_drain_commands(bpy.types.Operator):
def execute(self, context):
prefs = context.preferences.addons[__name__].preferences
receipts = target().drain_commands(context, prefs.mirror_root)
receipts = target().drain_commands(
context,
prefs.cultcache_path,
prefs.cultcache_py_src,
prefs.debug_mirror_root,
)
self.report({"INFO"}, f"Brokkr processed {len(receipts)} Blender command(s)")
return {"FINISHED"}
@ -119,7 +147,12 @@ def _auto_capture(scene, depsgraph):
prefs = context.preferences.addons.get(__name__)
if not prefs or not prefs.preferences.auto_capture:
return
target().publish_snapshot(context, prefs.preferences.mirror_root)
target().publish_snapshot(
context,
prefs.preferences.cultcache_path,
prefs.preferences.cultcache_py_src,
prefs.preferences.debug_mirror_root,
)
classes = (

View file

@ -3,6 +3,7 @@ from __future__ import annotations
import json
import math
import os
import sys
import uuid
from datetime import datetime, timezone
from pathlib import Path
@ -11,7 +12,9 @@ from typing import Any, Iterable
PROVIDER_ID = "brokkr.blender_editor"
TOOL_KIND = "blender-editor"
DEFAULT_BROKER_URI = "cultmesh://brokkr"
DEFAULT_MIRROR_ROOT = "//.brokkr/blender-editor"
DEFAULT_CULTCACHE_PATH = "//.brokkr/blender-editor.cultcache.jsonl"
DEFAULT_CULTCACHE_PY_SRC = "E:/Projects/cultcache-py/src"
DEFAULT_DEBUG_MIRROR_ROOT = "//.brokkr/blender-editor-debug"
HOST_SNAPSHOT_SCHEMA = "brokkr.blender.host_snapshot.v0"
COMMAND_INTENT_SCHEMA = "brokkr.blender.command_intent.v0"
@ -44,32 +47,62 @@ class BrokkrBlenderTarget:
self.last_snapshot: dict[str, Any] | None = None
self.last_receipt: dict[str, Any] | None = None
def resolve_mirror_root(self, mirror_root: str) -> str:
return self.bpy.path.abspath(mirror_root or DEFAULT_MIRROR_ROOT)
def resolve_cache_path(self, cache_path: str) -> str:
return self.bpy.path.abspath(cache_path or DEFAULT_CULTCACHE_PATH)
def publish_snapshot(self, context: Any, mirror_root: str) -> dict[str, Any]:
def resolve_debug_mirror_root(self, debug_mirror_root: str) -> str:
return self.bpy.path.abspath(debug_mirror_root or DEFAULT_DEBUG_MIRROR_ROOT)
def publish_snapshot(
self,
context: Any,
cache_path: str,
cultcache_py_src: str,
debug_mirror_root: str = "",
) -> dict[str, Any]:
snapshot = self.capture_snapshot(context)
self._write_debug_document(mirror_root, "blender/host/current.json", snapshot)
cache, documents = self._open_cache(cache_path, cultcache_py_src)
cache.put(documents["host_snapshot"], "blender/host/current", snapshot)
if debug_mirror_root:
self._write_debug_document(debug_mirror_root, "blender/host/current.json", snapshot)
self.last_snapshot = snapshot
return snapshot
def drain_commands(self, context: Any, mirror_root: str) -> list[dict[str, Any]]:
commands_root = Path(self.resolve_mirror_root(mirror_root)) / "blender" / "commands"
def drain_commands(
self,
context: Any,
cache_path: str,
cultcache_py_src: str,
debug_mirror_root: str = "",
) -> list[dict[str, Any]]:
cache, documents = self._open_cache(cache_path, cultcache_py_src)
if debug_mirror_root:
self._import_debug_command_files(cache, documents["command_intent"], debug_mirror_root)
snapshot = cache.snapshot()
commands = snapshot.get(COMMAND_INTENT_SCHEMA, {})
receipts: list[dict[str, Any]] = []
for command_path in sorted(commands_root.glob("*.json")):
with command_path.open("r", encoding="utf-8") as handle:
command = json.load(handle)
for command_key, command in sorted(commands.items()):
receipt = self.execute_command(context, command)
self.publish_receipt(mirror_root, receipt)
command_path.unlink(missing_ok=True)
self.publish_receipt(cache_path, cultcache_py_src, receipt, debug_mirror_root)
cache.delete(documents["command_intent"], command_key)
receipts.append(receipt)
return receipts
def publish_receipt(self, mirror_root: str, receipt: dict[str, Any]) -> None:
def publish_receipt(
self,
cache_path: str,
cultcache_py_src: str,
receipt: dict[str, Any],
debug_mirror_root: str = "",
) -> None:
command_id = receipt.get("commandId") or uuid.uuid4().hex
self._write_debug_document(mirror_root, f"blender/receipts/{command_id}.json", receipt)
cache, documents = self._open_cache(cache_path, cultcache_py_src)
cache.put(documents["command_receipt"], f"blender/receipts/{command_id}", receipt)
if debug_mirror_root:
self._write_debug_document(debug_mirror_root, f"blender/receipts/{command_id}.json", receipt)
self.last_receipt = receipt
def capture_snapshot(self, context: Any) -> dict[str, Any]:
@ -267,13 +300,57 @@ class BrokkrBlenderTarget:
"observedAt": _now(),
}
def _write_debug_document(self, mirror_root: str, relative_path: str, document: dict[str, Any]) -> None:
path = Path(self.resolve_mirror_root(mirror_root)) / relative_path
def _open_cache(self, cache_path: str, cultcache_py_src: str) -> tuple[Any, dict[str, Any]]:
cultcache = _load_cultcache(cultcache_py_src)
documents = {
"host_snapshot": cultcache.define_document_type(HOST_SNAPSHOT_SCHEMA),
"command_intent": cultcache.define_document_type(COMMAND_INTENT_SCHEMA),
"command_receipt": cultcache.define_document_type(COMMAND_RECEIPT_SCHEMA),
}
path = self.resolve_cache_path(cache_path)
cache = (
cultcache.CultCache.builder()
.register_registry(tuple(documents.values()))
.add_generic_store(cultcache.JsonLinesBackingStore(path))
.build()
)
cache.pull_all_backing_stores()
return cache, documents
def _import_debug_command_files(self, cache: Any, command_document: Any, debug_mirror_root: str) -> None:
commands_root = Path(self.resolve_debug_mirror_root(debug_mirror_root)) / "blender" / "commands"
for command_path in sorted(commands_root.glob("*.json")):
with command_path.open("r", encoding="utf-8") as handle:
command = json.load(handle)
command_id = command.get("commandId") or command_path.stem
command["schema"] = command.get("schema") or COMMAND_INTENT_SCHEMA
command["commandId"] = command_id
cache.put(command_document, f"blender/commands/{command_id}", command)
command_path.unlink(missing_ok=True)
def _write_debug_document(self, debug_mirror_root: str, relative_path: str, document: dict[str, Any]) -> None:
path = Path(self.resolve_debug_mirror_root(debug_mirror_root)) / relative_path
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8") as handle:
json.dump(document, handle, indent=2, sort_keys=True)
def _load_cultcache(cultcache_py_src: str) -> Any:
source = Path(cultcache_py_src or DEFAULT_CULTCACHE_PY_SRC)
if source.exists():
source_text = str(source)
if source_text not in sys.path:
sys.path.insert(0, source_text)
try:
import cultcache_py # type: ignore
except ModuleNotFoundError as exc:
raise RuntimeError(
"Brokkr Blender target requires cultcache-py. Set CultCache Python Source "
"to the cultcache-py/src directory or install cultcache-py into Blender's Python."
) from exc
return cultcache_py
def _now() -> str:
return datetime.now(timezone.utc).isoformat()

View file

@ -7,22 +7,34 @@ The add-on adds a Brokkr panel to the 3D View sidebar and exposes a Blender
target surface for host snapshots, admitted command intents, and command
receipts.
Until a Python CultMesh/CultCache binding is available inside Blender, the
adapter writes debug mirror documents under `.brokkr/blender-editor`. That export
is inspection glue, not durable state authority. The target module owns the
document shape so the debug writer can be replaced by a real CultMesh writer
without changing Blender command ownership.
The adapter writes its mirror documents through `cultcache-py`. If `cultcache-py`
is not installed into Blender's Python, point `CultCache Python Source` at:
```text
E:/Projects/cultcache-py/src
```
The default store is:
```text
.brokkr/blender-editor.cultcache.jsonl
```
`Debug Export Root` may still emit readable JSON probe files under
`.brokkr/blender-editor-debug`, but those files are debug/import glue. CultCache
owns the mirror state.
## Local Smoke
1. Install and enable `brokkr_bridge`.
2. Open `View3D > Sidebar > Brokkr`.
3. Click `Capture Snapshot`.
4. Inspect `.brokkr/blender-editor/blender/host/current.json`.
4. Inspect `.brokkr/blender-editor.cultcache.jsonl`.
5. Drop a command intent JSON file into
`.brokkr/blender-editor/blender/commands/{commandId}.json`.
`.brokkr/blender-editor-debug/blender/commands/{commandId}.json`.
6. Click `Drain Commands`.
7. Inspect `.brokkr/blender-editor/blender/receipts/{commandId}.json`.
7. Inspect `.brokkr/blender-editor.cultcache.jsonl` or the optional debug export
at `.brokkr/blender-editor-debug/blender/receipts/{commandId}.json`.
Admitted command actions:

View file

@ -77,6 +77,12 @@ try {
}
& $pythonExe -m py_compile surfaces\blender\brokkr_bridge\blender_target.py surfaces\blender\brokkr_bridge\__init__.py
Assert-NativeSuccess "Blender adapter Python compile"
$cultCachePySrc = "E:\Projects\cultcache-py\src"
if (Test-Path $cultCachePySrc) {
& $pythonExe -c "import sys; sys.path.insert(0, r'$cultCachePySrc'); sys.path.insert(0, r'surfaces\blender\brokkr_bridge'); import blender_target; blender_target._load_cultcache(r'$cultCachePySrc')"
Assert-NativeSuccess "Blender adapter CultCache import"
}
}
finally {
Pop-Location