From a7f83e24577e9e917b99d493cbb8fdd9b8fad26d Mon Sep 17 00:00:00 2001 From: Nicolas Echezarreta Date: Wed, 5 Feb 2025 16:14:37 -0300 Subject: [PATCH] fix Animator component initialization --- .../AnimatorInspector/AnimatorInspector.tsx | 58 ++++++++++++++----- .../AnimatorInspector/utils.ts | 12 +++- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/packages/@dcl/inspector/src/components/EntityInspector/AnimatorInspector/AnimatorInspector.tsx b/packages/@dcl/inspector/src/components/EntityInspector/AnimatorInspector/AnimatorInspector.tsx index 90c866f14..378866e55 100644 --- a/packages/@dcl/inspector/src/components/EntityInspector/AnimatorInspector/AnimatorInspector.tsx +++ b/packages/@dcl/inspector/src/components/EntityInspector/AnimatorInspector/AnimatorInspector.tsx @@ -23,29 +23,59 @@ export default withSdk(({ sdk, entity: entityId }) => { const entity = sdk.sceneContext.getEntityOrNull(entityId) const hasAnimator = useHasComponent(entityId, Animator) const [componentValue, setComponentValue, isComponentEqual] = useComponentValue(entityId, Animator) + const [gltfValue] = useComponentValue(entityId, GltfContainer) const [states, _, updateStates, _2, setStates] = useArrayState( componentValue === null ? [] : componentValue.states ) useEffect(() => { - if (isComponentEqual({ states })) return - setComponentValue({ states }) - }, [states]) + if (!entity || !gltfValue || hasAnimator) return + + const initializeComponent = async () => { + try { + await initializeAnimatorComponent(sdk, entityId, []) + } catch (error) { + // eslint-disable-next-line no-console + console.warn('Failed to initialize animator component:', error) + } + } + + void initializeComponent() + }, [entity, gltfValue, hasAnimator]) + + useEffect(() => { + if (!entity || !gltfValue || !hasAnimator) return + + const loadAnimations = async () => { + try { + const { animationGroups } = await entity.onGltfContainerLoaded() + if (animationGroups.length && (!states.length || states[0].clip !== animationGroups[0].name)) { + const newStates = animationGroups.map(($) => ({ + clip: $.name, + playing: false, + weight: 1, + speed: 1, + loop: false, + shouldReset: false + })) + setStates(newStates) + } + } catch (error) { + // eslint-disable-next-line no-console + console.warn('Failed to load animations:', error) + } + } + + void loadAnimations() + }, [entity, gltfValue, hasAnimator, states]) useEffect(() => { - if (entity) { - entity - .onGltfContainerLoaded() - .then(async ({ animationGroups }) => { - if (animationGroups.length) { - const { states } = await initializeAnimatorComponent(sdk, entityId, animationGroups) - setStates(states) - } - }) - .catch(() => {}) + if (isComponentEqual({ states })) { + return } - }, [entity]) + setComponentValue({ states }) + }, [states]) const handleRemove = useCallback(async () => { sdk.operations.removeComponent(entityId, Animator) diff --git a/packages/@dcl/inspector/src/components/EntityInspector/AnimatorInspector/utils.ts b/packages/@dcl/inspector/src/components/EntityInspector/AnimatorInspector/utils.ts index ee7340edb..1ecad6461 100644 --- a/packages/@dcl/inspector/src/components/EntityInspector/AnimatorInspector/utils.ts +++ b/packages/@dcl/inspector/src/components/EntityInspector/AnimatorInspector/utils.ts @@ -36,9 +36,15 @@ export async function initializeAnimatorComponent( })) const value: PBAnimator = { states } - sdk.operations.addComponent(entity, Animator.componentId) - sdk.operations.updateValue(Animator, entity, value) - await sdk.operations.dispatch() + + try { + sdk.operations.addComponent(entity, Animator.componentId) + sdk.operations.updateValue(Animator, entity, value) + await sdk.operations.dispatch() + } catch (error) { + console.warn('Failed to initialize animator component:', error) + throw error + } return value }