title: Dividing MDLMesh into multiple SCNGeometry
body: <p>I have an <em>.obj</em> file containing a 3D model, divided into multiple smaller meshes, e.g. a front and a back.<br>
  My goal is to load this file into a SceneKit View and interact with the different parts of the model, to color them, select them, hide them, or move them individually.</p>
  
  <p>I was able to load the file into a <code>MDLAsset</code>, containing the <code>MDLMesh</code>, wich itself contains all of the sub meshes as <code>MDLSubmesh</code>.</p>
  
  <p>To display the loaded model I have to convert the <code>MDLMesh</code>to an <code>SCNGeometry</code>.<br>
  The basic approach is to call <code>SCNGeometry geometryWithMDLMesh:(MDLMesh *)mdlMesh</code>. This works fine, the <code>SCNGeometry</code>contains different <code>SCNGeometryElements</code>.</p>
  
  <p>However, a lot of information that was in the <code>MDLSubmesh</code>is lost when converting this to the Scene Kit Geometry and my ability to interact with the different sub meshes is very limited.</p>
  
  <p>It would be ideal if I convert all of the <code>MDLSubmesh</code> to <code>SCNGeometry</code> individually. I have tried two different approaches:</p>
  
  <ol>
  <li>I tried to use <code>[MDLMes newSubdividedMesh: aMesh submeshIndex: i subdivisionLevels:0]</code> for each of the sub meshes. Then I created <code>SCNGeometry</code> out of them.<br>
  The problem was that SceneKit didn't render the scene as aspected. The geometry was working but the light was not applied to the model, something that worked when I was converting the whole <code>MDLMesh</code>.</li>
  </ol>
  
  <p>Generate a new mesh from each submesh: </p>
  
  <pre><code>for (NSInteger i = 0; i &lt; [[mesh submeshes] count]; i++) {
      MDLMesh *submesh = [MDLMesh newSubdividedMesh:mesh submeshIndex:i subdivisionLevels:0];
      SCNGeometry *geometry = [SCNGeometry geometryWithMDLMesh:submesh];           
      SCNNode *subNode = [SCNNode nodeWithGeometry:geometry];          
      [node addChildNode:subNode];
  }
  </code></pre>
  
  <p>The resulting rendering with this approach <a href="https://i.stack.imgur.com/J6Ehj.png" rel="nofollow noreferrer">rendering with subdivided meshes</a> and when converting the whole <code>MDLMesh</code><a href="https://i.stack.imgur.com/Xmxdm.png" rel="nofollow noreferrer">rendering the whole mesh</a>. Notice the missing light effects on the first rendering with the above code.</p>
  
  <ol start="2">
  <li><p>The second approach was to generate the <code>SCNGeometry</code> with the <code>SCNGeometry geometryWithSources:elements:</code> method. Although I doubt this is the 'right' way to do this, here is what I tried.</p>
  
  <ul>
  <li><p>(SCNNode*) loadMDLMesh : (MDLMesh*) mesh withSubmeshes: (bool) sub {
  if (sub) {
      //Generate a scene kit node 
      SCNNode * node = [SCNNode node];</p>
  
  <pre><code>//Generate a new mesh from each submesh
  for (NSInteger i = 0; i &lt; [[mesh submeshes] count]; i++) {
      //Create the geometry element from the mdl sub mesh 
      SCNGeometryElement *element = [SCNGeometryElement geometryElementWithMDLSubmesh: [[mesh submeshes] objectAtIndex:i] ];
  
      //Create a geometry source from the index buffer
      MDLMeshBufferMap *map = [[[[mesh submeshes] objectAtIndex:i] indexBuffer] map];
  
      SCNGeometrySource *source = [SCNGeometrySource geometrySourceWithVertices:[map bytes] count:[[[mesh submeshes] objectAtIndex:i] indexCount]];
  
      //Create the SCNGeometry from the source and the element
      SCNGeometry *subMesh = [SCNGeometry geometryWithSources:[NSArray arrayWithObject:source] elements:[NSArray arrayWithObject:element]];
  
      //Update the name
      subMesh.name = [[[mesh submeshes] objectAtIndex:i] name];
  
      //Create a subnode and add it to the object node
      SCNNode *subNode = [SCNNode nodeWithGeometry:subMesh];
  
      [node addChildNode:subNode];
  }       
  
  return node;
  
  } else {
  return [SCNNode nodeWithMDLObject:mesh];
  }
  </code></pre></li>
  </ul></li>
  </ol>
  
  <p>Unfortunately, the app crashes with an Bad Access Exception.</p>
  
  <p>As one can see, I am not that experienced with developing objective-c. Any help to fix my ideas or different approaches to subdivide the mesh would be great.</p>
  
  <p>Thank you.</p>