Skip to content

Commit a7f83e2

Browse files
committed
fix Animator component initialization
1 parent 86c87c6 commit a7f83e2

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

packages/@dcl/inspector/src/components/EntityInspector/AnimatorInspector/AnimatorInspector.tsx

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,59 @@ export default withSdk<Props>(({ sdk, entity: entityId }) => {
2323
const entity = sdk.sceneContext.getEntityOrNull(entityId)
2424
const hasAnimator = useHasComponent(entityId, Animator)
2525
const [componentValue, setComponentValue, isComponentEqual] = useComponentValue<PBAnimator>(entityId, Animator)
26+
const [gltfValue] = useComponentValue(entityId, GltfContainer)
2627

2728
const [states, _, updateStates, _2, setStates] = useArrayState<PBAnimationState>(
2829
componentValue === null ? [] : componentValue.states
2930
)
3031

3132
useEffect(() => {
32-
if (isComponentEqual({ states })) return
33-
setComponentValue({ states })
34-
}, [states])
33+
if (!entity || !gltfValue || hasAnimator) return
34+
35+
const initializeComponent = async () => {
36+
try {
37+
await initializeAnimatorComponent(sdk, entityId, [])
38+
} catch (error) {
39+
// eslint-disable-next-line no-console
40+
console.warn('Failed to initialize animator component:', error)
41+
}
42+
}
43+
44+
void initializeComponent()
45+
}, [entity, gltfValue, hasAnimator])
46+
47+
useEffect(() => {
48+
if (!entity || !gltfValue || !hasAnimator) return
49+
50+
const loadAnimations = async () => {
51+
try {
52+
const { animationGroups } = await entity.onGltfContainerLoaded()
53+
if (animationGroups.length && (!states.length || states[0].clip !== animationGroups[0].name)) {
54+
const newStates = animationGroups.map(($) => ({
55+
clip: $.name,
56+
playing: false,
57+
weight: 1,
58+
speed: 1,
59+
loop: false,
60+
shouldReset: false
61+
}))
62+
setStates(newStates)
63+
}
64+
} catch (error) {
65+
// eslint-disable-next-line no-console
66+
console.warn('Failed to load animations:', error)
67+
}
68+
}
69+
70+
void loadAnimations()
71+
}, [entity, gltfValue, hasAnimator, states])
3572

3673
useEffect(() => {
37-
if (entity) {
38-
entity
39-
.onGltfContainerLoaded()
40-
.then(async ({ animationGroups }) => {
41-
if (animationGroups.length) {
42-
const { states } = await initializeAnimatorComponent(sdk, entityId, animationGroups)
43-
setStates(states)
44-
}
45-
})
46-
.catch(() => {})
74+
if (isComponentEqual({ states })) {
75+
return
4776
}
48-
}, [entity])
77+
setComponentValue({ states })
78+
}, [states])
4979

5080
const handleRemove = useCallback(async () => {
5181
sdk.operations.removeComponent(entityId, Animator)

packages/@dcl/inspector/src/components/EntityInspector/AnimatorInspector/utils.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ export async function initializeAnimatorComponent(
3636
}))
3737

3838
const value: PBAnimator = { states }
39-
sdk.operations.addComponent(entity, Animator.componentId)
40-
sdk.operations.updateValue(Animator, entity, value)
41-
await sdk.operations.dispatch()
39+
40+
try {
41+
sdk.operations.addComponent(entity, Animator.componentId)
42+
sdk.operations.updateValue(Animator, entity, value)
43+
await sdk.operations.dispatch()
44+
} catch (error) {
45+
console.warn('Failed to initialize animator component:', error)
46+
throw error
47+
}
4248

4349
return value
4450
}

0 commit comments

Comments
 (0)