2017-01-17 09:47:57 +00:00
|
|
|
from ..basemodel import TextModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face
|
2016-12-02 14:52:01 +00:00
|
|
|
from ..mesh import Material, MeshPart
|
2016-11-21 14:59:03 +00:00
|
|
|
from functools import reduce
|
2016-11-29 11:05:03 +00:00
|
|
|
import os.path
|
2016-11-29 16:24:39 +00:00
|
|
|
import sys
|
2016-11-29 11:05:03 +00:00
|
|
|
|
2016-11-21 14:59:03 +00:00
|
|
|
|
2016-11-22 10:27:42 +00:00
|
|
|
def is_obj(filename):
|
2017-01-18 14:11:44 +00:00
|
|
|
"""Checks that the file is a .obj file
|
|
|
|
|
|
|
|
Only checks the extension of the file
|
|
|
|
:param filename: path to the file
|
|
|
|
"""
|
2016-11-22 10:27:42 +00:00
|
|
|
return filename[-4:] == '.obj'
|
|
|
|
|
2017-01-17 09:47:57 +00:00
|
|
|
class OBJParser(TextModelParser):
|
2017-01-18 14:11:44 +00:00
|
|
|
"""Parser that parses a .obj file
|
|
|
|
"""
|
2016-11-21 14:59:03 +00:00
|
|
|
|
2016-11-30 14:37:58 +00:00
|
|
|
def __init__(self, up_conversion = None):
|
|
|
|
super().__init__(up_conversion)
|
2016-11-29 11:13:43 +00:00
|
|
|
self.current_material = None
|
2016-11-29 16:24:39 +00:00
|
|
|
self.mtl = None
|
2017-02-27 13:29:39 +00:00
|
|
|
self.vertex_offset = 0
|
2016-11-21 14:59:03 +00:00
|
|
|
|
|
|
|
def parse_line(self, string):
|
2017-01-18 14:11:44 +00:00
|
|
|
"""Parses a line of .obj file
|
|
|
|
|
|
|
|
:param string: the line to parse
|
|
|
|
"""
|
2016-11-22 10:54:59 +00:00
|
|
|
if string == '':
|
|
|
|
return
|
|
|
|
|
|
|
|
split = string.split()
|
2016-11-21 14:59:03 +00:00
|
|
|
first = split[0]
|
|
|
|
split = split[1:]
|
|
|
|
|
2016-11-29 16:24:39 +00:00
|
|
|
if first == 'usemtl' and self.mtl is not None:
|
2016-11-29 14:40:30 +00:00
|
|
|
self.current_material = self.mtl[split[0]]
|
2016-11-29 11:05:03 +00:00
|
|
|
elif first == 'mtllib':
|
2017-02-27 13:29:39 +00:00
|
|
|
path = os.path.join(os.path.dirname(self.path), ' '.join(split[:]))
|
2016-11-29 16:24:39 +00:00
|
|
|
if os.path.isfile(path):
|
|
|
|
self.mtl = MTLParser(self)
|
|
|
|
self.mtl.parse_file(path)
|
|
|
|
else:
|
2017-01-19 10:23:48 +00:00
|
|
|
print('Warning : ' + path + ' not found ', file=sys.stderr)
|
2016-11-21 14:59:03 +00:00
|
|
|
elif first == 'v':
|
|
|
|
self.add_vertex(Vertex().from_array(split))
|
|
|
|
elif first == 'vn':
|
|
|
|
self.add_normal(Normal().from_array(split))
|
|
|
|
elif first == 'vt':
|
|
|
|
self.add_tex_coord(TexCoord().from_array(split))
|
|
|
|
elif first == 'f':
|
|
|
|
splits = list(map(lambda x: x.split('/'), split))
|
|
|
|
|
|
|
|
for i in range(len(splits)):
|
|
|
|
for j in range(len(splits[i])):
|
|
|
|
if splits[i][j] is not '':
|
2017-01-19 10:23:48 +00:00
|
|
|
splits[i][j] = int(splits[i][j])
|
|
|
|
if splits[i][j] > 0:
|
|
|
|
splits[i][j] -= 1
|
|
|
|
else:
|
|
|
|
splits[i][j] = len(self.vertices) + splits[i][j]
|
2016-11-21 14:59:03 +00:00
|
|
|
|
2016-12-02 08:26:07 +00:00
|
|
|
# if Face3
|
|
|
|
if len(split) == 3:
|
|
|
|
face = Face().from_array(splits)
|
|
|
|
face.material = self.current_material
|
|
|
|
self.add_face(face)
|
2016-12-21 11:16:49 +00:00
|
|
|
|
2017-02-27 13:29:39 +00:00
|
|
|
# Face4 are well supported with the next stuff
|
|
|
|
# elif len(split) == 4:
|
|
|
|
# face = Face().from_array(splits[:3])
|
|
|
|
# face.material = self.current_material
|
|
|
|
# self.add_face(face)
|
|
|
|
|
|
|
|
# face = Face().from_array([splits[0], splits[2], splits[3]])
|
|
|
|
# face.material = self.current_material
|
|
|
|
# self.add_face(face)
|
2016-12-02 08:26:07 +00:00
|
|
|
|
|
|
|
else:
|
2017-02-27 13:29:39 +00:00
|
|
|
# Bweeee
|
|
|
|
# First, lets compute all the FaceVertex for each vertex
|
|
|
|
face_vertices = []
|
|
|
|
for face_vertex in splits[:]:
|
|
|
|
face_vertices.append(FaceVertex(*face_vertex))
|
|
|
|
|
|
|
|
# Then, we build the faces 0 i i+1 for each 1 <= i < len - 1
|
|
|
|
for i in range(1, len(face_vertices) - 1):
|
|
|
|
|
|
|
|
# Create face with barycenter, i and i + 1
|
|
|
|
face = Face(face_vertices[0], face_vertices[i], face_vertices[i+1])
|
|
|
|
face.material = self.current_material
|
|
|
|
self.add_face(face)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-21 14:59:03 +00:00
|
|
|
|
2016-11-29 11:05:03 +00:00
|
|
|
class MTLParser:
|
2017-01-18 14:11:44 +00:00
|
|
|
"""Parser that parses a .mtl material file
|
|
|
|
"""
|
2016-11-29 11:05:03 +00:00
|
|
|
def __init__(self, parent):
|
2017-01-18 14:11:44 +00:00
|
|
|
"""Creates a MTLParser bound to the OBJParser
|
|
|
|
|
|
|
|
:param parent: the OBJParser this MTLParser refers to
|
|
|
|
"""
|
2016-11-29 11:05:03 +00:00
|
|
|
self.parent = parent
|
|
|
|
self.current_mtl = None
|
|
|
|
|
|
|
|
def parse_line(self, string):
|
2017-01-18 14:11:44 +00:00
|
|
|
"""Parses a line of .mtl file
|
|
|
|
|
|
|
|
:param string: line to parse
|
|
|
|
"""
|
2016-11-29 11:05:03 +00:00
|
|
|
|
|
|
|
if string == '':
|
|
|
|
return
|
|
|
|
|
|
|
|
split = string.split()
|
|
|
|
first = split[0]
|
|
|
|
split = split[1:]
|
|
|
|
|
|
|
|
if first == 'newmtl':
|
2017-02-27 13:29:39 +00:00
|
|
|
self.current_mtl = Material(' '.join(split[:]))
|
2017-01-19 10:23:48 +00:00
|
|
|
self.parent.materials.append(self.current_mtl)
|
2016-11-29 11:05:03 +00:00
|
|
|
elif first == 'Ka':
|
|
|
|
self.current_mtl.Ka = Vertex().from_array(split)
|
|
|
|
elif first == 'Kd':
|
|
|
|
self.current_mtl.Kd = Vertex().from_array(split)
|
|
|
|
elif first == 'Ks':
|
|
|
|
self.current_mtl.Ks = Vertex().from_array(split)
|
|
|
|
elif first == 'map_Kd':
|
2017-02-27 13:29:39 +00:00
|
|
|
self.current_mtl.relative_path_to_texture = ' '.join(split)
|
|
|
|
self.current_mtl.absolute_path_to_texture = os.path.join(os.path.dirname(self.parent.path), ' '.join(split))
|
2016-11-29 11:05:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parse_file(self, path):
|
|
|
|
with open(path) as f:
|
|
|
|
for line in f.readlines():
|
|
|
|
line = line.rstrip()
|
|
|
|
self.parse_line(line)
|
|
|
|
|
2016-11-29 14:40:30 +00:00
|
|
|
def __getitem__(self, key):
|
2017-01-19 10:23:48 +00:00
|
|
|
for material in self.parent.materials:
|
2016-11-29 14:40:30 +00:00
|
|
|
if material.name == key:
|
|
|
|
return material
|
|
|
|
|
2016-11-29 11:05:03 +00:00
|
|
|
|
2016-11-21 14:59:03 +00:00
|
|
|
class OBJExporter(Exporter):
|
2017-01-18 14:11:44 +00:00
|
|
|
"""Exporter to .obj format
|
|
|
|
"""
|
|
|
|
|
2016-11-21 14:59:03 +00:00
|
|
|
def __init__(self, model):
|
2017-01-18 14:11:44 +00:00
|
|
|
"""Creates an exporter from the model
|
|
|
|
|
|
|
|
:param model: Model to export
|
|
|
|
"""
|
2016-11-21 14:59:03 +00:00
|
|
|
super().__init__(model)
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-18 14:11:44 +00:00
|
|
|
"""Exports the model
|
|
|
|
"""
|
2016-11-30 10:47:21 +00:00
|
|
|
current_material = ''
|
2016-11-21 14:59:03 +00:00
|
|
|
string = ""
|
|
|
|
|
|
|
|
for vertex in self.model.vertices:
|
2016-11-22 10:40:58 +00:00
|
|
|
string += "v " + ' '.join([str(vertex.x), str(vertex.y), str(vertex.z)]) + "\n"
|
2016-11-21 14:59:03 +00:00
|
|
|
|
|
|
|
string += "\n"
|
|
|
|
|
2016-11-21 15:18:49 +00:00
|
|
|
if len(self.model.tex_coords) > 0:
|
|
|
|
for tex_coord in self.model.tex_coords:
|
2016-11-22 10:40:58 +00:00
|
|
|
string += "vt " + ' '.join([str(tex_coord.x), str(tex_coord.y)]) + "\n"
|
2016-11-21 14:59:03 +00:00
|
|
|
|
2016-11-21 15:18:49 +00:00
|
|
|
string += "\n"
|
2016-11-21 14:59:03 +00:00
|
|
|
|
2016-11-21 15:18:49 +00:00
|
|
|
if len(self.model.normals) > 0:
|
|
|
|
for normal in self.model.normals:
|
2016-11-22 10:40:58 +00:00
|
|
|
string += "vn " + ' '.join([str(normal.x), str(normal.y), str(normal.z)]) + "\n"
|
2016-11-21 14:59:03 +00:00
|
|
|
|
2016-11-21 15:18:49 +00:00
|
|
|
string += "\n"
|
2016-11-21 14:59:03 +00:00
|
|
|
|
2016-11-30 10:47:21 +00:00
|
|
|
faces = sum(map(lambda x: x.faces, self.model.parts), [])
|
|
|
|
|
|
|
|
for face in faces:
|
2016-11-30 13:57:58 +00:00
|
|
|
if face.material is not None and face.material.name != current_material:
|
2016-11-30 10:47:21 +00:00
|
|
|
current_material = face.material.name
|
|
|
|
string += "usemtl " + current_material + "\n"
|
2016-11-21 14:59:03 +00:00
|
|
|
string += "f "
|
|
|
|
arr = []
|
|
|
|
for v in [face.a, face.b, face.c]:
|
|
|
|
sub_arr = []
|
2016-11-22 10:40:58 +00:00
|
|
|
sub_arr.append(str(v.vertex + 1))
|
2016-11-21 14:59:03 +00:00
|
|
|
if v.normal is None:
|
2016-11-30 10:47:21 +00:00
|
|
|
if v.tex_coord is not None:
|
2016-11-21 14:59:03 +00:00
|
|
|
sub_arr.append('')
|
2016-11-30 10:47:21 +00:00
|
|
|
sub_arr.append(str(v.tex_coord + 1))
|
|
|
|
elif v.tex_coord is not None:
|
|
|
|
sub_arr.append(str(v.tex_coord + 1))
|
2016-11-21 14:59:03 +00:00
|
|
|
if v.normal is not None:
|
2016-11-22 10:40:58 +00:00
|
|
|
sub_arr.append(str(v.normal + 1))
|
2016-11-21 14:59:03 +00:00
|
|
|
arr.append('/'.join(sub_arr))
|
|
|
|
|
|
|
|
string += ' '.join(arr) + '\n'
|
|
|
|
return string
|
|
|
|
|