3

I've made 3d model with blender and I want to use it in scene kit.

var platform = SCNNode();
let dae = SCNScene(named:"platform.dae");
platform = dae!.rootNode;
platform.position = SCNVector3(x: 0,y: 0,z:0);
if platform.geometry == nil {
    print("geo is nil.")
}
platform.geometry?.firstMaterial?.diffuse.contents = "wood.png";
platform.geometry?.firstMaterial?.diffuse.wrapS = SCNWrapMode.Repeat
platform.geometry?.firstMaterial?.diffuse.wrapT = SCNWrapMode.Repeat
platform.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.Static, shape: SCNPhysicsShape(node: platform, options: nil));
scene.rootNode.addChildNode(platform);

It always prints out "geo is nil". So I can't make a physics body for the model. Can anyone tell me why does this happen?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
white5tone
  • 166
  • 8
  • Can you view platform.dae in Preview? Do you have it at root level in your app (as your call implies) or in an scnassets folder (need the name of that folder in your load path). Also: drop "var platform = SCNNode()" in favor of "let platform = dae?.rootNode". And you don't need those semicolons. – Hal Mueller Jan 10 '16 at 03:26
  • dae is not in the scnassets folder but there is no problem with printing it out. I can even give actions to it but I can't change the material, or I can't make a physic body using its geometry. Because its geometry is always nil. – white5tone Jan 10 '16 at 08:43
  • Also, I tried to turn the dae into scn file. But after I do it, app crashes. I think dae!.rootNode returns nil. And yes I changed the initializing method as SCNScene(named:"platform.scn") – white5tone Jan 10 '16 at 08:51

1 Answers1

3

The DAE file, when loaded, returns a full node tree. If the Blender file has a light and a camera (which it almost certainly does), so does the DAE's node tree. The root SCNNode doesn't have a geometry (and there's no requirement that an SCNNode have one). But you can find the children that do.

print (platform)
platform.childNodes.map {
    print($0.name, $0.geometry)
}

produces

<SCNNode: 0x7fe008e938d0 | 3 children>
Optional("Camera") nil
Optional("Lamp") nil
Optional("Cube") Optional(<SCNGeometry: 0x7fe00ab08930 'Cube'>)

You can use childNodeWithName() or childNodesPassingTest() to pick out the object you want and add it to your scene.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42