From 41a13e601470dc4d981928d5fcd2cf483df2e648 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Tue, 17 Jan 2017 10:47:57 +0100 Subject: [PATCH] Added TextModelParser for text files --- d3/model/basemodel.py | 27 ++++++++++++--------------- d3/model/formats/obj.py | 4 ++-- d3/model/formats/off.py | 4 ++-- d3/model/formats/ply.py | 4 ++-- d3/model/formats/stl.py | 4 ++-- 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/d3/model/basemodel.py b/d3/model/basemodel.py index a98fbb9..c64c386 100644 --- a/d3/model/basemodel.py +++ b/d3/model/basemodel.py @@ -134,23 +134,9 @@ class ModelParser: self.current_part.add_face(face) - def parse_line(self, string): - """Parses a line of a text model - - This method needs to be implemented by each subclass of ModelParser. - """ + def parse_bytes(self, bytes): pass - def parse_file(self, path): - """Sets the path of the model and parse each line - """ - self.path = path - with open(path) as f: - for line in f.readlines(): - line = line.rstrip() - if line != '': - self.parse_line(line) - def draw(self): """Draws each part of the model with OpenGL """ @@ -221,6 +207,17 @@ class ModelParser: face.b.normal = index face.c.normal = index +class TextModelParser(ModelParser): + def parse_file(self, path): + """Sets the path of the model and parse each line + """ + self.path = path + with open(path) as f: + for line in f.readlines(): + line = line.rstrip() + if line != '': + self.parse_line(line) + class BoundingBox: """Represents a bounding box of a 3D model diff --git a/d3/model/formats/obj.py b/d3/model/formats/obj.py index 62d75ab..37f8c22 100644 --- a/d3/model/formats/obj.py +++ b/d3/model/formats/obj.py @@ -1,4 +1,4 @@ -from ..basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face +from ..basemodel import TextModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face from ..mesh import Material, MeshPart from functools import reduce import os.path @@ -8,7 +8,7 @@ import sys def is_obj(filename): return filename[-4:] == '.obj' -class OBJParser(ModelParser): +class OBJParser(TextModelParser): def __init__(self, up_conversion = None): super().__init__(up_conversion) diff --git a/d3/model/formats/off.py b/d3/model/formats/off.py index 19776a5..af5b573 100644 --- a/d3/model/formats/off.py +++ b/d3/model/formats/off.py @@ -1,10 +1,10 @@ -from ..basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face +from ..basemodel import TextModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face from ..mesh import Material, MeshPart def is_off(filename): return filename[-4:] == '.off' -class OFFParser(ModelParser): +class OFFParser(TextModelParser): def __init__(self, up_conversion = None): super().__init__(up_conversion) self.vertex_number = None diff --git a/d3/model/formats/ply.py b/d3/model/formats/ply.py index 8bb970f..22e840a 100644 --- a/d3/model/formats/ply.py +++ b/d3/model/formats/ply.py @@ -1,11 +1,11 @@ import os import PIL -from ..basemodel import ModelParser, Exporter, Vertex, Face, FaceVertex, TexCoord, Material +from ..basemodel import ModelParser, TextModelParser, Exporter, Vertex, Face, FaceVertex, TexCoord, Material def is_ply(filename): return filename[-4:] == '.ply' -class PLYParser(ModelParser): +class PLYParser(TextModelParser): def __init__(self, up_conversion = None): super().__init__(up_conversion) diff --git a/d3/model/formats/stl.py b/d3/model/formats/stl.py index 8c104f3..1e86fee 100644 --- a/d3/model/formats/stl.py +++ b/d3/model/formats/stl.py @@ -1,4 +1,4 @@ -from ..basemodel import ModelParser, Exporter, Vertex, FaceVertex, Face +from ..basemodel import TextModelParser, Exporter, Vertex, FaceVertex, Face from ..mesh import MeshPart import os.path @@ -6,7 +6,7 @@ import os.path def is_stl(filename): return filename[-4:] == '.stl' -class STLParser(ModelParser): +class STLParser(TextModelParser): def __init__(self, up_conversion = None): super().__init__(up_conversion)