2
votes

I have a SceneKit SCNSceneRendererDelegate that implement the following method :

- (void)    renderer:(id<SCNSceneRenderer>)renderer
      didRenderScene:(SCNScene*)scene
              atTime:(NSTimeInterval)time
{
    static  NSUInteger  counter = 0 ;

    counter++ ;


    //  Rotate via animation
    if (counter==1)
    {
        SCNNode *man                = [scene.rootNode childNodeWithName:@"Man" recursively:YES] ;
        SCNNode *headBone           = [man childNodeWithName:@"mixamorig_Head" recursively:YES] ;

        CABasicAnimation    *animation1  = [CABasicAnimation animationWithKeyPath:@"rotation"] ;
        animation1.fromValue = [NSValue valueWithSCNVector4:headBone.rotation] ;
        animation1.toValue   = [NSValue valueWithSCNVector4:SCNVector4Make(0,1,0,M_PI_4)] ;
        animation1.duration = 1.2 ;
        animation1.fillMode = kCAFillModeForwards ;
        animation1.removedOnCompletion   = NO ;
        [headBone addAnimation:animation1 forKey:@"rotationAnimation"] ;
    }
}

Obviously, this is just rotating a bone named "mixamorig_Head" by 45 degrees.

When I run the program, I can actually see the head rotating... only one time out of 2.

The only thing I do is click on the run button of XCode. The program starts and show the scene, with the head rotating... or not. There is no code update in between runs, just click on the run button.

I have tried to log every variable, and everything seems correct.

Any idea why SceneKit behaviour could be different from one run to another ?

Edit Upload a demo project here.

1
In the case where the head is not rotating, does the head appear to have already rotated, or does it appear to have not rotated at all?Aaron Golden
It does not move at all and remains in its start position.AirXygène
I wonder if the scene is somehow not fully initialized sometimes... you say you've logged just about everything---have you confirmed that man and headBone are not nil in the case where the rotation does not happen?Aaron Golden
Yes, I did confirm that (and also that animation1 is not nil). I am uploading the project to airxygene.fr/transfer/3DTests.zip if you are interested. This is a simple "build the scene, add the animation" test. If you build and run it several times, you should see both behaviour without changing any code. I am using XCode 8.3 (8E162) and macOS Sierra.AirXygène
I think your headBone already has an animation attached. Calling [headBone removeAllAnimations]; in awakeFromNib seems to make the animation work every time for me.James P

1 Answers

1
votes

I looked at the DAE file and noticed that all the bones have animations attached. This seems to be a problem when adding new animations, and they don't always work. (No idea why it works sometimes and not others).

You can either delete all the animations in the SceneKit Editor, or remove them programmatically from the nodes you want to animate:

[headBone removeAllAnimations];