Sync Unity materials into Blender
This commit is contained in:
parent
16a90514ce
commit
de8a4b6d63
4 changed files with 124 additions and 5 deletions
|
|
@ -653,6 +653,7 @@ struct UnityObject {
|
|||
local_position: Vec<f64>,
|
||||
local_euler_angles: Vec<f64>,
|
||||
local_scale: Vec<f64>,
|
||||
materials: Vec<String>,
|
||||
components: Vec<UnityComponent>,
|
||||
}
|
||||
|
||||
|
|
@ -814,6 +815,43 @@ fn run_sync_once(args: &SyncArgs) -> Result<SyncPassReport> {
|
|||
report.blender_commands_written += 1;
|
||||
}
|
||||
}
|
||||
("unity-to-blender", "material") => {
|
||||
let Some(source) = find_unity_object(&unity_objects, binding) else {
|
||||
report.messages.push(format!(
|
||||
"missing Unity object for material binding {}",
|
||||
binding.binding_id
|
||||
));
|
||||
continue;
|
||||
};
|
||||
let Some(material) = source.materials.first() else {
|
||||
report.messages.push(format!(
|
||||
"material sync requested for {} but no Unity material is assigned",
|
||||
source.name
|
||||
));
|
||||
continue;
|
||||
};
|
||||
let command_id = stable_id([
|
||||
"sync",
|
||||
&binding.binding_id,
|
||||
&sync_var.sync_var_id,
|
||||
"unity-to-blender-material",
|
||||
]);
|
||||
if emitted_command_ids.insert(command_id.clone()) {
|
||||
let command = blender_material_command(
|
||||
&command_id,
|
||||
&binding.blender_object_name,
|
||||
material,
|
||||
);
|
||||
if !args.dry_run {
|
||||
blender_store.put_json_document(
|
||||
"brokkr.blender.command_intent.v0",
|
||||
&format!("blender/commands/{command_id}"),
|
||||
&command,
|
||||
)?;
|
||||
}
|
||||
report.blender_commands_written += 1;
|
||||
}
|
||||
}
|
||||
("unity-to-blender", "component-property" | "custom-property") => {
|
||||
let Some(source) = find_unity_object(&unity_objects, binding) else {
|
||||
report.messages.push(format!(
|
||||
|
|
@ -1303,6 +1341,14 @@ 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]),
|
||||
materials: field_array(value, 13, "materialNames")
|
||||
.map(|items| {
|
||||
items
|
||||
.iter()
|
||||
.filter_map(|item| item.as_str().map(str::to_string))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
components: parse_unity_components(value),
|
||||
})
|
||||
})
|
||||
|
|
@ -1531,6 +1577,20 @@ fn unity_material_command(command_id: &str, target_object_id: &str, material_nam
|
|||
])
|
||||
}
|
||||
|
||||
fn blender_material_command(
|
||||
command_id: &str,
|
||||
target_object_name: &str,
|
||||
material_name: &str,
|
||||
) -> Value {
|
||||
json!({
|
||||
"schema": "brokkr.blender.command_intent.v0",
|
||||
"commandId": command_id,
|
||||
"action": "assignMaterial",
|
||||
"targetObjectName": target_object_name,
|
||||
"materialName": material_name,
|
||||
})
|
||||
}
|
||||
|
||||
fn unity_active_command(command_id: &str, target_object_id: &str, active: bool) -> Value {
|
||||
json!([
|
||||
"brokkr.unity.command_intent.v0",
|
||||
|
|
@ -2083,7 +2143,7 @@ mod tests {
|
|||
let temp = tempfile::tempdir()?;
|
||||
let unity_path = temp.path().join("unity.ccmp");
|
||||
let blender_path = temp.path().join("blender.ccmp");
|
||||
seed_material_stores(&unity_path, &blender_path)?;
|
||||
seed_material_stores(&unity_path, &blender_path, "blender-to-unity")?;
|
||||
|
||||
let report = run_sync_once(&SyncArgs {
|
||||
unity_cache: unity_path.clone(),
|
||||
|
|
@ -2115,6 +2175,34 @@ mod tests {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_once_writes_unity_material_to_blender_assign_material_command() -> Result<()> {
|
||||
let temp = tempfile::tempdir()?;
|
||||
let unity_path = temp.path().join("unity.ccmp");
|
||||
let blender_path = temp.path().join("blender.ccmp");
|
||||
seed_material_stores(&unity_path, &blender_path, "unity-to-blender")?;
|
||||
|
||||
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 material command");
|
||||
|
||||
assert_eq!(command.value["action"], "assignMaterial");
|
||||
assert_eq!(command.value["targetObjectName"], "Cube");
|
||||
assert_eq!(command.value["materialName"], "UnityGold");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_once_writes_blender_visibility_to_unity_active_command() -> Result<()> {
|
||||
let temp = tempfile::tempdir()?;
|
||||
|
|
@ -2284,10 +2372,26 @@ mod tests {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn seed_material_stores(unity_path: &Path, blender_path: &Path) -> Result<()> {
|
||||
fn seed_material_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)?;
|
||||
|
||||
unity_store.put_json_document(
|
||||
"brokkr.unity.host_snapshot.v0",
|
||||
"unity/host/current",
|
||||
&json!({
|
||||
"sceneObjects": [{
|
||||
"objectId": "unity-cube",
|
||||
"name": "Cube",
|
||||
"path": "/Cube",
|
||||
"activeSelf": true,
|
||||
"localPosition": [0.0, 0.0, 0.0],
|
||||
"localEulerAngles": [0.0, 0.0, 0.0],
|
||||
"localScale": [1.0, 1.0, 1.0],
|
||||
"materialNames": ["UnityGold"]
|
||||
}]
|
||||
}),
|
||||
)?;
|
||||
unity_store.put_json_document(
|
||||
"brokkr.sync.object_binding.v0",
|
||||
"sync/bindings/objects/binding-cube",
|
||||
|
|
@ -2300,7 +2404,7 @@ mod tests {
|
|||
"unityPath": "/Cube",
|
||||
"blenderObjectName": "Cube",
|
||||
"enabled": true,
|
||||
"authority": "blender-to-unity"
|
||||
"authority": authority
|
||||
}),
|
||||
)?;
|
||||
unity_store.put_json_document(
|
||||
|
|
@ -2315,7 +2419,7 @@ mod tests {
|
|||
"kind": "material",
|
||||
"unityPropertyPath": "Renderer.m_Materials",
|
||||
"blenderPropertyPath": "materials",
|
||||
"authority": "blender-to-unity",
|
||||
"authority": authority,
|
||||
"enabled": true
|
||||
}),
|
||||
)?;
|
||||
|
|
|
|||
|
|
@ -88,6 +88,8 @@ Implemented sync lanes:
|
|||
|
||||
- Unity GameObject transform to Blender object transform.
|
||||
- Unity GameObject active state to Blender object visibility.
|
||||
- Unity Renderer material name to Blender object material assignment through
|
||||
`assignMaterial`.
|
||||
- Blender object visibility to Unity GameObject active state through
|
||||
`setGameObjectActive`.
|
||||
- Unity serialized component property to Blender object custom property through
|
||||
|
|
|
|||
|
|
@ -90,7 +90,8 @@ namespace GameCult.Brokkr.Editor
|
|||
components = CaptureComponents(gameObject),
|
||||
localPosition = WriteVector3(gameObject.transform.localPosition),
|
||||
localEulerAngles = WriteVector3(gameObject.transform.localEulerAngles),
|
||||
localScale = WriteVector3(gameObject.transform.localScale)
|
||||
localScale = WriteVector3(gameObject.transform.localScale),
|
||||
materialNames = CaptureMaterialNames(gameObject)
|
||||
});
|
||||
|
||||
for (var childIndex = 0; childIndex < gameObject.transform.childCount; childIndex++)
|
||||
|
|
@ -115,6 +116,17 @@ namespace GameCult.Brokkr.Editor
|
|||
.ToArray();
|
||||
}
|
||||
|
||||
private static string[] CaptureMaterialNames(GameObject gameObject)
|
||||
{
|
||||
var renderer = gameObject.GetComponent<Renderer>();
|
||||
return renderer == null
|
||||
? Array.Empty<string>()
|
||||
: renderer.sharedMaterials
|
||||
.Where(material => material != null)
|
||||
.Select(material => material.name)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
private static BrokkrSerializedPropertySnapshot[] CaptureProperties(UnityEngine.Object target)
|
||||
{
|
||||
var properties = new List<BrokkrSerializedPropertySnapshot>();
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ namespace GameCult.Brokkr
|
|||
[Key(10)] public string localPosition = "";
|
||||
[Key(11)] public string localEulerAngles = "";
|
||||
[Key(12)] public string localScale = "";
|
||||
[Key(13)] public string[] materialNames = Array.Empty<string>();
|
||||
}
|
||||
|
||||
[MessagePackObject]
|
||||
|
|
|
|||
Loading…
Reference in a new issue