@@ -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 )
0 commit comments