Codermonk Codermonk - 1 month ago 18x
iOS Question

Transform and Rotate in Scenekit

I have a SCN Node that I have Transformed to a desired Rotation and Angle.

I now want to rotate it infinitely on its new x-axis, but I can't seem to get it to rotate 360 degrees, it just shifts slightly.

let spin = CABasicAnimation(keyPath: "rotation")
spin.fromValue = NSValue(SCNVector4: SCNVector4(x: item.rotation.x, y: item.rotation.y, z: item.rotation.z, w: item.rotation.w))
spin.toValue = NSValue(SCNVector4: SCNVector4(x: Float(2*M_PI), y: item.rotation.y, z: item.rotation.z, w: item.rotation.w))
spin.duration = 3
spin.repeatCount = .infinity
item.addAnimation(spin, forKey: "spin around")

Note I found the rotation code from an earlier question, and it works as expected, but not if I want to rotate on the transformed node.

EDIT: The Transform Code:

self.item.transform = SCNMatrix4Mult(SCNMatrix4Mult(SCNMatrix4MakeScale(1.1, 1.1, 1.1), SCNMatrix4MakeRotation(2.2, 228.0, 120.0, 55.2)), SCNMatrix4MakeTranslation(-2.5, 12, 6))
self.item.pivot = SCNMatrix4MakeTranslation(0, 0, 4)
self.item.transform = SCNMatrix4Mult(SCNMatrix4MakeTranslation(0, 0, 4), item.transform)


In answer to your comment and possibly your issue. Once you have your .dae file installed, you can access the nodes as in the following example:

    // "rhodopsin" is the namne of a .dae file
    guard let rho = SCNScene(named: "rhodopsin") else {
        print("Couldn't find molecule in dictionary  (rhodopsin)")
        return  }

    let chain = rho.rootNode.childNodeWithName("chain", recursively: true)!
    let hetero = rho.rootNode.childNodeWithName("hetero", recursively: true)!

If you need to find the names of these nodes, open the .dae file in the editor window and click on the little sidebar icon lower left. In that side bar is the Scene graph (pic below). You can not only witness the hierarchy, you can rename and, if necessary, move nodes around and reparent (careful). You may need to add other safeguards against the file not being found. I'm using this to build archives, not client-side.

To continue the example:

    // (baseNode is a child of the current scene's root node) 

These now behave just like nodes you've created within SceneKit. In my example, if chain had some tweaked orientation that prevented an easy animation, say about the world's y-axis, could rotate baseNode instead. Or if it needed rotation separate from hetero I would instead put it in a new, otherwise empty node and rotate that.

    let panNode = SCNNode()
    // perform rotations on panNode or baseNode

Where to get node info from a dae file.