Including textures when exporting from Blender to COLLADA/.dae format for use in SceneKit

Jonny picture Jonny · Jul 30, 2014 · Viewed 12k times · Source

I am able to export meshes created in Blender for use in SceneKit by using the COLLADA/.dae format - however no textures show up on iOS device.

Also, Preview on OS X won't open any COLLADA file exported from Blender - yet the sidebar preview does show the mesh. What are the options needed at export to make this work?

Answer

FlippinFun picture FlippinFun · Aug 4, 2014

Create a scnassets folder first (not required but useful)

  1. Create a folder on your desktop and give it an extension of ".scnassets"

  2. Put your collada(.dae) file along with any textures you will be using in it.

  3. Drag the folder into your project and save as copy like usual.

  4. Click on your scnassets folder and you will see a checked box (if not check it) for converting to a "y" up axis.

Assigning textures to your scene model

  1. Click on your collada(.dae) file inside your project in xcode.

  2. You should see your scene and on its left side a list of cameras, lights, materials, and such. This is your scene tree.

  3. Open the materials tab and click on one of your materials.

  4. On the right hand side in the inspection window click on the blue ball shaped icon(Material inspector) to view the diffuse, specularity, and such of that one material.

  5. Click on the diffuse tab and when it opens you should have an option of colors and your textures within your project. Select the texture you used on your model within 3d program. As long as you UV unwrapped them properly in your 3D program, they should apply instantly within your scene view.

What if I want to change my material after loading my scene? Glad you asked!

To do this we must use entryWithIdentifier method of SCNSceneSource class. I am going to use swift here because it is awesome! Here we go...

  1. Get the url of your scene(.dae) like so...

    let url = NSBundle.mainBundle().URLForResource("YourFolder.scnassets/yourScene", withExtension "dae")
    
  2. Now lets put that url to use...

    let source = SCNSceneSource(URL: url, options: nil)
    
  3. Click on your .dae and under Scene Graph is list of items one of which is your geometry. It will have a tee kettle just to the right of it signifying so. We are going to use its name here. Let say your geometry's name is Geo. Use it like so...

    let yourGeometry = source.entryWithIdentifier("Geo", withClass: SCNGeometry.self) as SCNGeometry
    
  4. Now we have a source attatched to a SCNNode called yourGeomerty. Lets create a new material with a UIColor like so...

    let newMaterial = SCNMaterial()
    newMaterial.diffuse.contents = UIColor.redColor()
    
  5. Finally we will switch out the old material with the newMaterial like so...

    yourGeometry.geometry.replaceMaterialAtIndex(0, withMaterial: newMaterial)
    

Play around with it and other indexes if you have more than one material. You can also use the UIImage class to use another texture instead of a color.

TIP If you want to add to or delete something from your scnassets folder, navigate to your project folder to do so and not xcode. This took me a while to figure out so thought I would save everyone the trouble.