Scrape 3D model from Sketchfab

Alec Jacobson

October 16, 2013

weblog/

SketchFab has many 3D models, like this bulldog. Open up their webgl viewer and extract the following Javascript variables to recover the model vertices and faces.

// Copy result of this:
view3D._scene.children[2].children[0].children[0].children[0].children[0].children[0].children[0].children[1].attributes.Vertex._elements
// to U


for each primitive p
  // Copy result of this
  view3D._scene.children[2].children[0].children[0].children[0].children[0].children[0].children[0].children[1].primitives[p].indices._elements
  // to P{p+1}.indices, and 
  view3D._scene.children[2].children[0].children[0].children[0].children[0].children[0].children[0].children[1].primitives[p].mode // 5 means TRIANGLE_STRIP, 4 means TRIANGLES
  // to P{p+1}.mode

Then in matlab you can issue:

V = reshape(U,3,[])';
F = [];
for p = 1:numel(P)
  switch P{p}.mode
  case 4
    F = [F;reshape(P{p}.indices+1,3,[])'];
  case 5
    pF = triangles_from_strip(P{p}.indices+1);
    dblA = doublearea(V,pF);
    F = [F;pF(dblA>0,:)];
  end
end

Then you can save it to a obj, off or stl file or just render it in MATLAB:

sketchfab bulldog in matlab