diff --git a/README.md b/README.md index c5a8a7f..782309d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # model-converter This project aims to be a simple, lightweight, and useful 3D model editor. -For the moment, only `obj`, `ply` ascii and `stl` models are supported. +For the moment, only `obj`, `off`, `ply` ascii and `stl` models are supported. Feel free to open an issue if you find anything wrong in this. @@ -51,5 +51,6 @@ It should have a constructor that takes a `ModelParser` has parameter and a Here is the list of all the supported formats - Wavefront `.obj` - Stanford `.ply` + - Object File Format `.off` - STL files `.stl` diff --git a/d3/model/formats/obj.py b/d3/model/formats/obj.py index b64dfd6..c005ddf 100644 --- a/d3/model/formats/obj.py +++ b/d3/model/formats/obj.py @@ -52,6 +52,7 @@ class OBJParser(ModelParser): face = Face().from_array(splits) face.material = self.current_material self.add_face(face) + elif len(split) == 4: face = Face().from_array(splits[:3]) face.material = self.current_material diff --git a/d3/model/formats/off.py b/d3/model/formats/off.py new file mode 100644 index 0000000..19776a5 --- /dev/null +++ b/d3/model/formats/off.py @@ -0,0 +1,48 @@ +from ..basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face +from ..mesh import Material, MeshPart + +def is_off(filename): + return filename[-4:] == '.off' + +class OFFParser(ModelParser): + def __init__(self, up_conversion = None): + super().__init__(up_conversion) + self.vertex_number = None + self.face_number = None + self.edge_number = None + + def parse_line(self, string): + + split = string.split() + + if string == 'OFF': + pass + elif self.vertex_number is None: + # The first will be the header + self.vertex_number = int(split[0]) + self.face_number = int(split[1]) + self.edge_number = int(split[2]) + elif len(self.vertices) < self.vertex_number: + self.add_vertex(Vertex().from_array(split)) + else: + self.add_face(Face(FaceVertex(int(split[1])), FaceVertex(int(split[2])), FaceVertex(int(split[3])))) + + + +class OFFExporter(Exporter): + def __init__(self, model): + super().__init__(model) + + def __str__(self): + + faces = sum(map(lambda x: x.faces, self.model.parts), []) + string = "OFF\n{} {} {}".format(len(self.model.vertices), len(faces), 0) + '\n' + + for vertex in self.model.vertices: + string += ' '.join([str(vertex.x), str(vertex.y), str(vertex.z)]) + '\n' + + for face in faces: + string += '3 ' + ' '.join([str(face.a.vertex), str(face.b.vertex), str(face.c.vertex)]) + '\n' + + return string +