From 42df45a8247628ea62ed4c25b6dd1ee2d5db57fb Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Mon, 28 Aug 2023 18:29:45 +0900 Subject: [PATCH 1/3] fix(ClearEndpointPosition): not working correctly with ignore transforms --- .../Processors/ClearEndpointPositionProcessor.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Editor/Processors/ClearEndpointPositionProcessor.cs b/Editor/Processors/ClearEndpointPositionProcessor.cs index 824140fa9..d3eb246ff 100644 --- a/Editor/Processors/ClearEndpointPositionProcessor.cs +++ b/Editor/Processors/ClearEndpointPositionProcessor.cs @@ -21,19 +21,21 @@ public static void Process(VRCPhysBoneBase pb) EditorUtility.SetDirty(pb); } - internal static void WalkChildrenAndSetEndpoint(Transform target, VRCPhysBoneBase physBone) + internal static bool WalkChildrenAndSetEndpoint(Transform target, VRCPhysBoneBase physBone) { if (physBone.ignoreTransforms.Contains(target)) - return; - if (target.childCount == 0) + return false; + var childCount = 0; + for (var i = 0; i < target.childCount; i++) + if (WalkChildrenAndSetEndpoint(target.GetChild(i), physBone)) + childCount++; + if (childCount == 0) { var go = new GameObject($"{target.name}_EndPhysBone"); go.transform.parent = target; go.transform.localPosition = physBone.endpointPosition; - return; } - for (var i = 0; i < target.childCount; i++) - WalkChildrenAndSetEndpoint(target.GetChild(i), physBone); + return true; } } } From 311f66e3dd049a8f8ed9f895f4dabcee720f79cc Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Mon, 28 Aug 2023 18:46:08 +0900 Subject: [PATCH 2/3] feat(ClearEndpointPosition): Undo Support for Apply and Remove Component --- Editor/ClearEndpointPositionEditor.cs | 17 +++++++++-- .../ClearEndpointPositionProcessor.cs | 28 ++++++++++++------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/Editor/ClearEndpointPositionEditor.cs b/Editor/ClearEndpointPositionEditor.cs index 6e2a6f424..9c5aa30a0 100644 --- a/Editor/ClearEndpointPositionEditor.cs +++ b/Editor/ClearEndpointPositionEditor.cs @@ -13,8 +13,21 @@ protected override void OnInspectorGUIInner() { if (GUILayout.Button(CL4EE.Tr("ClearEndpointPosition:button:Apply and Remove Component"))) { - ClearEndpointPositionProcessor.Process(((Component)target).GetComponent()); - DestroyImmediate(target); + var pb = ((Component)target).GetComponent(); + Undo.SetCurrentGroupName("Clear Endpoint Position"); + ClearEndpointPositionProcessor.CreateEndBones(pb, (name, parent, localPosition) => + { + var gameObject = new GameObject(name); + Undo.RegisterCreatedObjectUndo(gameObject, $"Create EndBone: {name}"); + Undo.SetTransformParent(gameObject.transform, parent, $"Set EndBone Parent: {name}"); + Undo.RecordObject(gameObject.transform, $"Set Position of EndBone: {name}"); + gameObject.transform.localPosition = localPosition; + PrefabUtility.RecordPrefabInstancePropertyModifications(gameObject.transform); + }); + Undo.RecordObject(pb, "Set Endpoint Position to Zero"); + pb.endpointPosition = Vector3.zero; + PrefabUtility.RecordPrefabInstancePropertyModifications(pb); + Undo.DestroyObjectImmediate(target); } } } diff --git a/Editor/Processors/ClearEndpointPositionProcessor.cs b/Editor/Processors/ClearEndpointPositionProcessor.cs index d3eb246ff..8e987b9fe 100644 --- a/Editor/Processors/ClearEndpointPositionProcessor.cs +++ b/Editor/Processors/ClearEndpointPositionProcessor.cs @@ -1,3 +1,4 @@ +using System; using Anatawa12.AvatarOptimizer.ErrorReporting; using UnityEditor; using UnityEngine; @@ -12,29 +13,36 @@ public void Process(OptimizerSession session) BuildReport.ReportingObjects(session.GetComponents(), component => BuildReport.ReportingObjects(component.GetComponents(), Process)); } - + public static void Process(VRCPhysBoneBase pb) { - if (pb.endpointPosition == Vector3.zero) return; - WalkChildrenAndSetEndpoint(pb.GetTarget(), pb); + CreateEndBones(pb, (name, parent, localPosition) => + { + new GameObject(name) { transform = { localPosition = localPosition } } + .transform.SetParent(parent, worldPositionStays: false); + }); pb.endpointPosition = Vector3.zero; EditorUtility.SetDirty(pb); } - internal static bool WalkChildrenAndSetEndpoint(Transform target, VRCPhysBoneBase physBone) + public static void CreateEndBones(VRCPhysBoneBase pb, Action createEndBone) + { + if (pb.endpointPosition == Vector3.zero) return; + WalkChildrenAndSetEndpoint(pb.GetTarget(), pb, createEndBone); + } + + internal static bool WalkChildrenAndSetEndpoint(Transform target, VRCPhysBoneBase physBone, + Action createEndBone) { if (physBone.ignoreTransforms.Contains(target)) return false; var childCount = 0; for (var i = 0; i < target.childCount; i++) - if (WalkChildrenAndSetEndpoint(target.GetChild(i), physBone)) + if (WalkChildrenAndSetEndpoint(target.GetChild(i), physBone, createEndBone)) childCount++; if (childCount == 0) - { - var go = new GameObject($"{target.name}_EndPhysBone"); - go.transform.parent = target; - go.transform.localPosition = physBone.endpointPosition; - } + createEndBone($"{target.name}_EndPhysBone", target, physBone.endpointPosition); + return true; } } From 42fe68131ff3707b379ee336afa45e18576ac812 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Mon, 28 Aug 2023 18:49:18 +0900 Subject: [PATCH 3/3] docs(changelog): add Clear Endpoint Position improvments --- CHANGELOG-PRERELEASE.md | 2 ++ CHANGELOG.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/CHANGELOG-PRERELEASE.md b/CHANGELOG-PRERELEASE.md index beb9c33bd..947635231 100644 --- a/CHANGELOG-PRERELEASE.md +++ b/CHANGELOG-PRERELEASE.md @@ -16,6 +16,8 @@ The format is based on [Keep a Changelog]. ### Removed ### Fixed +- Clear Endpoint Position may not work well with ignore transforms `#390` +- Clear Endpoint Position doesn't support Undo `#390` ### Security diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d1b675cb..4ddab8d34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ The format is based on [Keep a Changelog]. ### Fixed - MergeBone not working well with non-restpose bones `#379` - Unclear Error with Mesh with Read/Write off `#386` +- Clear Endpoint Position may not work well with ignore transforms `#390` +- Clear Endpoint Position doesn't support Undo `#390` ### Security