Add Unity component property sync lane

This commit is contained in:
Meta Vens 2026-06-15 01:40:51 +01:00
commit a53b034053
6 changed files with 310 additions and 0 deletions

View file

@ -653,6 +653,20 @@ struct UnityObject {
local_position: Vec<f64>,
local_euler_angles: Vec<f64>,
local_scale: Vec<f64>,
components: Vec<UnityComponent>,
}
#[derive(Debug, Clone)]
struct UnityComponent {
type_name: String,
assembly_qualified_name: String,
properties: Vec<UnityProperty>,
}
#[derive(Debug, Clone)]
struct UnityProperty {
path: String,
value: String,
}
#[derive(Debug, Clone)]
@ -800,6 +814,61 @@ fn run_sync_once(args: &SyncArgs) -> Result<SyncPassReport> {
report.blender_commands_written += 1;
}
}
("unity-to-blender", "component-property") => {
let Some(source) = find_unity_object(&unity_objects, binding) else {
report.messages.push(format!(
"missing Unity object for component-property binding {}",
binding.binding_id
));
continue;
};
let (component_type, property_path) =
split_unity_property_target(&sync_var.unity_property_path);
if property_path.is_empty() {
report.messages.push(format!(
"component-property sync var {} has no Unity property path",
sync_var.sync_var_id
));
continue;
}
let Some(property) = find_unity_property(source, component_type, property_path)
else {
report.messages.push(format!(
"component-property sync requested for {} but Unity property {} was not present",
source.name, sync_var.unity_property_path
));
continue;
};
let blender_property_name =
normalize_blender_custom_property_path(&sync_var.blender_property_path);
let command_id = stable_id([
"sync",
&binding.binding_id,
&sync_var.sync_var_id,
"unity-to-blender-component-property",
]);
if emitted_command_ids.insert(command_id.clone()) {
let command = json!({
"schema": "brokkr.blender.command_intent.v0",
"commandId": command_id,
"action": "setObjectCustomProperty",
"targetObjectName": binding.blender_object_name,
"propertyName": blender_property_name,
"value": property.value,
});
if !args.dry_run {
blender_store.put_json_document(
"brokkr.blender.command_intent.v0",
&format!(
"blender/commands/{}",
command["commandId"].as_str().unwrap_or("sync")
),
&command,
)?;
}
report.blender_commands_written += 1;
}
}
("blender-to-unity", "transform") => {
let Some(source) = blender_objects.get(&binding.blender_object_name) else {
report.messages.push(format!(
@ -1206,11 +1275,50 @@ fn parse_unity_objects(documents: &[CacheDocument]) -> Vec<UnityObject> {
local_euler_angles: field_vec3(value, 11, "localEulerAngles").unwrap_or_default(),
local_scale: field_vec3(value, 12, "localScale")
.unwrap_or_else(|| vec![1.0, 1.0, 1.0]),
components: parse_unity_components(value),
})
})
.collect()
}
fn parse_unity_components(value: &Value) -> Vec<UnityComponent> {
field_array(value, 9, "components")
.map(|components| {
components
.iter()
.filter_map(|component| {
Some(UnityComponent {
type_name: field_string(component, 1, "typeName").unwrap_or_default(),
assembly_qualified_name: field_string(
component,
2,
"assemblyQualifiedName",
)
.unwrap_or_default(),
properties: parse_unity_properties(component),
})
})
.collect()
})
.unwrap_or_default()
}
fn parse_unity_properties(value: &Value) -> Vec<UnityProperty> {
field_array(value, 4, "properties")
.map(|properties| {
properties
.iter()
.filter_map(|property| {
Some(UnityProperty {
path: field_string(property, 0, "path")?,
value: field_string(property, 3, "value").unwrap_or_default(),
})
})
.collect()
})
.unwrap_or_default()
}
fn parse_blender_objects(documents: &[CacheDocument]) -> BTreeMap<String, BlenderObject> {
let Some(snapshot) = documents
.iter()
@ -1307,6 +1415,28 @@ fn find_unity_object<'a>(
})
}
fn find_unity_property<'a>(
object: &'a UnityObject,
component_type: &str,
property_path: &str,
) -> Option<&'a UnityProperty> {
object
.components
.iter()
.filter(|component| {
component_type.is_empty()
|| component.type_name == component_type
|| component
.type_name
.rsplit('.')
.next()
.is_some_and(|name| name == component_type)
|| component.assembly_qualified_name == component_type
})
.flat_map(|component| component.properties.iter())
.find(|property| property.path == property_path)
}
fn unity_transform_command(
command_id: &str,
target_object_id: &str,
@ -1872,6 +2002,36 @@ mod tests {
Ok(())
}
#[test]
fn sync_once_writes_unity_component_property_to_blender_custom_property_command() -> Result<()>
{
let temp = tempfile::tempdir()?;
let unity_path = temp.path().join("unity.ccmp");
let blender_path = temp.path().join("blender.ccmp");
seed_component_property_stores(&unity_path, &blender_path)?;
let report = run_sync_once(&SyncArgs {
unity_cache: unity_path,
blender_cache: blender_path.clone(),
dry_run: false,
})?;
assert_eq!(report.blender_commands_written, 1, "{report:#?}");
let mut blender_store = MirrorStore::open(&blender_path)?;
let command = blender_store
.documents()?
.into_iter()
.find(|document| document.key.starts_with("blender/commands/"))
.expect("Brokkr should emit a Blender custom property command");
assert_eq!(command.value["action"], "setObjectCustomProperty");
assert_eq!(command.value["targetObjectName"], "Cube");
assert_eq!(command.value["propertyName"], "speed");
assert_eq!(command.value["value"], "12.5");
Ok(())
}
fn seed_sync_stores(unity_path: &Path, blender_path: &Path, authority: &str) -> Result<()> {
let mut unity_store = MirrorStore::open(unity_path)?;
let mut blender_store = MirrorStore::open(blender_path)?;
@ -1999,6 +2159,85 @@ mod tests {
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)?;
unity_store.put_json_document(
"brokkr.unity.host_snapshot.v0",
"unity/host/current",
&json!({
"sceneObjects": [{
"objectId": "unity-cube",
"name": "Cube",
"path": "/Cube",
"activeSelf": true,
"components": [{
"componentId": "component-speed",
"typeName": "GameCult.Example.SpeedSource",
"assemblyQualifiedName": "GameCult.Example.SpeedSource, Example",
"enabled": true,
"properties": [{
"path": "speed",
"displayName": "Speed",
"propertyType": "Float",
"value": "12.5",
"editable": true
}]
}]
}]
}),
)?;
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": "unity-to-blender"
}),
)?;
unity_store.put_json_document(
"brokkr.sync.var.v0",
"sync/vars/var-cube-component-speed",
&json!({
"schema": "brokkr.sync.var.v0",
"syncVarId": "var-cube-component-speed",
"sessionId": "session-main",
"bindingId": "binding-cube",
"displayName": "Speed",
"kind": "component-property",
"unityPropertyPath": "GameCult.Example.SpeedSource::speed",
"blenderPropertyPath": "customProperties.speed",
"authority": "unity-to-blender",
"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": true,
"materials": []
}]
}),
)?;
Ok(())
}
fn seed_custom_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,9 @@ Implemented sync lanes:
- Unity GameObject transform to Blender object transform.
- Unity GameObject active state to Blender object visibility.
- Unity serialized component property to Blender object custom property through
`setObjectCustomProperty`. The syncvar uses `unityPropertyPath =
Component.Type::propertyPath` and `blenderPropertyPath = customProperties.name`.
- Blender object transform to Unity GameObject transform.
- Blender object material name to Unity Renderer material assignment through
`assignMaterial`.
@ -126,6 +129,7 @@ All Blender writes use `brokkr.blender.command_intent.v0` and receive
- `deleteObject`
- `setObjectTransform`
- `setObjectVisibility`
- `setObjectCustomProperty`
- `selectObject`
- `assignMaterial`

View file

@ -337,6 +337,8 @@ class BrokkrBlenderTarget:
return self._set_object_transform(context, command)
if action == "setObjectVisibility":
return self._set_object_visibility(context, command)
if action == "setObjectCustomProperty":
return self._set_object_custom_property(context, command)
if action == "selectObject":
return self._select_object(context, command)
if action == "assignMaterial":
@ -394,6 +396,17 @@ class BrokkrBlenderTarget:
obj.hide_render = not visible
return self._receipt(command, "accepted", "Blender object visibility updated.", obj.name)
def _set_object_custom_property(self, context: Any, command: dict[str, Any]) -> dict[str, Any]:
obj = self._resolve_object(command)
if obj is None:
return self._receipt(command, "failed", "Target Blender object was not found.", "")
property_name = command.get("propertyName") or "brokkrValue"
if property_name.startswith("customProperties."):
property_name = property_name[len("customProperties."):]
obj[property_name] = command.get("value")
return self._receipt(command, "accepted", "Blender custom property updated.", obj.name)
def _select_object(self, context: Any, command: dict[str, Any]) -> dict[str, Any]:
obj = self._resolve_object(command)
if obj is None:

View file

@ -55,5 +55,6 @@ Admitted command actions:
- `deleteObject`
- `setObjectTransform`
- `setObjectVisibility`
- `setObjectCustomProperty`
- `selectObject`
- `assignMaterial`

View file

@ -98,6 +98,24 @@ namespace GameCult.Brokkr.Editor
set => EditorPrefs.SetString("GameCult.Brokkr.BlenderCustomPropertyPath", value);
}
internal static string UnityComponentPropertyComponentType
{
get => EditorPrefs.GetString("GameCult.Brokkr.UnityComponentPropertyComponentType", "");
set => EditorPrefs.SetString("GameCult.Brokkr.UnityComponentPropertyComponentType", value);
}
internal static string UnityComponentPropertyPath
{
get => EditorPrefs.GetString("GameCult.Brokkr.UnityComponentPropertyPath", "");
set => EditorPrefs.SetString("GameCult.Brokkr.UnityComponentPropertyPath", value);
}
internal static string BlenderComponentPropertyPath
{
get => EditorPrefs.GetString("GameCult.Brokkr.BlenderComponentPropertyPath", "");
set => EditorPrefs.SetString("GameCult.Brokkr.BlenderComponentPropertyPath", value);
}
internal static string ScriptableObjectType
{
get => EditorPrefs.GetString("GameCult.Brokkr.ScriptableObjectType", "");

View file

@ -25,6 +25,7 @@ namespace GameCult.Brokkr.Editor
private bool syncActiveState = true;
private bool syncMaterial;
private bool syncCustomProperty;
private bool syncComponentProperty;
private bool syncTimelineFrame = true;
private bool syncCinemachineCamera = true;
private float syncTimelineFrameRate = 24.0f;
@ -35,6 +36,9 @@ namespace GameCult.Brokkr.Editor
private string unityCustomPropertyComponentType = "";
private string unityCustomPropertyPath = "";
private string blenderCustomPropertyPath = "";
private string unityComponentPropertyComponentType = "";
private string unityComponentPropertyPath = "";
private string blenderComponentPropertyPath = "";
private string scriptableObjectType = "";
private string scriptableObjectName = "";
private string scriptableObjectAssetPath = "Assets/BrokkrAsset.asset";
@ -65,6 +69,9 @@ namespace GameCult.Brokkr.Editor
unityCustomPropertyComponentType = BrokkrSettings.UnityCustomPropertyComponentType;
unityCustomPropertyPath = BrokkrSettings.UnityCustomPropertyPath;
blenderCustomPropertyPath = BrokkrSettings.BlenderCustomPropertyPath;
unityComponentPropertyComponentType = BrokkrSettings.UnityComponentPropertyComponentType;
unityComponentPropertyPath = BrokkrSettings.UnityComponentPropertyPath;
blenderComponentPropertyPath = BrokkrSettings.BlenderComponentPropertyPath;
scriptableObjectType = BrokkrSettings.ScriptableObjectType;
scriptableObjectName = BrokkrSettings.ScriptableObjectName;
scriptableObjectAssetPath = BrokkrSettings.ScriptableObjectAssetPath;
@ -113,6 +120,9 @@ namespace GameCult.Brokkr.Editor
BrokkrSettings.UnityCustomPropertyComponentType = unityCustomPropertyComponentType;
BrokkrSettings.UnityCustomPropertyPath = unityCustomPropertyPath;
BrokkrSettings.BlenderCustomPropertyPath = blenderCustomPropertyPath;
BrokkrSettings.UnityComponentPropertyComponentType = unityComponentPropertyComponentType;
BrokkrSettings.UnityComponentPropertyPath = unityComponentPropertyPath;
BrokkrSettings.BlenderComponentPropertyPath = blenderComponentPropertyPath;
BrokkrSettings.ScriptableObjectType = scriptableObjectType;
BrokkrSettings.ScriptableObjectName = scriptableObjectName;
BrokkrSettings.ScriptableObjectAssetPath = scriptableObjectAssetPath;
@ -219,6 +229,13 @@ namespace GameCult.Brokkr.Editor
unityCustomPropertyPath = EditorGUILayout.TextField("Unity Property Path", unityCustomPropertyPath);
blenderCustomPropertyPath = EditorGUILayout.TextField("Blender Custom Property", blenderCustomPropertyPath);
}
syncComponentProperty = EditorGUILayout.Toggle("Sync Component Property", syncComponentProperty);
using (new EditorGUI.DisabledScope(!syncComponentProperty))
{
unityComponentPropertyComponentType = EditorGUILayout.TextField("Source Component Type", unityComponentPropertyComponentType);
unityComponentPropertyPath = EditorGUILayout.TextField("Source Property Path", unityComponentPropertyPath);
blenderComponentPropertyPath = EditorGUILayout.TextField("Blender Custom Property", blenderComponentPropertyPath);
}
using (new EditorGUILayout.HorizontalScope())
{
@ -461,6 +478,14 @@ namespace GameCult.Brokkr.Editor
: NormalizeBlenderCustomPropertyPath(blenderCustomPropertyPath);
PublishSyncVar(bindingId, "custom-property", "Custom Property", unityPath, blenderPath, true, "step", now);
}
if (syncComponentProperty)
{
var unityPath = BuildUnityComponentPropertyPath();
var blenderPath = string.IsNullOrWhiteSpace(blenderComponentPropertyPath)
? "customProperties.value"
: NormalizeBlenderCustomPropertyPath(blenderComponentPropertyPath);
PublishSyncVar(bindingId, "component-property", "Component Property", unityPath, blenderPath, true, "step", now);
}
SetStatus($"Published Brokkr object sync binding: {binding.displayName}", MessageType.Info);
}
catch (Exception error)
@ -575,6 +600,16 @@ namespace GameCult.Brokkr.Editor
: $"{unityCustomPropertyComponentType.Trim()}::{propertyPath}";
}
private string BuildUnityComponentPropertyPath()
{
var propertyPath = string.IsNullOrWhiteSpace(unityComponentPropertyPath)
? "m_Enabled"
: unityComponentPropertyPath.Trim();
return string.IsNullOrWhiteSpace(unityComponentPropertyComponentType)
? propertyPath
: $"{unityComponentPropertyComponentType.Trim()}::{propertyPath}";
}
private static string NormalizeBlenderCustomPropertyPath(string path)
{
var trimmed = path.Trim();