├── README.md ├── obj2sc.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # obj2sc 2 | Command line tool that converts OBJ files to a JSON formatted simplicial complex 3 | 4 | # Install 5 | 6 | ``` 7 | npm i -g obj2sc 8 | ``` 9 | 10 | # Usage 11 | 12 | ``` 13 | obj2sc < mymesh.obj > mymesh.json 14 | ``` 15 | 16 | # Credits 17 | (c) 2017 Mikola Lysenko. MIT License 18 | -------------------------------------------------------------------------------- /obj2sc.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var str = [] 4 | process.stdin 5 | .setEncoding('utf-8') 6 | .on('readable', function () { 7 | var chunk 8 | while (chunk = process.stdin.read()) { 9 | if (chunk) { 10 | str.push(chunk) 11 | } 12 | } 13 | }) 14 | .on('end', function () { 15 | var verts = [] 16 | var vt = [] 17 | var vn = [] 18 | var faces = [] 19 | var uv = [] 20 | var normals = [] 21 | 22 | str.join('').split('\n').forEach(function (line) { 23 | var toks = line.split(/\s+/) 24 | if (toks[0] === 'v') { 25 | verts.push(toks.slice(1).map((x) => +x)) 26 | } else if (toks[0] === 'vt') { 27 | vt.push(toks.slice(1).map((x) => +x)) 28 | } else if (toks[0] === 'vn') { 29 | vn.push(toks.slice(1).map((x) => +x)) 30 | } else if (toks[0] === 'f') { 31 | var f = [] 32 | for (var i = 1; i < toks.length; i++) { 33 | var vtn = toks[i].split('/') 34 | var vi = (vtn[0]-1)|0 35 | f.push(vi) 36 | if (vtn[1]) uv[vi] = vt[(vtn[1]-1)|0] // texture index 37 | if (vtn[2]) normals[vi] = vn[(vtn[2]-1)|0] // normal index 38 | } 39 | if (f.length === 3) faces.push(f) 40 | else { 41 | for (var i = 2; i < f.length; i++) { 42 | faces.push([f[0],f[i-1],f[i]]) 43 | } 44 | } 45 | } 46 | }) 47 | 48 | var data = { 49 | positions: verts, 50 | cells: faces 51 | } 52 | if (uv.length) { 53 | data.uv = uv 54 | } 55 | if (normals.length) { 56 | data.normals = normals 57 | } 58 | console.log(JSON.stringify(data)) 59 | }) 60 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "obj2sc", 3 | "version": "1.0.0", 4 | "description": "Command line tool to convert OBJ format meshes to JSON formatted simplicial complexes", 5 | "main": "obj2sc.js", 6 | "bin": { 7 | "obj2sc": "obj2sc.js" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/mikolalysenko/obj2sc.git" 15 | }, 16 | "keywords": [ 17 | "obj", 18 | "parse", 19 | "simplicial", 20 | "complex", 21 | "cells", 22 | "triangles", 23 | "mesh", 24 | "3d" 25 | ], 26 | "author": "Mikola Lysenko", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/mikolalysenko/obj2sc/issues" 30 | }, 31 | "homepage": "https://github.com/mikolalysenko/obj2sc#readme" 32 | } 33 | --------------------------------------------------------------------------------