|
| 1 | +""" |
| 2 | +------------------------- |
| 3 | +How to use this converter |
| 4 | +------------------------- |
| 5 | +python convert_obj_three.py -i infile.obj -o outfile.txt |
| 6 | +Notes: |
| 7 | + - flags |
| 8 | + -i infile.obj input OBJ file |
| 9 | + -o outfile.js output txt file |
| 10 | +
|
| 11 | +""" |
| 12 | + |
| 13 | +from _typeshed import Self |
| 14 | +import fileinput |
| 15 | +import operator |
| 16 | +import random |
| 17 | +import os.path |
| 18 | +import getopt |
| 19 | +import sys |
| 20 | +import struct |
| 21 | +import math |
| 22 | +import glob |
| 23 | + |
| 24 | +# ##################################################### |
| 25 | +# Utils |
| 26 | +# ##################################################### |
| 27 | +def file_exists(filename): |
| 28 | + """Return true if file exists and is accessible for reading. |
| 29 | + Should be safer than just testing for existence due to links and |
| 30 | + permissions magic on Unix filesystems. |
| 31 | + @rtype: boolean |
| 32 | + """ |
| 33 | + |
| 34 | + try: |
| 35 | + f = open(filename, 'r') |
| 36 | + f.close() |
| 37 | + return True |
| 38 | + except IOError: |
| 39 | + return False |
| 40 | + |
| 41 | + |
| 42 | +# ##################################################### |
| 43 | +# graph class |
| 44 | +# ##################################################### |
| 45 | + |
| 46 | +class Point: |
| 47 | + def __init__(self, x, y): |
| 48 | + self.x = x |
| 49 | + self.y = y |
| 50 | + |
| 51 | + def sub(self, point): |
| 52 | + return Point(self.x - point.x, self.y - point.y) |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +# ##################################################### |
| 58 | +# OBJ parser |
| 59 | +# ##################################################### |
| 60 | +def parse_vertex(text): |
| 61 | + """Parse text chunk specifying single vertex. |
| 62 | + Possible formats: |
| 63 | + vertex index |
| 64 | + vertex index / texture index |
| 65 | + vertex index / texture index / normal index |
| 66 | + vertex index / / normal index |
| 67 | + """ |
| 68 | + |
| 69 | + v = 0 |
| 70 | + t = 0 |
| 71 | + n = 0 |
| 72 | + |
| 73 | + chunks = text.split("/") |
| 74 | + |
| 75 | + v = int(chunks[0]) |
| 76 | + if len(chunks) > 1: |
| 77 | + if chunks[1]: |
| 78 | + t = int(chunks[1]) |
| 79 | + if len(chunks) > 2: |
| 80 | + if chunks[2]: |
| 81 | + n = int(chunks[2]) |
| 82 | + |
| 83 | + return { 'v':v, 't':t, 'n':n } |
| 84 | + |
| 85 | +def parse_obj(fname): |
| 86 | + """Parse OBJ file. |
| 87 | + """ |
| 88 | + |
| 89 | + vertices = [] |
| 90 | + normals = [] |
| 91 | + uvs = [] |
| 92 | + |
| 93 | + faces = [] |
| 94 | + |
| 95 | + materials = {} |
| 96 | + material = "" |
| 97 | + mcounter = 0 |
| 98 | + mcurrent = 0 |
| 99 | + |
| 100 | + mtllib = "" |
| 101 | + |
| 102 | + # current face state |
| 103 | + group = 0 |
| 104 | + object = 0 |
| 105 | + smooth = 0 |
| 106 | + |
| 107 | + for line in fileinput.input(fname): |
| 108 | + chunks = line.split() |
| 109 | + if len(chunks) > 0: |
| 110 | + |
| 111 | + # Vertices as (x,y,z) coordinates |
| 112 | + # v 0.123 0.234 0.345 |
| 113 | + if chunks[0] == "v" and len(chunks) == 4: |
| 114 | + x = float(chunks[1]) |
| 115 | + y = float(chunks[2]) |
| 116 | + z = float(chunks[3]) |
| 117 | + vertices.append([x,y,z]) |
| 118 | + |
| 119 | + # Normals in (x,y,z) form; normals might not be unit |
| 120 | + # vn 0.707 0.000 0.707 |
| 121 | + if chunks[0] == "vn" and len(chunks) == 4: |
| 122 | + x = float(chunks[1]) |
| 123 | + y = float(chunks[2]) |
| 124 | + z = float(chunks[3]) |
| 125 | + normals.append([x,y,z]) |
| 126 | + |
| 127 | + # Texture coordinates in (u,v[,w]) coordinates, w is optional |
| 128 | + # vt 0.500 -1.352 [0.234] |
| 129 | + if chunks[0] == "vt" and len(chunks) >= 3: |
| 130 | + u = float(chunks[1]) |
| 131 | + v = float(chunks[2]) |
| 132 | + w = 0 |
| 133 | + if len(chunks)>3: |
| 134 | + w = float(chunks[3]) |
| 135 | + uvs.append([u,v,w]) |
| 136 | + |
| 137 | + # Face |
| 138 | + if chunks[0] == "f" and len(chunks) >= 4: |
| 139 | + vertex_index = [] |
| 140 | + uv_index = [] |
| 141 | + normal_index = [] |
| 142 | + |
| 143 | + |
| 144 | + # Precompute vert / normal / uv lists |
| 145 | + # for negative index lookup |
| 146 | + vertlen = len(vertices) + 1 |
| 147 | + normlen = len(normals) + 1 |
| 148 | + uvlen = len(uvs) + 1 |
| 149 | + |
| 150 | + for v in chunks[1:]: |
| 151 | + vertex = parse_vertex(v) |
| 152 | + if vertex['v']: |
| 153 | + if vertex['v'] < 0: |
| 154 | + vertex['v'] += vertlen |
| 155 | + vertex_index.append(vertex['v']) |
| 156 | + if vertex['t']: |
| 157 | + if vertex['t'] < 0: |
| 158 | + vertex['t'] += uvlen |
| 159 | + uv_index.append(vertex['t']) |
| 160 | + if vertex['n']: |
| 161 | + if vertex['n'] < 0: |
| 162 | + vertex['n'] += normlen |
| 163 | + normal_index.append(vertex['n']) |
| 164 | + faces.append({ |
| 165 | + 'vertex':vertex_index, |
| 166 | + 'uv':uv_index, |
| 167 | + 'normal':normal_index, |
| 168 | + |
| 169 | + 'material':mcurrent, |
| 170 | + 'group':group, |
| 171 | + 'object':object, |
| 172 | + 'smooth':smooth, |
| 173 | + }) |
| 174 | + |
| 175 | + # Group |
| 176 | + if chunks[0] == "g" and len(chunks) == 2: |
| 177 | + group = chunks[1] |
| 178 | + |
| 179 | + # Object |
| 180 | + if chunks[0] == "o" and len(chunks) == 2: |
| 181 | + object = chunks[1] |
| 182 | + |
| 183 | + # Materials definition |
| 184 | + if chunks[0] == "mtllib" and len(chunks) == 2: |
| 185 | + mtllib = chunks[1] |
| 186 | + |
| 187 | + # Material |
| 188 | + if chunks[0] == "usemtl": |
| 189 | + if len(chunks) > 1: |
| 190 | + material = chunks[1] |
| 191 | + else: |
| 192 | + material = "" |
| 193 | + if not material in materials: |
| 194 | + mcurrent = mcounter |
| 195 | + materials[material] = mcounter |
| 196 | + mcounter += 1 |
| 197 | + else: |
| 198 | + mcurrent = materials[material] |
| 199 | + |
| 200 | + # Smooth shading |
| 201 | + if chunks[0] == "s" and len(chunks) == 2: |
| 202 | + smooth = chunks[1] |
| 203 | + |
| 204 | + return faces, vertices, uvs, normals, materials, mtllib |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | +def GenPolygon(vertices, indices): |
| 209 | + pass |
| 210 | + |
| 211 | + |
| 212 | +def convert_txt(infile, outfile): |
| 213 | + if not file_exists(infile): |
| 214 | + print("Couldn't find [%s]" % infile) |
| 215 | + return |
| 216 | + |
| 217 | + faces, vertices, uvs, normals, materials, mtllib = parse_obj(infile) |
| 218 | + print(faces) |
| 219 | + # print(vertices) |
| 220 | + # print(uvs, normals, materials, mtllib) |
| 221 | + |
| 222 | + GenPolygon(vertices, faces) |
| 223 | + |
| 224 | +# ############################################################################# |
| 225 | +# Helpers |
| 226 | +# ############################################################################# |
| 227 | +def usage(): |
| 228 | + print("Usage: {} -i filename.obj -o filename.js".format(os.path.basename(sys.argv[0]))) |
| 229 | + |
| 230 | +# ##################################################### |
| 231 | +# Main |
| 232 | +# ##################################################### |
| 233 | +if __name__ == "__main__": |
| 234 | + |
| 235 | + # get parameters from the command line |
| 236 | + try: |
| 237 | + opts, args = getopt.getopt(sys.argv[1:], "hi:o:", ["help", "input=", "output="]) |
| 238 | + |
| 239 | + except getopt.GetoptError: |
| 240 | + usage() |
| 241 | + sys.exit(2) |
| 242 | + |
| 243 | + infile = outfile = "" |
| 244 | + |
| 245 | + for o, a in opts: |
| 246 | + if o in ("-h", "--help"): |
| 247 | + usage() |
| 248 | + sys.exit() |
| 249 | + |
| 250 | + elif o in ("-i", "--input"): |
| 251 | + infile = a |
| 252 | + |
| 253 | + elif o in ("-o", "--output"): |
| 254 | + outfile = a |
| 255 | + print("infile:" + infile) |
| 256 | + print("outfile:" + outfile) |
| 257 | + if infile == "" or outfile == "": |
| 258 | + usage() |
| 259 | + sys.exit(2) |
| 260 | + |
| 261 | + convert_txt(infile, outfile) |
| 262 | + |
0 commit comments