1

It's possible to open the OBJ file below and change the material colors from within the Xcode SceneKit Editor.

However, the documentation isn't clear how to access this same list of materials from within code, and change the colors programmatically. (See attachment.)

func enumerateChildObjects(of: AnyClass, root: MDLObject, using: (MDLObject, UnsafeMutablePointer<ObjCBool>) -> Void, stopPointer: UnsafeMutablePointer<ObjCBool>) seems like it might help, but it's not returning the same list of materials.

Code to load OBJ file into SceneKit:

    let modelPath = "model.obj"
    let url = NSURL(string: modelPath)

    let scene = SCNScene(named: modelPath)!
    sceneView.autoenablesDefaultLighting = true
    sceneView.allowsCameraControl = true
    sceneView.scene = scene
    sceneView.backgroundColor = UIColor.white

List of materials on right side (Xcode screenshot):

enter image description here

Download OBJ file (click Download link): https://poly.google.com/view/cKryD9VnDEZ

Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • You can use enumerateChildObjects as nodes are reference type so u don't. Need it back in return. If you change node.firstMaterial.diffuse.content = UIColor.red will helpful. Also you can print node.name so identify node – Prashant Tukadiya Jan 20 '18 at 04:15
  • I didn't try personally but select .obj file goto editor menu in xcode and there may be option to convert it in .scn file – Prashant Tukadiya Jan 20 '18 at 04:17

1 Answers1

1

The SCNScene in which you load the OBJ file contains a .rootNode property, which in turn has a childNodes property that contain the SCNNodes in the scene/obj file. Each SCNNode has a .geometry property that contains a SCNGeometry that describes the model’s vertices. A SCNGeometry also has a .materials property. This is an array of SCNMaterials you can simply loop through.

For example, if there is only one node in the scene loaded from obj, then the mat23 material is scene.rootNode.childNodes.firstObject.geometry.materials.firstMaterial or scene.rootNode.childNodes.firstObject.geometry.materials[0]

Usually when you load a single object from an OBJ you assign the firstObject to a SCNNode, which you then add as a child to the rootNode of the scene of the SCNView. (Rather than assigning the entire scene from the obj).

In short, set up a scene programmatically, configure it and assign it to the SCNView. Don’t use the scene you create to load the OBJ file, but grab the first child node from that scene’s rootnode, and add that to your scene (and for example and array of Monsters). Each node you add has a geometry property which in turn has a Materials property (https://developer.apple.com/documentation/scenekit/scngeometry/1523472-materials ) containing the materials assigned to that node specifically.

So you don’t actually access the colors of the obj (nor its mtl file), you instead access the SCNMaterials Xcode created for each SCNNode.

Xartec
  • 2,369
  • 11
  • 22
  • Thanks so much! Part of the backpack is missing from the OBJ file in question. Does this mean we did something wrong? Also how do you recommend fixing the lighting to match what Google Poly has? The default ambient lighting is far too harsh. – Crashalot Jan 20 '18 at 21:57
  • Unfortunately no it doesn't mean you did something wrong. There are several bugs in the importing of OBJ files, including issues with normals and incorrect topology. If you can, convert the file to DAE instead, or triangulate it. – Xartec Jan 20 '18 at 22:21
  • Thanks! Any suggestions about the lighting? I can send screenshots of the current lighting if it makes it simpler. You can see the Google lighting here: poly.google.com/view/cKryD9VnDEZ – Crashalot Jan 20 '18 at 22:44
  • Lighting is a whole different topic by itself, if you have any questions that I suggest creating a new question. That said, you probably want to try and change the specular and the shininess values of the material. – Xartec Jan 21 '18 at 00:50
  • OK, thanks. Posted a new question here: https://stackoverflow.com/questions/48362672/scenekit-how-to-recreate-lighting-from-google-poly-for-same-obj-file – Crashalot Jan 21 '18 at 01:00