Import a 3D Model and automatically assign and export animations
TypeScript
Fix a bug handling a bad chain in the rig
Scott Petrovic2897c922f6d3
diff --git a/src/retarget/AnimationRetargetService.ts b/src/retarget/AnimationRetargetService.tsindex 2dbdd22..20a6550 100644--- a/src/retarget/AnimationRetargetService.ts+++ b/src/retarget/AnimationRetargetService.ts@@ -147,7 +147,7 @@ export class AnimationRetargetService {// we ignore Mesh2Motion type since that is identical to the source and does not need retargetingif (this.skeleton_type === SkeletonType.Human && this.target_mapping_type !== TargetBoneMappingType.Mesh2Motion) {console.log('Using Human Retargeter for retargeting animation clip:', source_clip.name, this.target_mapping_type)- return this.apply_human_swing_twist_retargeting(source_clip, this.target_mapping_type)+ return this.apply_human_swing_twist_retargeting(source_clip)}// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~@@ -206,7 +206,7 @@ export class AnimationRetargetService {// #region PRIVATE METHODS- private apply_human_swing_twist_retargeting (source_clip: AnimationClip, target_mapping_type: TargetBoneMappingType): AnimationClip {+ private apply_human_swing_twist_retargeting (source_clip: AnimationClip): AnimationClip {// the retargeter needs Skeleton inputs for both source and target.// the source armature is a Group, so we need to convert to a THREE.Skeleton before we can continueconst source_skeleton: Skeleton | null = RetargetUtils.create_skeleton_from_group_object(this.source_armature)@@ -236,20 +236,17 @@ export class AnimationRetargetService {const source_rig: Rig = new Rig(source_skeleton)const target_rig: Rig = new Rig(detached_target_skeleton)- // if it is a known bone mapping, we can grab the preset config- // if not, we will need to manually build the config from the bone mappings- if (target_mapping_type === TargetBoneMappingType.Mixamo) {- source_rig.fromConfig(HumanChainConfig.mesh2motion_config)- target_rig.fromConfig(HumanChainConfig.mixamo_config)- } else if (target_mapping_type === TargetBoneMappingType.Custom ||- target_mapping_type === TargetBoneMappingType.Rigify) {- const custom_source_config = HumanChainConfig.build_custom_source_config(this.get_bone_mappings())- const custom_target_config = HumanChainConfig.build_custom_target_config(custom_source_config, this.get_bone_mappings())-- // set the custom rigs from the generated configs- source_rig.fromConfig(custom_source_config)- target_rig.fromConfig(custom_target_config)- }+ // Build the chain configs from the bone mapping we actually have, whatever rig+ // type was detected. There used to be a shortcut here that fed the Mixamo rig a+ // hardcoded config, but that config spells every bone with a "mixamorig" prefix,+ // so a Mixamo rig exported without the prefix resolved to no joints at all and+ // came out as an empty rig. The bone mapping already encodes the same chains+ // using the target's real bone names, so there is nothing to shortcut.+ const custom_source_config = HumanChainConfig.build_custom_source_config(this.get_bone_mappings())+ const custom_target_config = HumanChainConfig.build_custom_target_config(custom_source_config, this.get_bone_mappings())++ source_rig.fromConfig(custom_source_config)+ target_rig.fromConfig(custom_target_config)// instrumentation. Runs before the retargeter drives anything, so the rigs are still// holding the rest pose they captured. See RetargetDiagnostics for why each check existsdiff --git a/src/retarget/bone-automap/BoneAutoMapping.test.ts b/src/retarget/bone-automap/BoneAutoMapping.test.tsindex 8781c61..3d66e95 100644--- a/src/retarget/bone-automap/BoneAutoMapping.test.ts+++ b/src/retarget/bone-automap/BoneAutoMapping.test.ts@@ -4,6 +4,7 @@ import { BoneChainResolver, type RawBoneRecord } from './BoneChainResolver'import { MixamoMapper } from './MixamoMapper'import { RigifyMapper } from './RigifyMapper'import { type BoneMetadata, BoneSlot } from './BoneTypes'+import { HumanChainConfig } from '../human-retargeting/HumanChainConfig'import {bone_names, daz_rig, mesh2motion_rig, mixamo_rig, unreal_rig, vrm_rig} from './rig-fixtures'@@ -271,6 +272,33 @@ describe('template rig detection', () => {expect(mappings.size).toBe(mixamo_rig().length)})+ it('builds retarget chains from the rig\'s real bone names, not the template\'s', () => {+ // Regression: the retargeter used to feed a detected Mixamo rig a hardcoded+ // chain config spelling every bone "mixamorigX". A rig exported without the+ // prefix resolved to no joints at all, and the empty pelvis chain crashed+ // Rig.buildRigScalar. The chains must come from the actual bone mapping.+ const source = BoneChainResolver.build_metadata(mesh2motion_rig())+ const target = BoneChainResolver.build_metadata(mixamo_rig(''))+ const mappings = MixamoMapper.map_mixamo_bones(source, target)++ const source_config = HumanChainConfig.build_custom_source_config(mappings)+ const target_config = HumanChainConfig.build_custom_target_config(source_config, mappings)++ expect(target_config.pelvis).toEqual(['Hips'])+ expect(target_config.armL).toEqual(['LeftArm', 'LeftForeArm', 'LeftHand'])+ expect(target_config.legR).toEqual(['RightUpLeg', 'RightLeg', 'RightFoot'])+ expect(target_config.spine).toEqual(['Spine', 'Spine1', 'Spine2'])++ // every configured bone must actually exist in the target skeleton+ const target_names = new Set<string>(bone_names(mixamo_rig('')))+ for (const chain of Object.values(target_config)) {+ for (const name of chain) {+ if (name === '') continue+ expect(target_names.has(name)).toBe(true)+ }+ }+ })+it('matches Rigify bones across exporter separator variations', () => {const source = BoneChainResolver.build_metadata(mesh2motion_rig())diff --git a/src/retarget/human-retargeting/Rig.ts b/src/retarget/human-retargeting/Rig.tsindex 7603b7f..4f1c266 100644--- a/src/retarget/human-retargeting/Rig.ts+++ b/src/retarget/human-retargeting/Rig.ts@@ -127,6 +127,15 @@ export class Rig {// it helps with calculations with hips where characters might have different heightsprivate buildRigScalar (chain_key: string): this {const ch: RigItem[] = this.chains[chain_key]++ // the chain can come back empty when none of its configured joint names exist in+ // this skeleton. Keep the default scalar rather than throwing - the retarget will+ // be poor, but a badly mapped rig should not take the whole preview down+ if (ch === undefined || ch.length === 0) {+ console.warn(`Rig.buildRigScalar: chain "${chain_key}" resolved to no joints, keeping default scalar.`)+ return this+ }+const j: Joint = this.tpose.joints[ch[0].idx]this.scalar = j.world.pos[1]return this