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):
|
def add_normal(self, normal):
|
||||||
self.normals.append(normal)
|
self.normals.append(normal)
|
||||||
|
|
||||||
def addFace(self, face):
|
def add_face(self, face):
|
||||||
self.faces.append(face)
|
self.faces.append(face)
|
||||||
|
|
||||||
def parse_line(self, string):
|
def parse_line(self, string):
|
||||||
|
|
14
obj.py
14
obj.py
|
@ -30,7 +30,7 @@ class OBJParser(ModelParser):
|
||||||
if splits[i][j] is not '':
|
if splits[i][j] is not '':
|
||||||
splits[i][j] = str(int(splits[i][j]) - 1)
|
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):
|
class OBJExporter(Exporter):
|
||||||
def __init__(self, model):
|
def __init__(self, model):
|
||||||
|
@ -40,15 +40,17 @@ class OBJExporter(Exporter):
|
||||||
string = ""
|
string = ""
|
||||||
|
|
||||||
for vertex in self.model.vertices:
|
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"
|
string += "\n"
|
||||||
|
|
||||||
|
if len(self.model.tex_coords) > 0:
|
||||||
for tex_coord in self.model.tex_coords:
|
for tex_coord in self.model.tex_coords:
|
||||||
string += "vt " + ' '.join([tex_coord.x, tex_coord.y]) + "\n"
|
string += "vt " + ' '.join([tex_coord.x, tex_coord.y]) + "\n"
|
||||||
|
|
||||||
string += "\n"
|
string += "\n"
|
||||||
|
|
||||||
|
if len(self.model.normals) > 0:
|
||||||
for normal in self.model.normals:
|
for normal in self.model.normals:
|
||||||
string += "vn " + ' '.join([normal.x, normal.y, normal.z]) + "\n"
|
string += "vn " + ' '.join([normal.x, normal.y, normal.z]) + "\n"
|
||||||
|
|
||||||
|
@ -59,15 +61,15 @@ class OBJExporter(Exporter):
|
||||||
arr = []
|
arr = []
|
||||||
for v in [face.a, face.b, face.c]:
|
for v in [face.a, face.b, face.c]:
|
||||||
sub_arr = []
|
sub_arr = []
|
||||||
sub_arr.append(v.vertex)
|
sub_arr.append(str(int(v.vertex) + 1))
|
||||||
if v.normal is None:
|
if v.normal is None:
|
||||||
if v.texture is not None:
|
if v.texture is not None:
|
||||||
sub_arr.append('')
|
sub_arr.append('')
|
||||||
sub_arr.append(v.texture)
|
sub_arr.append(str(int(v.texture) + 1))
|
||||||
elif v.texture is not None:
|
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:
|
if v.normal is not None:
|
||||||
sub_arr.append(v.normal)
|
sub_arr.append(str(int(v.normal + 1)))
|
||||||
arr.append('/'.join(sub_arr))
|
arr.append('/'.join(sub_arr))
|
||||||
|
|
||||||
string += ' '.join(arr) + '\n'
|
string += ' '.join(arr) + '\n'
|
||||||
|
|
15
ply.py
15
ply.py
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from model import ModelParser, Exporter, Vertex, Face
|
from model import ModelParser, Exporter, Vertex, Face, FaceVertex
|
||||||
|
|
||||||
class PLYParser(ModelParser):
|
class PLYParser(ModelParser):
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ class PLYParser(ModelParser):
|
||||||
self.inner_parser = PLYHeaderParser(self)
|
self.inner_parser = PLYHeaderParser(self)
|
||||||
|
|
||||||
def parse_line(self, string):
|
def parse_line(self, string):
|
||||||
self.inner_parser.parse_line(self, string)
|
self.inner_parser.parse_line(string)
|
||||||
|
|
||||||
class PLYHeaderParser:
|
class PLYHeaderParser:
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
|
@ -45,7 +45,7 @@ class PLYElement:
|
||||||
self.number = number
|
self.number = number
|
||||||
self.properties = []
|
self.properties = []
|
||||||
|
|
||||||
def add_property(name, type):
|
def add_property(self, name, type):
|
||||||
self.properties.append((name, type))
|
self.properties.append((name, type))
|
||||||
|
|
||||||
class PLYContentParser:
|
class PLYContentParser:
|
||||||
|
@ -56,19 +56,22 @@ class PLYContentParser:
|
||||||
self.current_element = self.parent.elements[0]
|
self.current_element = self.parent.elements[0]
|
||||||
|
|
||||||
def parse_line(self, string):
|
def parse_line(self, string):
|
||||||
self.counter += 1
|
|
||||||
|
|
||||||
split = string.split(' ')
|
split = string.split(' ')
|
||||||
|
|
||||||
if self.current_element.name == 'vertex':
|
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':
|
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
|
self.counter += 1
|
||||||
|
|
||||||
|
if self.counter == self.current_element.number:
|
||||||
|
self.next_element()
|
||||||
|
|
||||||
def next_element(self):
|
def next_element(self):
|
||||||
self.element_index += 1
|
self.element_index += 1
|
||||||
|
if self.element_index < len(self.parent.elements):
|
||||||
self.current_element = self.parent.elements[self.element_index]
|
self.current_element = self.parent.elements[self.element_index]
|
||||||
|
|
||||||
class PLYExporter(Exporter):
|
class PLYExporter(Exporter):
|
||||||
|
|
|
@ -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