Skip to content

Commit 48983ff

Browse files
authored
convert animation mixer animation clip references to weak_ptrs so updating from scene will take effect. (#8393)
1 parent a74e731 commit 48983ff

File tree

5 files changed

+83
-33
lines changed

5 files changed

+83
-33
lines changed

addons/ofxAssimp/src/ofxAssimpAnimationMixer.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using namespace ofxAssimp;
66

77
//--------------------------------------------------------------
88
bool AnimationClip::shouldRemove( const AnimationClip& ac ) {
9-
return ac.bRemove;
9+
return (ac.animationWeak.expired() || ac.bRemove);
1010
}
1111

1212
//--------------------------------------------------------------
@@ -31,8 +31,11 @@ AnimationClip& AnimationMixer::getAnimationClip(const std::string& aname) {
3131
return dummy;
3232
}
3333
for( auto& anim : mAnimationClips ) {
34-
if( anim.animation.getName() == aname ) {
35-
return anim;
34+
if(anim.animationWeak.expired()) {continue;}
35+
if( auto alock = anim.animationWeak.lock() ) {
36+
if( alock->getName() == aname ) {
37+
return anim;
38+
}
3639
}
3740
}
3841
ofLogWarning("ofxAssimp::AnimationMixer::getAnimationClip") << " could not find clip " << aname;
@@ -57,9 +60,12 @@ bool AnimationMixer::remove( int aindex ) {
5760
bool AnimationMixer::remove( const std::string& aname ) {
5861
int tindex = -1;
5962
for(int i = 0; i < (int)mAnimationClips.size(); i++) {
60-
if( mAnimationClips[i].animation.getName() == aname ) {
61-
tindex = i;
62-
break;
63+
if(mAnimationClips[i].animationWeak.expired()) {continue;}
64+
if( auto alock = mAnimationClips[i].animationWeak.lock() ) {
65+
if( alock->getName() == aname ) {
66+
tindex = i;
67+
break;
68+
}
6369
}
6470
}
6571
if( tindex > -1 ) {
@@ -76,22 +82,31 @@ void AnimationMixer::removeAll() {
7682
//--------------------------------------------------------------
7783
void AnimationMixer::playAll() {
7884
for( auto& anim : mAnimationClips ) {
79-
anim.animation.play();
85+
if(anim.animationWeak.expired()) {continue;}
86+
if( auto alock = anim.animationWeak.lock() ) {
87+
alock->play();
88+
}
8089
}
8190
}
8291

8392
//--------------------------------------------------------------
8493
void AnimationMixer::stopAll() {
8594
for( auto& anim : mAnimationClips ) {
86-
anim.animation.stop();
95+
if(anim.animationWeak.expired()) {continue;}
96+
if( auto alock = anim.animationWeak.lock() ) {
97+
alock->stop();
98+
}
8799
}
88100
}
89101

90102
//--------------------------------------------------------------
91103
void AnimationMixer::update(float aElapsedTimef) {
92104
for(auto& anim : mAnimationClips ) {
93105
// std::cout << "AnimationMixer :: update : " << anim.animation.getName() << " position: " << anim.animation.getPositionInSeconds() << std::endl;
94-
anim.animation.update();
106+
if(anim.animationWeak.expired()) {continue;}
107+
if( auto alock = anim.animationWeak.lock() ) {
108+
alock->update();
109+
}
95110
}
96111

97112
float tdelta = std::max( std::min(aElapsedTimef-mLastUpdateTime, 10.f), ai_epsilon);
@@ -166,9 +181,13 @@ void AnimationMixer::transition( const std::string& aname, float aduration, bool
166181
// first get the targeted animation //
167182
int animIndex = -1;
168183
for( int i = 0; i < (int)mAnimationClips.size(); i++ ) {
169-
if( mAnimationClips[i].animation.getName() == aname ) {
170-
animIndex = i;
171-
break;
184+
// if( mAnimationClips[i].animation.getName() == aname ) {
185+
if(mAnimationClips[i].animationWeak.expired()) {continue;}
186+
if( auto alock = mAnimationClips[i].animationWeak.lock() ) {
187+
if( alock->getName() == aname ) {
188+
animIndex = i;
189+
break;
190+
}
172191
}
173192
}
174193
if( animIndex < 0 ) {

addons/ofxAssimp/src/ofxAssimpAnimationMixer.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class AnimationClip {
1212
static bool shouldRemove( const AnimationClip& ac );
1313

1414
AnimationClip() {}
15-
AnimationClip(Animation anim, float aweight) {
16-
animation = anim;
15+
AnimationClip(std::shared_ptr<Animation> anim, float aweight) {
16+
animationWeak = anim;
1717
weight = aweight;
1818
}
1919
// AnimationClip(const Animation& anim, float aweight, bool abOneShot) {
@@ -39,8 +39,15 @@ class AnimationClip {
3939

4040
void tagForRemoval() {bRemove=true;}
4141

42+
void resetAnimation() {
43+
if( auto alock = animationWeak.lock() ) {
44+
alock->reset();
45+
}
46+
}
47+
4248
float weight = 1.0f;
43-
Animation animation;
49+
// Animation animation;
50+
std::weak_ptr<Animation> animationWeak;
4451

4552
//protected:
4653
float mTargetWeight = -1.f;

addons/ofxAssimp/src/ofxAssimpNode.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ void Node::update( const std::shared_ptr<ofxAssimp::AnimationMixer>& aAnimMixer
5050
bool bHasSomeKeys = false;
5151
int clipWithKeysIndex = 0;
5252
for( auto& animClip : mixer->getAnimationClips() ) {
53-
auto& keyCollection = mSrcNode->getKeyCollection(animClip.animation.getUid());
53+
54+
auto animation = animClip.animationWeak.lock();
55+
if( !animation ) {continue;}
56+
57+
auto& keyCollection = mSrcNode->getKeyCollection(animation->getUid());
5458

5559
if( keyCollection.hasKeys() ) {
56-
auto animTime = animClip.animation.getPositionInTicks() + animClip.animation.getStartTick();
60+
auto animTime = animation->getPositionInTicks() + animation->getStartTick();
5761
tempPos += animClip.weight * keyCollection.getPosition( animTime );
5862
tempScale += animClip.weight * keyCollection.getScale( animTime );
5963
// TODO: Keep an eye on this :O

addons/ofxAssimp/src/ofxAssimpScene.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,17 @@ bool Scene::processScene() {
7979

8080
if(mSrcScene){
8181
if( mSrcScene->getImportSettings().importAnimations ) {
82-
mAnimations = mSrcScene->getAnimations();
82+
mAnimations.clear();
83+
// mAnimations = mSrcScene->getAnimations();
84+
auto& srcAnims = mSrcScene->getAnimations();
85+
for( auto& srcA : srcAnims ) {
86+
auto nanim = std::make_shared<ofxAssimp::Animation>( srcA );
87+
mAnimations.push_back(nanim);
88+
}
8389
if(mAnimations.size() > 0 ) {
84-
// add a default mixer
85-
mAnimMixer = std::make_shared<AnimationMixer>();
86-
mAnimMixer->add( AnimationClip( mAnimations[0], 1.0f ));
87-
mAnimMixer->getAnimationClip(0).animation.play();
90+
mAnimationIndex = -1; // set to -1 so it will trigger playing
91+
setCurrentAnimation(0);
92+
getCurrentAnimation().play();
8893
}
8994
}
9095

@@ -354,6 +359,13 @@ void Scene::flagSceneDirty() {
354359
mBSceneDirty = true;
355360
}
356361

362+
std::shared_ptr<AnimationMixer> Scene::_getAnimationMixer() {
363+
if( !mAnimMixer ) {
364+
mAnimMixer = std::make_shared<AnimationMixer>();
365+
}
366+
return mAnimMixer;
367+
}
368+
357369
void Scene::updateAnimations() {
358370
if( mAnimMixer ) {
359371
mAnimMixer->update(ofGetElapsedTimef());
@@ -480,7 +492,10 @@ ofxAssimp::Animation& Scene::getCurrentAnimation() {
480492
return dummyAnimation;
481493
}
482494
if( mAnimMixer && mAnimMixer->getNumAnimationClips() > 0 ) {
483-
return mAnimMixer->getAnimationClips().back().animation;
495+
// return mAnimMixer->getAnimationClips().back().animation;
496+
if( auto alock = mAnimMixer->getAnimationClips().back().animationWeak.lock() ) {
497+
return *alock;
498+
}
484499
}
485500
ofLogWarning("ofxAssimp::Scene::getCurrentAnimation") << " does not have current animation!";
486501
return dummyAnimation;
@@ -495,10 +510,11 @@ bool Scene::setCurrentAnimation( int aindex ) {
495510
ofLogWarning("ofxAssimp::Scene::setAnimation") << " already have this as the index!";
496511
return false;
497512
}
513+
_getAnimationMixer();
498514
mAnimationIndex = ofClamp(aindex, 0, mAnimations.size()-1);
499515
mAnimMixer->removeAll();
500516
mAnimMixer->add( AnimationClip( mAnimations[mAnimationIndex], 1.0f ));
501-
mAnimMixer->getAnimationClips().back().animation.reset();
517+
mAnimMixer->getAnimationClips().back().resetAnimation();
502518
return true;
503519
}
504520

@@ -516,10 +532,11 @@ bool Scene::transitionCurrentAnimation( int aTargetAnimIndex, float aduration )
516532
ofLogWarning("ofxAssimp::Scene::setAnimation") << " no animations!!";
517533
return false;
518534
}
535+
_getAnimationMixer();
519536
mAnimationIndex = ofClamp(aTargetAnimIndex, 0, mAnimations.size()-1);
520537
mAnimMixer->add( AnimationClip( mAnimations[mAnimationIndex], 1.f-mAnimMixer->getTotalClipWeights() ));
521-
mAnimMixer->transition(mAnimations[mAnimationIndex].getName(), aduration );
522-
mAnimMixer->getAnimationClips().back().animation.reset();
538+
mAnimMixer->transition(mAnimations[mAnimationIndex]->getName(), aduration );
539+
mAnimMixer->getAnimationClips().back().resetAnimation();
523540
return true;
524541
}
525542

@@ -539,7 +556,7 @@ bool Scene::hasAnimation( const std::string& aname ) {
539556
int Scene::getAnimationIndex( const std::string& aname ) {
540557
int tindex = -1;
541558
for( int i = 0; i < (int)mAnimations.size(); i++ ) {
542-
if( mAnimations[i].getName() == aname ) {
559+
if( mAnimations[i] && mAnimations[i]->getName() == aname ) {
543560
tindex = i;
544561
break;
545562
}
@@ -553,7 +570,7 @@ ofxAssimp::Animation& Scene::getAnimation(int aindex) {
553570
return dummyAnimation;
554571
}
555572
aindex = ofClamp(aindex, 0, mAnimations.size()-1);
556-
return mAnimations[aindex];
573+
return *mAnimations[aindex];
557574
}
558575

559576
ofxAssimp::Animation& Scene::getAnimation(const std::string& aname) {
@@ -579,10 +596,11 @@ bool Scene::addAnimation( int aSrcAnimIndex, const std::string& aNewAnimName, fl
579596
return false;
580597
}
581598
auto& canim = mAnimations[aSrcAnimIndex];
582-
ofxAssimp::Animation nanim = mAnimations[aSrcAnimIndex];
583-
nanim.setup(aStartTick, aEndTick);
584-
nanim.setName(aNewAnimName);
585-
nanim.setLoopType(aLoopType);
599+
// ofxAssimp::Animation nanim = mAnimations[aSrcAnimIndex];
600+
auto nanim = std::make_shared<ofxAssimp::Animation>( *mAnimations[aSrcAnimIndex] );
601+
nanim->setup(aStartTick, aEndTick);
602+
nanim->setName(aNewAnimName);
603+
nanim->setLoopType(aLoopType);
586604
mAnimations.push_back(nanim);
587605
return true;
588606
}

addons/ofxAssimp/src/ofxAssimpScene.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ class Scene : public ofxAssimp::Node {
273273

274274

275275
protected:
276+
std::shared_ptr<AnimationMixer> _getAnimationMixer();
276277
void updateAnimations();
277278
void updateMeshesFromBones();
278279

@@ -292,7 +293,8 @@ class Scene : public ofxAssimp::Node {
292293
// the skeletons are the root bones
293294
std::vector< std::shared_ptr<ofxAssimp::Skeleton> > mSkeletons;
294295
std::vector< std::shared_ptr<ofxAssimp::Node> > mNullNodes;
295-
std::vector<ofxAssimp::Animation> mAnimations;
296+
// std::vector<ofxAssimp::Animation> mAnimations;
297+
std::vector< std::shared_ptr<ofxAssimp::Animation> > mAnimations;
296298

297299
int mAnimationIndex = 0;
298300

0 commit comments

Comments
 (0)