forked from Annovae/sketchfab-dl-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketchfab-scene-exporter.user.js
347 lines (323 loc) · 11.1 KB
/
sketchfab-scene-exporter.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// ==UserScript==
// @name sketchfab-scene-exporter
// @description lets you export Sketchfab scenes
// @author Reinitialized
//
//Version Number
// @version 1.09
//
// Urls process this user script on
// @include /^https?://(www\.)?sketchfab\.com/models/.*/embed.*$/
// @grant none
// ==/UserScript==
function unfreeze(obj) {
var copy = {};
for(var i in obj) {
copy[i] = obj[i];
}
return copy;
}
var models = [];
var baseModelName = safeName(document.title.replace(' - Sketchfab', ''));
function overrideDrawImplementation() {
var geometry = OSG.osg.Geometry;
var newPrototype = unfreeze(geometry.prototype);
geometry.prototype = newPrototype;
newPrototype.originalDrawImplementation = newPrototype.drawImplementation;
newPrototype.drawImplementation = function(a) {
this.originalDrawImplementation(a);
if (!this.computedOBJ) {
this.computedOBJ = true;
this.name = baseModelName + '-' + models.length;
this.__defineGetter__("textures", function() {
return this._textures ? this._textures : this._textures = textureInfoForGeometry(this);
});
models.push({
name: this.name,
geom: this,
get obj() {
return OBJforGeometry(this.geom);
},
get mtl() {
return MTLforGeometry(this.geom);
},
get textures() {
return this.geom.textures;
}
});
}
};
}
// source: http://stackoverflow.com/a/8485137
function safeName(s) {
return s.replace(/[^a-zA-Z0-9]/gi, '_').toLowerCase();
}
function InfoForGeometry(geom) {
var attributes = geom.attributes;
if (!attributes)
throw "No attributes for geometry";
var vertices = attributes.Vertex;
if (!vertices)
throw "No vertices for geometry";
var normals = attributes.Normal;
var texCoords = attributes.TexCoord0;
var info = {
'vertices' : vertices._elements,
'normals' : normals ? normals._elements : [],
'texCoords' : texCoords ? texCoords._elements : [],
'primitives' : [],
'name' : geom.name
};
for (i = 0; i < geom.primitives.length; ++i) {
var primitive = geom.primitives[i];
info.primitives.push({
'mode' : primitive.mode,
'indices' : primitive.indices._elements
});
}
return info;
}
var verticesExported = 0;
var nl = '\n';
function OBJforGeometry(geom) {
var obj = '';
var info = InfoForGeometry(geom);
obj += 'mtllib ' + MTLFilenameForGeometry(geom) + nl;
obj += 'o ' + geom.name + nl;
for (i = 0; i < info.vertices.length; i += 3) {
obj += 'v ';
for (j = 0; j < 3; ++j) {
obj += info.vertices[i + j] + ' ';
}
obj += nl;
}
for (i = 0; i < info.normals.length; i += 3) {
obj += 'vn ';
for (j = 0; j < 3; ++j) {
obj += info.normals[i + j] + ' ';
}
obj += nl;
}
for (i = 0; i < info.texCoords.length; i += 2) {
obj += 'vt ';
for (j = 0; j < 2; ++j) {
obj += info.texCoords[i + j] + ' ';
}
obj += nl;
}
obj += 'usemtl ' + MTLNameForGeometry(geom) + nl;
obj += 's on' + nl;
var exist = {
normals: info.normals.length != 0,
texCoords: info.texCoords.length != 0,
};
for (i = 0; i < info.primitives.length; ++i) {
var primitive = info.primitives[i];
if (primitive.mode == 4 || primitive.mode == 5) {
var isTriangleStrip = primitive.mode == 5;
for (j = 0; j + 2 < primitive.indices.length; !isTriangleStrip ? j += 3 : ++j) {
obj += 'f ';
var isOddFace = j % 2 == 1;
var order = [ 0, 1, 2];
if (isTriangleStrip && isOddFace)
order = [ 0, 2, 1];
for (k = 0; k < 3; ++k) {
var faceNum = primitive.indices[j + order[k]] + 1 + verticesExported;
obj += faceNum;
if (exist.normals && !exist.texCoords) {
obj += '//' + faceNum;
}
else {
if (exist.texCoords) {
obj += '/' + faceNum;
}
if (exist.normals) {
obj += '/' + faceNum;
}
}
obj += ' ';
}
obj += nl;
}
}
else {
throw 'Primitive mode not implemented';
}
}
verticesExported += info.vertices.length / 3.0;
return obj;
}
var textureMTLMap = {
DiffuseColor: "map_Kd",
SpecularColor: "map_Ks",
NormalMap : "map_bump",
EmitColor : "map_Ke",
AlphaMask : "map_d",
Opacity : "map_o"
};
// source: http://stackoverflow.com/a/3820412
function baseName(str)
{
var base = new String(str).substring(str.lastIndexOf('/') + 1);
if(base.lastIndexOf(".") != -1)
base = base.substring(0, base.lastIndexOf("."));
return base;
}
function ext(str) {
return str.split('.').pop();
}
function textureInfoForGeometry(geom) {
var textureMap = [];
if (stateset = geom.stateset) {
if (textures = stateset.textureAttributeMapList) {
textures.forEach(function(texture) {
if (Texture = texture.Texture) {
if (object = Texture._object) {
if (texture = object._texture) {
if (imageProxy = texture._imageProxy) {
var textureURL = imageProxy.attributes.images[0].url;
var texture = {
url: textureURL,
type: textureMTLMap[object._channelName],
ext: ext(textureURL)
};
texture.filename = textureFilename(geom, texture);
textureMap.push(texture);
}
}
}
}
});
}
}
return textureMap;
}
function textureFilename(geom, texture) {
return baseName(texture.url) + '.' + texture.ext;
}
function MTLFilenameForGeometry(geom) {
return baseModelName + '.mtl';
}
function MTLNameForGeometry(geom) {
return geom.name;
}
function MTLforGeometry(geom) {
var mtl = '';
mtl += 'newmtl ' + MTLNameForGeometry(geom) + nl;
geom.textures.forEach(function(texture) {
mtl += texture.type + ' ' + texture.filename + nl;
});
return mtl;
}
// source: http://stackoverflow.com/questions/3219758/detect-changes-in-the-dom
var observeDOM = (function(){
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver,
eventListenerSupported = window.addEventListener;
return function(obj, callback){
if( MutationObserver ){
// define a new observer
var obs = new MutationObserver(function(mutations, observer){
if( mutations[0].addedNodes.length || mutations[0].removedNodes.length )
callback();
});
// have the observer observe foo for changes in children
obs.observe( obj, { childList:true, subtree:true });
}
else if( eventListenerSupported ){
obj.addEventListener('DOMNodeInserted', callback, false);
obj.addEventListener('DOMNodeRemoved', callback, false);
}
}
})();
// source: http://stackoverflow.com/questions/10596417/is-there-a-way-to-get-element-by-xpath-in-javascript
function getElementByXpath(path) {
return document.evaluate(path, document, null, 9, null).singleNodeValue;
}
var addedDownloadButton = false;
var downloadButtonParentXPath = "//div[@class='controls']";
var osgScriptElementPath = "//script[contains (@src, 'sketchfab')]";
var foundOsgScript = false;
observeDOM(document.body, function(){
if (!foundOsgScript) {
if (osgScript = getElementByXpath(osgScriptElementPath)) {
overrideDrawImplementation();
foundOsgScript = true;
}
}
if (!addedDownloadButton) {
if (downloadButtonParent = getElementByXpath(downloadButtonParentXPath))
{
setTimeout(function () {
addDownloadButton(downloadButtonParent);
}, 2000);
addedDownloadButton = true;
}
}
});
// source: http://thiscouldbebetter.wordpress.com/2012/12/18/loading-editing-and-saving-a-text-file-in-html5-using-javascrip/
function downloadString(filename, ext, str) {
function destroyClickedElement(event)
{
document.body.removeChild(event.target);
}
var textFileAsBlob = new Blob([str], {type:'text/plain'});
var fileNameToSaveAs = filename + '.' + ext;
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null)
{
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
}
else
{
// Firefox requires the link to be added to the DOM
// before it can be clicked.
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
}
downloadLink.click();
}
var imagesDownloaded = {};
// source: http://muaz-khan.blogspot.com/2012/10/save-files-on-disk-using-javascript-or.html
function downloadFileAtURL(fileURL) {
if (!imagesDownloaded[fileURL]) {
imagesDownloaded[fileURL] = true;
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = '';
var event = document.createEvent('Event');
event.initEvent('click', true, true);
save.dispatchEvent(event);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
}
function downloadModels() {
if (models.length == 0) {
alert("Download script failed... try refreshing the page");
return;
}
var combinedOBJ = '';
var combinedMTL = '';
models.forEach(function(model) {
combinedOBJ += model.obj + nl;
combinedMTL += model.mtl + nl;
model.textures.forEach(function(texture) {
downloadFileAtURL(texture.url);
});
});
downloadString(baseModelName, 'obj', combinedOBJ);
downloadString(baseModelName, 'mtl', combinedMTL);
}
function addDownloadButton(downloadButtonParent) {
var downloadButton = document.createElement("a");
downloadButton.setAttribute("class", "control");
downloadButton.innerHTML = "<pre>DOWNLOAD </pre>";
downloadButton.addEventListener("click", downloadModels , false);
downloadButtonParent.appendChild(downloadButton);
}