diff --git a/Assets/UniGLTF/Runtime/UniHumanoid/Humanoid.cs b/Assets/UniGLTF/Runtime/UniHumanoid/Humanoid.cs index 5680e7d45..8261b90f9 100644 --- a/Assets/UniGLTF/Runtime/UniHumanoid/Humanoid.cs +++ b/Assets/UniGLTF/Runtime/UniHumanoid/Humanoid.cs @@ -456,5 +456,35 @@ namespace UniHumanoid bone = default; return false; } + + public static Func Get_GetBoneTransform(GameObject root) + { + if (root.TryGetComponent(out var humanoid)) + { + return humanoid.GetBoneTransform; + } + else if (root.TryGetComponent(out var animator)) + { + // avatar + var avatar = animator.avatar; + if (avatar == null) + { + throw new ArgumentException("no avatar"); + } + if (!avatar.isValid) + { + throw new ArgumentException("invalid avatar"); + } + if (!avatar.isHuman) + { + throw new ArgumentException("avatar is not humanoid"); + } + return animator.GetBoneTransform; + } + else + { + throw new ArgumentException("no animator nor humanoid"); + } + } } } diff --git a/Assets/VRM/Editor/Format/VRMEditorExporter.cs b/Assets/VRM/Editor/Format/VRMEditorExporter.cs index a51df4bfc..427da420d 100644 --- a/Assets/VRM/Editor/Format/VRMEditorExporter.cs +++ b/Assets/VRM/Editor/Format/VRMEditorExporter.cs @@ -167,14 +167,14 @@ namespace VRM { // copy元 - var animator = exportRoot.GetComponentOrNull(); + var getBone = UniHumanoid.Humanoid.Get_GetBoneTransform(exportRoot); var beforeTransforms = exportRoot.GetComponentsInChildren(true); // copy先 var afterTransforms = target.GetComponentsInChildren(true); // copy先のhumanoidBoneのリストを得る var humanTransforms = CachedEnum.GetValues() .Where(x => x != HumanBodyBones.LastBone) - .Select(x => animator.GetBoneTransform(x)) + .Select(x => getBone(x)) .Where(x => x != null) .Select(x => afterTransforms[Array.IndexOf(beforeTransforms, x)]) // copy 先を得る .ToArray(); diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index 5497b1b3e..f1f5d02b2 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -47,55 +47,51 @@ namespace VRM public override void ExportExtensions(ITextureSerializer textureSerializer) { - // avatar - if (Copy.TryGetComponent(out var animator)) + var getBone = UniHumanoid.Humanoid.Get_GetBoneTransform(Copy); + + UniHumanoid.AvatarDescription description = null; + var isCreated = false; + if (Copy.TryGetComponent(out var humanoid)) { - UniHumanoid.AvatarDescription description = null; - var isCreated = false; - if (Copy.TryGetComponent(out var humanoid)) + if (humanoid != null) { - if (humanoid != null) - { - description = humanoid.GetDescription(out isCreated); - } + description = humanoid.GetDescription(out isCreated); } - var nodes = Copy.transform.Traverse().Skip(1).ToList(); + } + var nodes = Copy.transform.Traverse().Skip(1).ToList(); + { + if (description != null) { - if (description != null) - { - // use description - VRM.humanoid.Apply(description, nodes); - } - - if (isCreated) - { - GameObject.DestroyImmediate(description); - } + // use description + VRM.humanoid.Apply(description, nodes); } + if (isCreated) { - // set humanoid bone mapping - var avatar = animator.avatar; - foreach (HumanBodyBones key in CachedEnum.GetValues()) - { - if (key == HumanBodyBones.LastBone) - { - break; - } + GameObject.DestroyImmediate(description); + } + } - var transform = animator.GetBoneTransform(key); - if (transform != null) - { - var nodeIndex = nodes.IndexOf(transform); - if (nodeIndex < 0) - { - Debug.LogError($"ヒューマンボーンが export 対象に含まれていない?", transform); - } - else - { - VRM.humanoid.SetNodeIndex(key, nodeIndex); - } - } + // set humanoid bone mapping + // var avatar = animator.avatar; + foreach (HumanBodyBones key in CachedEnum.GetValues()) + { + if (key == HumanBodyBones.LastBone) + { + break; + } + + var transform = getBone(key); + if (transform != null) + { + var nodeIndex = nodes.IndexOf(transform); + if (nodeIndex < 0) + { + Debug.LogError($"ヒューマンボーンが export 対象に含まれていない?", transform); + } + else + { + VRM.humanoid.SetNodeIndex(key, nodeIndex); } } }