From cd98c29e00c5eb507489e00c765fc3dde29db860 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Wed, 9 Dec 2020 15:05:40 +0100 Subject: [PATCH] Working OBJ parser --- obja.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/obja.py b/obja.py index ecdf0e2..62a6f7e 100755 --- a/obja.py +++ b/obja.py @@ -3,7 +3,7 @@ import sys """ -obja parser for python. +obja model for python. """ class Vertex: @@ -47,9 +47,9 @@ class Face: """ Sets a face from an array of strings representing vertex indices (starting at 1) """ - self.a = int(array[0]) - 1 - self.b = int(array[1]) - 1 - self.c = int(array[2]) - 1 + self.a = int(array[0].split('/')[0]) - 1 + self.b = int(array[1].split('/')[0]) - 1 + self.c = int(array[2].split('/')[0]) - 1 def test(self, vertices, line = "unknown"): """ @@ -134,13 +134,13 @@ class UnknownInstruction(Exception): """ return f'Instruction {self.instruction} unknown (line {self.line})' -class Parser: +class Model: """ - The OBJA parser. + The OBJA model. """ def __init__(self): """ - Intializes an empty parser. + Intializes an empty model. """ self.vertices = [] self.faces = [] @@ -164,6 +164,14 @@ class Parser: raise FaceError(index + 1, self.line) return self.faces[index] + def parse_file(self, path): + """ + Parses an OBJA file. + """ + with open(path, "r") as file: + for line in file.readlines(): + self.parse_line(line) + def parse_line(self, line): """ Parses a line of obja file. @@ -227,17 +235,17 @@ class Parser: return else: - raise UnknownInstruction(split[0], self.line) + return + # raise UnknownInstruction(split[0], self.line) def main(): if len(sys.argv) == 1: print("obja needs a path to an obja file") return - parser = Parser() - with open(sys.argv[1], "r") as file: - for line in file.readlines(): - parser.parse_line(line) + model = Model() + model.parse_file(sys.argv[1]) + if __name__ == "__main__": main()