Added ply2obj, it is now working
This commit is contained in:
parent
c5ef973914
commit
71919abb21
2
model.py
2
model.py
|
@ -58,7 +58,7 @@ class ModelParser:
|
|||
def add_normal(self, normal):
|
||||
self.normals.append(normal)
|
||||
|
||||
def addFace(self, face):
|
||||
def add_face(self, face):
|
||||
self.faces.append(face)
|
||||
|
||||
def parse_line(self, string):
|
||||
|
|
26
obj.py
26
obj.py
|
@ -30,7 +30,7 @@ class OBJParser(ModelParser):
|
|||
if splits[i][j] is not '':
|
||||
splits[i][j] = str(int(splits[i][j]) - 1)
|
||||
|
||||
self.addFace(Face().from_array(splits))
|
||||
self.add_face(Face().from_array(splits))
|
||||
|
||||
class OBJExporter(Exporter):
|
||||
def __init__(self, model):
|
||||
|
@ -40,34 +40,36 @@ class OBJExporter(Exporter):
|
|||
string = ""
|
||||
|
||||
for vertex in self.model.vertices:
|
||||
string += "n " + ' '.join([vertex.x, vertex.y, vertex.z]) + "\n"
|
||||
string += "v " + ' '.join([vertex.x, vertex.y, vertex.z]) + "\n"
|
||||
|
||||
string += "\n"
|
||||
|
||||
for tex_coord in self.model.tex_coords:
|
||||
string += "vt " + ' '.join([tex_coord.x, tex_coord.y]) + "\n"
|
||||
if len(self.model.tex_coords) > 0:
|
||||
for tex_coord in self.model.tex_coords:
|
||||
string += "vt " + ' '.join([tex_coord.x, tex_coord.y]) + "\n"
|
||||
|
||||
string += "\n"
|
||||
string += "\n"
|
||||
|
||||
for normal in self.model.normals:
|
||||
string += "vn " + ' '.join([normal.x, normal.y, normal.z]) + "\n"
|
||||
if len(self.model.normals) > 0:
|
||||
for normal in self.model.normals:
|
||||
string += "vn " + ' '.join([normal.x, normal.y, normal.z]) + "\n"
|
||||
|
||||
string += "\n"
|
||||
string += "\n"
|
||||
|
||||
for face in self.model.faces:
|
||||
string += "f "
|
||||
arr = []
|
||||
for v in [face.a, face.b, face.c]:
|
||||
sub_arr = []
|
||||
sub_arr.append(v.vertex)
|
||||
sub_arr.append(str(int(v.vertex) + 1))
|
||||
if v.normal is None:
|
||||
if v.texture is not None:
|
||||
sub_arr.append('')
|
||||
sub_arr.append(v.texture)
|
||||
sub_arr.append(str(int(v.texture) + 1))
|
||||
elif v.texture is not None:
|
||||
sub_arr.append(v.texture)
|
||||
sub_arr.append(str(int(v.texture) + 1))
|
||||
if v.normal is not None:
|
||||
sub_arr.append(v.normal)
|
||||
sub_arr.append(str(int(v.normal + 1)))
|
||||
arr.append('/'.join(sub_arr))
|
||||
|
||||
string += ' '.join(arr) + '\n'
|
||||
|
|
17
ply.py
17
ply.py
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from model import ModelParser, Exporter, Vertex, Face
|
||||
from model import ModelParser, Exporter, Vertex, Face, FaceVertex
|
||||
|
||||
class PLYParser(ModelParser):
|
||||
|
||||
|
@ -11,7 +11,7 @@ class PLYParser(ModelParser):
|
|||
self.inner_parser = PLYHeaderParser(self)
|
||||
|
||||
def parse_line(self, string):
|
||||
self.inner_parser.parse_line(self, string)
|
||||
self.inner_parser.parse_line(string)
|
||||
|
||||
class PLYHeaderParser:
|
||||
def __init__(self, parent):
|
||||
|
@ -45,7 +45,7 @@ class PLYElement:
|
|||
self.number = number
|
||||
self.properties = []
|
||||
|
||||
def add_property(name, type):
|
||||
def add_property(self, name, type):
|
||||
self.properties.append((name, type))
|
||||
|
||||
class PLYContentParser:
|
||||
|
@ -56,20 +56,23 @@ class PLYContentParser:
|
|||
self.current_element = self.parent.elements[0]
|
||||
|
||||
def parse_line(self, string):
|
||||
self.counter += 1
|
||||
|
||||
split = string.split(' ')
|
||||
|
||||
if self.current_element.name == 'vertex':
|
||||
self.parent.add_vertex(Vertex.from_array(split))
|
||||
self.parent.add_vertex(Vertex().from_array(split))
|
||||
elif self.current_element.name == 'face':
|
||||
self.parent.add_face(Face(split[1], split[2], split[3]))
|
||||
self.parent.add_face(Face(FaceVertex(split[1]), FaceVertex(split[2]), FaceVertex(split[3])))
|
||||
|
||||
self.counter += 1
|
||||
|
||||
if self.counter == self.current_element.number:
|
||||
self.next_element()
|
||||
|
||||
def next_element(self):
|
||||
self.element_index += 1
|
||||
self.current_element = self.parent.elements[self.element_index]
|
||||
if self.element_index < len(self.parent.elements):
|
||||
self.current_element = self.parent.elements[self.element_index]
|
||||
|
||||
class PLYExporter(Exporter):
|
||||
def __init__(self, model):
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import os
|
||||
|
||||
from obj import OBJExporter
|
||||
from ply import PLYParser
|
||||
from functools import partial
|
||||
|
||||
def check_path(path, should_exist):
|
||||
""" Check that a path (file or folder) exists or not and return it.
|
||||
"""
|
||||
path = os.path.normpath(path)
|
||||
if should_exist != os.path.exists(path):
|
||||
msg = "path " + ("does not" if should_exist else "already") + " exist: " + path
|
||||
raise argparse.ArgumentTypeError(msg)
|
||||
return path
|
||||
|
||||
def main(args):
|
||||
parser = PLYParser()
|
||||
parser.parse_file(args.input)
|
||||
exporter = OBJExporter(parser)
|
||||
result = str(exporter)
|
||||
|
||||
if args.output is None:
|
||||
print(result)
|
||||
else:
|
||||
with open(args.output, 'w') as f:
|
||||
f.write(result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.set_defaults(func=main)
|
||||
parser.add_argument('-v', '--version', action='version', version='1.0')
|
||||
parser.add_argument('-i', '--input', metavar='input',
|
||||
type=partial(check_path, should_exist=True), default=None,
|
||||
help='Input file (.obj)')
|
||||
parser.add_argument('-o', '--output', metavar='output',
|
||||
help='Output path')
|
||||
args = parser.parse_args()
|
||||
args.func(args)
|
||||
|
Loading…
Reference in New Issue