Working OBJ parser
This commit is contained in:
parent
88da9df256
commit
cd98c29e00
32
obja.py
32
obja.py
|
@ -3,7 +3,7 @@
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
"""
|
"""
|
||||||
obja parser for python.
|
obja model for python.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class Vertex:
|
class Vertex:
|
||||||
|
@ -47,9 +47,9 @@ class Face:
|
||||||
"""
|
"""
|
||||||
Sets a face from an array of strings representing vertex indices (starting at 1)
|
Sets a face from an array of strings representing vertex indices (starting at 1)
|
||||||
"""
|
"""
|
||||||
self.a = int(array[0]) - 1
|
self.a = int(array[0].split('/')[0]) - 1
|
||||||
self.b = int(array[1]) - 1
|
self.b = int(array[1].split('/')[0]) - 1
|
||||||
self.c = int(array[2]) - 1
|
self.c = int(array[2].split('/')[0]) - 1
|
||||||
|
|
||||||
def test(self, vertices, line = "unknown"):
|
def test(self, vertices, line = "unknown"):
|
||||||
"""
|
"""
|
||||||
|
@ -134,13 +134,13 @@ class UnknownInstruction(Exception):
|
||||||
"""
|
"""
|
||||||
return f'Instruction {self.instruction} unknown (line {self.line})'
|
return f'Instruction {self.instruction} unknown (line {self.line})'
|
||||||
|
|
||||||
class Parser:
|
class Model:
|
||||||
"""
|
"""
|
||||||
The OBJA parser.
|
The OBJA model.
|
||||||
"""
|
"""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""
|
"""
|
||||||
Intializes an empty parser.
|
Intializes an empty model.
|
||||||
"""
|
"""
|
||||||
self.vertices = []
|
self.vertices = []
|
||||||
self.faces = []
|
self.faces = []
|
||||||
|
@ -164,6 +164,14 @@ class Parser:
|
||||||
raise FaceError(index + 1, self.line)
|
raise FaceError(index + 1, self.line)
|
||||||
return self.faces[index]
|
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):
|
def parse_line(self, line):
|
||||||
"""
|
"""
|
||||||
Parses a line of obja file.
|
Parses a line of obja file.
|
||||||
|
@ -227,17 +235,17 @@ class Parser:
|
||||||
return
|
return
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise UnknownInstruction(split[0], self.line)
|
return
|
||||||
|
# raise UnknownInstruction(split[0], self.line)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) == 1:
|
if len(sys.argv) == 1:
|
||||||
print("obja needs a path to an obja file")
|
print("obja needs a path to an obja file")
|
||||||
return
|
return
|
||||||
|
|
||||||
parser = Parser()
|
model = Model()
|
||||||
with open(sys.argv[1], "r") as file:
|
model.parse_file(sys.argv[1])
|
||||||
for line in file.readlines():
|
|
||||||
parser.parse_line(line)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue