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

3

14

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?

Kamila answered 30/7, 2014 at 8:10 Comment(4)
I think you need to tell us more about how you are exporting these textures. Also, if preview can't even open the textures, then this sounds more like a issue with Blender than an issue with Scene Kit.Kazan
I have been looking around for a tutorial or explanation on how to export from Blender to SceneKit but haven't really found anything. This text is interesting, but old: iphonedevelopment.blogspot.jp/2012/08/… So far I have just tried to export from Blender to COLLADA, changing some of the export options regarding COLLADA - at first it didn't export the one texture file I was playing with but got it to do that later - it puts it in the same folder. However I just get the results as in my question.Kamila
The template "ship" if you start a scenekit game in Xcode is defined in "ship.dae", opening up the xml file reveals that Apple used "SceneKit Collada Exporter v1.0" to export it - that looks like a tool of their own. Makes you wonder from what tool/format they exported in the first place, maybe it's 3dsmax, Maya or something. Collada version is 1.4.1, same as Blender. I'll go ahead and look at what is different...Kamila
One more note: Apple's ship.dae has Y up, Blender has Z up. Maybe it breaks things.Kamila
H
26

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.

Hillinck answered 4/8, 2014 at 17:48 Comment(6)
I know I answered this for you on Blender Jonny but thought I would place it here too.Hillinck
Just made an update to show how to switch material after loading the scene.Hillinck
Sorry for the delay. This is the correct method. Somehow Blender does not write COLLADA in way that OSX/iOS can understand. Only after setting the texture manually in the Xcode 3d editor and saving, the .dae file can be opened in Preview and used with SceneKit. Thanks. With enough time on hand, one could compare the COLLADA format written by Xcode and Blender. Then maybe write a plugin or similar for Blender, allowing Blender to write SceneKit compatible COLLADA in the first place - this would be a huge time saver because now you have to manually select those textures in Xcode every time.Kamila
How can we change the identifier from "Geo" to others.Crofton
I haven't had luck changing from within scenekit. The name is pulled in from your collada. So just rename your geometry from within the 3d program you are using before exporting.Hillinck
@Hillinck How we can put 3d models in server and get url.Please guide normal 3d models put in server then i am not loading objects. ocation:Optional(file:///private/var/mobile/Containers/Data/Application/A1B996D7-ABE9-4000-91DB-2370076198D5/tmp/CFNetworkDownload_duDlwf.tmp) new location:/var/mobile/Containers/Data/Application/A1B996D7-ABE9-4000-91DB-2370076198D5/Documents/mug.scn This error occured.Insurmountable
I
4

Collada files don't embed textures, they only have references to them. Make sure that your textures are reachable from the collada file when you open it in Preview and make sure to include the textures in the app bundle when building an app.

Isometric answered 30/7, 2014 at 8:44 Comment(0)
V
4

3 things I had to do to make it work.

  1. Make sure images are packed in the .blend file (this is an option in UV editor)

  2. when you export, the file will not automatically include the uv's or the materials. there is a checkbox to include the UV's and Materials in the DAE. I missed this as well when doing it.

  3. This one you only need if your putting in a playground. The dae isn't quite applized, but i was able to use their scntool in the developer tools to export a dae that is. here is a sample command line that i used.

./scntool --convert ~/Documents/Art/BlenderArt/tableandappleUV.dae --format c3d --output ~/Documents/Table5.dae

notice the c3d format. use that and your playground can now also recognize the dae. (One other note: if you want to used the dae in the playground you need to put in resources folder of the playground)

Vocoid answered 10/11, 2014 at 20:6 Comment(3)
One more side note, packing the image puts in the full path of the UV asset. If you save them externally i found that it will used the UV image if place in same relative path.Vocoid
Where is documentation on how to use scntool?Dinge
in blender 2.79, this was needed "there is a checkbox to include the UV's and Materials in the DA"Faiyum

© 2022 - 2024 — McMap. All rights reserved.