Created
August 29, 2021 19:56
-
-
Save iam4722202468/15166f8694a751baf697da7f0c7d5670 to your computer and use it in GitHub Desktop.
BlockBench File Reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| function makeId (length) { | |
| var result = ''; | |
| var characters = 'abcdefghijklmnopqrstuvwxyz'; | |
| var charactersLength = characters.length; | |
| for ( var i = 0; i < length; i++ ) { | |
| result += characters.charAt(Math.floor(Math.random() * | |
| charactersLength)); | |
| } | |
| return result; | |
| } | |
| const fileName = './ASDF_my_texture.bbmodel' | |
| const folderName = fileName.split('.')[fileName.split('.').length-2].replace(/\.|\//, '').toLowerCase() | |
| let rawdata = fs.readFileSync(fileName); | |
| let bbench = JSON.parse(rawdata); | |
| const elements = bbench.elements.map(el => { | |
| inflate = 0 | |
| if (el.inflate != undefined) | |
| inflate = el.inflate | |
| inflate *= 100 | |
| inflate = Math.round(inflate) / 100 | |
| objRes = { | |
| from: el.from.map(el => el-inflate), | |
| to: el.to.map(el => el+inflate), | |
| faces: Object.keys(el.faces).map(face => { | |
| if (el.faces[face].texture == undefined) | |
| el.faces[face].texture = '#missing' | |
| else | |
| el.faces[face].texture = '#' + el.faces[face]['texture'] | |
| obj = { | |
| ...el.faces[face], | |
| texture: el.faces[face].texture | |
| } | |
| em = {} | |
| em[face] = obj | |
| return em | |
| }) | |
| } | |
| if (el.rotation != undefined) { | |
| axis = '' | |
| angle = 0 | |
| if (el.rotation[0] != 0) { | |
| axis = 'x' | |
| angle = el.rotation[0] | |
| } else if (el.rotation[1] != 0) { | |
| axis = 'y' | |
| angle = el.rotation[1] | |
| } else { | |
| axis = 'z' | |
| angle = el.rotation[2] | |
| } | |
| objRes.rotation = { | |
| angle: angle, | |
| axis: axis, | |
| origin: el.origin | |
| } | |
| } else { | |
| objRes.rotation = { | |
| angle: 0, | |
| axis: 'y', | |
| origin: el.origin | |
| } | |
| } | |
| return objRes | |
| }) | |
| textureList = {} | |
| for (textureIndex in bbench.textures) { | |
| const texture = bbench.textures[textureIndex] | |
| if (!fs.existsSync(folderName)){ | |
| fs.mkdirSync(folderName); | |
| } else { | |
| fs.rmdirSync(folderName, { recursive: true }); | |
| fs.mkdirSync(folderName); | |
| } | |
| var base64Data = texture.source.replace(/^data:image\/png;base64,/, ""); | |
| const id = makeId(20) | |
| fs.writeFile(folderName + "/" + id + ".png", base64Data, 'base64', function(err) { | |
| if (err != null) | |
| console.log(err); | |
| }); | |
| textureList[texture.id] = folderName + "/" + id | |
| textureList['particle'] = folderName + "/" + id | |
| } | |
| const parsed = { | |
| "textures": textureList, | |
| elements: elements, | |
| display: bbench.display | |
| } | |
| fs.writeFile(folderName + "/" + "data", JSON.stringify(parsed), function(err) { | |
| if (err != null) | |
| console.log(err); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment