Skip to content

Instantly share code, notes, and snippets.

@IntriguingTiles
Created May 11, 2021 09:29
Show Gist options
  • Select an option

  • Save IntriguingTiles/6cd29b37c0f0101bcbe3e9d44000aba8 to your computer and use it in GitHub Desktop.

Select an option

Save IntriguingTiles/6cd29b37c0f0101bcbe3e9d44000aba8 to your computer and use it in GitHub Desktop.
converts proprietary matterport dam models to obj
const protobuf = require("protobufjs");
const fs = require("fs");
const fetch = require("node-fetch");
async function go(id) {
console.log(`Downloading list of files for ${id}...`);
let files;
try {
files = await (await fetch(`https://my.matterport.com/api/player/models/${id}/files?type=1&format=json`)).json();
} catch (err) {
console.log(err);
console.log("Failed to download list of files.");
return;
}
if (files.code) {
console.log(`Failed to download list of files: ${files.message}`);
return;
}
let modelID = "";
for (const key in files) {
if (key.includes("_50k.dam")) modelID = key;
}
console.log("Downloading model...");
let rawModel;
try {
rawModel = await (await fetch(`${files[modelID]}`)).buffer();
} catch (err) {
console.log("Failed to download model.");
}
const proto = (await protobuf.load(__dirname + "/dam.proto")).lookupType("DAMFile");
const model = proto.decode(rawModel);
console.log("Converting to MDL...");
if (!fs.existsSync("./out")) fs.mkdirSync("./out");
let objOut = "mtllib model.mtl\n";
let mtlOut = "";
let faceOffset = 1;
model.chunk.forEach(c => {
const xyz = c.vertices.xyz;
const uv = c.vertices.uv;
const faces = c.faces.faces;
if (!mtlOut.includes(c.materialName)) {
mtlOut += `newmtl ${c.materialName}\n`;
mtlOut += `map_Kd ${c.materialName}\n`;
fetch(files[`${modelID.replace(".dam", "")}_texture_jpg_high/${c.materialName}`]).then(r => {
r.buffer().then(data => {
fs.writeFileSync(`./out/${c.materialName}`, data);
});
});
}
for (let i = 0; i < xyz.length; i+=3) {
objOut += `v ${xyz[i]} ${xyz[i + 1]} ${xyz[i + 2]}\n`;
}
for (let i = 0; i < uv.length; i+=2) {
objOut += `vt ${uv[i]} ${uv[i + 1]}\n`;
}
objOut += `usemtl ${c.materialName}\n`;
for (let i = 0; i < faces.length; i+=3) {
objOut += `f ${faces[i] + faceOffset}/${faces[i] + faceOffset} ${faces[i + 1] + faceOffset}/${faces[i + 1] + faceOffset} ${faces[i + 2] + faceOffset}/${faces[i + 2] + faceOffset}\n`;
}
faceOffset += xyz.length / 3;
});
fs.writeFileSync("./out/model.mtl", mtlOut);
fs.writeFileSync("./out/model.obj", objOut);
console.log("Done!");
}
go(process.argv[process.argv.length - 1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment