Added OFF files support

This commit is contained in:
Thomas FORGIONE 2016-12-21 12:16:49 +01:00
parent 9860d90e0b
commit d5d3bc78ed
No known key found for this signature in database
GPG Key ID: 2A210FFC062E00C3
3 changed files with 51 additions and 1 deletions

View File

@ -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`

View File

@ -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

48
d3/model/formats/off.py Normal file
View File

@ -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