Add Blender visibility to Unity active sync

This commit is contained in:
Meta Vens 2026-06-15 01:49:02 +01:00
commit 9c2a4522f7
5 changed files with 173 additions and 0 deletions

View file

@ -901,6 +901,36 @@ fn run_sync_once(args: &SyncArgs) -> Result<SyncPassReport> {
report.unity_commands_written += 1;
}
}
("blender-to-unity", "active-state") => {
let Some(source) = blender_objects.get(&binding.blender_object_name) else {
report.messages.push(format!(
"missing Blender object {} for active-state binding {}",
binding.blender_object_name, binding.binding_id
));
continue;
};
let command_id = stable_id([
"sync",
&binding.binding_id,
&sync_var.sync_var_id,
"blender-to-unity-active",
]);
if emitted_command_ids.insert(command_id.clone()) {
let command = unity_active_command(
&command_id,
&binding.unity_object_id,
source.visible,
);
if !args.dry_run {
unity_store.put_messagepack_document(
"brokkr.unity.command_intent.v0",
&format!("unity/commands/{command_id}"),
&command,
)?;
}
report.unity_commands_written += 1;
}
}
("blender-to-unity", "material") => {
let Some(source) = blender_objects.get(&binding.blender_object_name) else {
report.messages.push(format!(
@ -1503,6 +1533,24 @@ fn unity_material_command(command_id: &str, target_object_id: &str, material_nam
])
}
fn unity_active_command(command_id: &str, target_object_id: &str, active: bool) -> Value {
json!([
"brokkr.unity.command_intent.v0",
command_id,
"setGameObjectActive",
target_object_id,
"",
"",
"",
active.to_string(),
"",
"",
"",
"",
""
])
}
fn normalize_blender_custom_property_path(path: &str) -> String {
path.trim()
.strip_prefix("customProperties.")
@ -2002,6 +2050,43 @@ mod tests {
Ok(())
}
#[test]
fn sync_once_writes_blender_visibility_to_unity_active_command() -> Result<()> {
let temp = tempfile::tempdir()?;
let unity_path = temp.path().join("unity.ccmp");
let blender_path = temp.path().join("blender.ccmp");
seed_visibility_stores(&unity_path, &blender_path)?;
let report = run_sync_once(&SyncArgs {
unity_cache: unity_path.clone(),
blender_cache: blender_path,
dry_run: false,
})?;
assert_eq!(report.unity_commands_written, 1, "{report:#?}");
let mut unity_store = MirrorStore::open(&unity_path)?;
let command = unity_store
.documents()?
.into_iter()
.find(|document| document.key.starts_with("unity/commands/"))
.expect("Brokkr should emit a Unity active-state command");
assert_eq!(
command.value.as_array().and_then(|items| items.get(2)),
Some(&json!("setGameObjectActive"))
);
assert_eq!(
command.value.as_array().and_then(|items| items.get(3)),
Some(&json!("unity-cube"))
);
assert_eq!(
command.value.as_array().and_then(|items| items.get(7)),
Some(&json!("false"))
);
Ok(())
}
#[test]
fn sync_once_writes_unity_component_property_to_blender_custom_property_command() -> Result<()>
{
@ -2159,6 +2244,60 @@ mod tests {
Ok(())
}
fn seed_visibility_stores(unity_path: &Path, blender_path: &Path) -> Result<()> {
let mut unity_store = MirrorStore::open(unity_path)?;
let mut blender_store = MirrorStore::open(blender_path)?;
unity_store.put_json_document(
"brokkr.sync.object_binding.v0",
"sync/bindings/objects/binding-cube",
&json!({
"schema": "brokkr.sync.object_binding.v0",
"bindingId": "binding-cube",
"sessionId": "session-main",
"displayName": "Cube",
"unityObjectId": "unity-cube",
"unityPath": "/Cube",
"blenderObjectName": "Cube",
"enabled": true,
"authority": "blender-to-unity"
}),
)?;
unity_store.put_json_document(
"brokkr.sync.var.v0",
"sync/vars/var-cube-active",
&json!({
"schema": "brokkr.sync.var.v0",
"syncVarId": "var-cube-active",
"sessionId": "session-main",
"bindingId": "binding-cube",
"displayName": "Active State",
"kind": "active-state",
"unityPropertyPath": "m_IsActive",
"blenderPropertyPath": "visible",
"authority": "blender-to-unity",
"enabled": true
}),
)?;
blender_store.put_json_document(
"brokkr.blender.host_snapshot.v0",
"blender/host/current",
&json!({
"objects": [{
"name": "Cube",
"type": "MESH",
"location": [0.0, 0.0, 0.0],
"rotationEuler": [0.0, 0.0, 0.0],
"scale": [1.0, 1.0, 1.0],
"visible": false,
"materials": []
}]
}),
)?;
Ok(())
}
fn seed_component_property_stores(unity_path: &Path, blender_path: &Path) -> Result<()> {
let mut unity_store = MirrorStore::open(unity_path)?;
let mut blender_store = MirrorStore::open(blender_path)?;

View file

@ -79,6 +79,8 @@ Implemented sync lanes:
- Unity GameObject transform to Blender object transform.
- Unity GameObject active state to Blender object visibility.
- Blender object visibility to Unity GameObject active state through
`setGameObjectActive`.
- Unity serialized component property to Blender object custom property through
`setObjectCustomProperty`. The syncvar uses `unityPropertyPath =
Component.Type::propertyPath` and `blenderPropertyPath = customProperties.name`.
@ -102,6 +104,7 @@ All Unity writes use `brokkr.unity.command_intent.v0` and receive
- `createGameObject`
- `attachComponent`
- `setGameObjectTransform`
- `setGameObjectActive`
- `setComponentProperty`
- `instantiatePrefab`
- `createPrefabVariant`
@ -113,6 +116,7 @@ write typed command intents; Unity executes recognized intents and publishes
receipts. `setComponentProperty` writes the target object by default; when
`componentType` is present, Unity resolves that component on the target
GameObject and writes the serialized property there.
`setGameObjectActive` uses `targetObjectId` and boolean `value`.
`instantiatePrefab` uses `assetPath` as the source prefab, `name` as the
optional instance name, and `parentObjectId` as the optional parent.
`createPrefabVariant` uses `assetPath` as the source prefab and `value` as the

View file

@ -130,6 +130,12 @@ class BrokkrPreferences(bpy.types.AddonPreferences):
description="Synchronize material assignment",
)
sync_visibility: bpy.props.BoolProperty(
name="Sync Visibility",
default=True,
description="Synchronize Blender object visibility to Unity active state",
)
sync_custom_properties: bpy.props.BoolProperty(
name="Sync Custom Properties",
default=False,
@ -201,6 +207,7 @@ class BrokkrPreferences(bpy.types.AddonPreferences):
layout.prop(self, "unity_object_path")
layout.prop(self, "sync_transform")
layout.prop(self, "sync_material")
layout.prop(self, "sync_visibility")
layout.prop(self, "sync_custom_properties")
layout.prop(self, "unity_custom_property_component_type")
layout.prop(self, "unity_custom_property_path")
@ -336,6 +343,7 @@ class BROKKR_OT_publish_object_sync(bpy.types.Operator):
prefs.unity_object_path,
prefs.sync_transform,
prefs.sync_material,
prefs.sync_visibility,
prefs.sync_custom_properties,
prefs.unity_custom_property_component_type,
prefs.unity_custom_property_path,

View file

@ -126,6 +126,7 @@ class BrokkrBlenderTarget:
unity_path: str,
sync_transform: bool,
sync_material: bool,
sync_visibility: bool,
sync_custom_properties: bool,
unity_custom_property_component_type: str = "",
unity_custom_property_path: str = "",
@ -157,6 +158,7 @@ class BrokkrBlenderTarget:
node.database.put(documents["sync_session"], f"sync/sessions/{normalized_session_id}", session)
node.database.put(documents["sync_object_binding"], f"sync/bindings/objects/{binding_id}", binding)
self._put_sync_var(node, documents, normalized_session_id, binding_id, "transform", "Transform", "Transform", "location,rotationEuler,scale", "blender-to-unity", sync_transform, "linear", now)
self._put_sync_var(node, documents, normalized_session_id, binding_id, "active-state", "Active State", "m_IsActive", "visible", "blender-to-unity", sync_visibility, "step", now)
self._put_sync_var(node, documents, normalized_session_id, binding_id, "material", "Material", "Renderer.m_Materials", "materials", "blender-to-unity", sync_material, "step", now)
unity_custom_path = _unity_custom_property_path(
unity_custom_property_component_type,

View file

@ -19,6 +19,7 @@ namespace GameCult.Brokkr.Editor
"createGameObject" => CreateGameObject(command),
"attachComponent" => AttachComponent(command),
"setGameObjectTransform" => SetGameObjectTransform(command),
"setGameObjectActive" => SetGameObjectActive(command),
"setComponentProperty" => SetComponentProperty(command),
"instantiatePrefab" => InstantiatePrefab(command),
"createPrefabVariant" => CreatePrefabVariant(command),
@ -140,6 +141,25 @@ namespace GameCult.Brokkr.Editor
return Accepted(command, "GameObject transform updated.", command.targetObjectId);
}
private static BrokkrUnityCommandReceipt SetGameObjectActive(BrokkrUnityCommand command)
{
var gameObject = ResolveGameObject(command.targetObjectId);
if (gameObject == null)
{
return Failed(command, "Target GameObject was not found.");
}
if (!bool.TryParse(command.value, out var active))
{
return Failed(command, $"Expected active state as boolean: {command.value}");
}
Undo.RecordObject(gameObject, "Brokkr Set GameObject Active");
gameObject.SetActive(active);
EditorSceneManager.MarkSceneDirty(gameObject.scene);
return Accepted(command, "GameObject active state updated.", command.targetObjectId);
}
private static BrokkrUnityCommandReceipt InstantiatePrefab(BrokkrUnityCommand command)
{
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(command.assetPath);