2020-12-07 17:57:45 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
"""
|
2020-12-09 15:05:40 +01:00
|
|
|
obja model for python.
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
class Vector:
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
The class that holds the x, y, and z coordinates of a vector.
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
def __init__(self, x, y, z):
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
Initializes a vector from values.
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.z = z
|
|
|
|
|
|
|
|
def from_array(array):
|
|
|
|
"""
|
|
|
|
Creates a vector from an array of string representing floats.
|
|
|
|
"""
|
|
|
|
return Vector(0, 0, 0).set(array)
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
def set(self, array):
|
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
Sets a vector from an array of string representing floats.
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
|
|
|
self.x = float(array[0])
|
|
|
|
self.y = float(array[1])
|
|
|
|
self.z = float(array[2])
|
2020-12-09 15:31:45 +01:00
|
|
|
return self
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
def translate(self, array):
|
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
Translates a vector from an array of string representing floats.
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
|
|
|
self.x += float(array[0])
|
|
|
|
self.y += float(array[1])
|
|
|
|
self.z += float(array[2])
|
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
def __str__(self):
|
|
|
|
return "({}, {}, {})".format(self.x, self.y, self.z)
|
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
return Vector(self.x + other.x, self.y + other.y, self.z + other.z)
|
|
|
|
|
2021-01-05 14:52:09 +01:00
|
|
|
def __iadd__(self, other):
|
|
|
|
self.x += other.x
|
|
|
|
self.y += other.y
|
|
|
|
self.z += other.z
|
|
|
|
return self
|
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
def __sub__(self, other):
|
|
|
|
return Vector(self.x - other.x, self.y - other.y, self.z - other.z)
|
|
|
|
|
2021-01-05 14:52:09 +01:00
|
|
|
def __isub__(self, other):
|
|
|
|
self.x -= other.x
|
|
|
|
self.y -= other.y
|
|
|
|
self.z -= other.z
|
|
|
|
return self
|
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
def __mul__(self, other):
|
|
|
|
return Vector(self.x * other, self.y * other, self.z * other)
|
|
|
|
|
2021-01-05 14:52:09 +01:00
|
|
|
def __imul__(self, other):
|
|
|
|
self.x *= other
|
|
|
|
self.y *= other
|
|
|
|
self.z *= other
|
|
|
|
return self
|
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
def __truediv__(self, other):
|
|
|
|
return Vector(self.x / other, self.y / other, self.z / other)
|
|
|
|
|
2021-01-05 14:52:09 +01:00
|
|
|
def __itruediv__(self, other):
|
|
|
|
self.x /= other
|
|
|
|
self.y /= other
|
|
|
|
self.z /= other
|
|
|
|
return self
|
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
2020-12-07 17:57:45 +01:00
|
|
|
class Face:
|
|
|
|
"""
|
|
|
|
The class that holds a, b, and c, the indices of the vertices of the face.
|
|
|
|
"""
|
|
|
|
def __init__(self, array):
|
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
Initializes a face from an array of strings representing vector indices (starting at 1)
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
|
|
|
self.set(array)
|
|
|
|
self.visible = True
|
|
|
|
|
|
|
|
def set(self, array):
|
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
Sets a face from an array of strings representing vector indices (starting at 1)
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
2020-12-09 15:05:40 +01:00
|
|
|
self.a = int(array[0].split('/')[0]) - 1
|
|
|
|
self.b = int(array[1].split('/')[0]) - 1
|
|
|
|
self.c = int(array[2].split('/')[0]) - 1
|
2020-12-09 15:31:45 +01:00
|
|
|
return self
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
def test(self, vertices, line = "unknown"):
|
|
|
|
"""
|
|
|
|
Tests if a face references only vertices that exist when the face is declared.
|
|
|
|
"""
|
|
|
|
if self.a >= len(vertices):
|
|
|
|
raise VertexError(self.a + 1, line)
|
|
|
|
if self.b >= len(vertices):
|
|
|
|
raise VertexError(self.b + 1, line)
|
|
|
|
if self.c >= len(vertices):
|
|
|
|
raise VertexError(self.c + 1, line)
|
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
def __str__(self):
|
|
|
|
return "Face({}, {}, {})".format(self.a, self.b, self.c)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
2020-12-07 17:57:45 +01:00
|
|
|
class VertexError(Exception):
|
|
|
|
"""
|
|
|
|
An operation references a vertex that does not exist.
|
|
|
|
"""
|
|
|
|
def __init__(self, index, line):
|
|
|
|
"""
|
|
|
|
Creates the error from index of the referenced vertex and the line where the error occured.
|
|
|
|
"""
|
|
|
|
self.line = line
|
|
|
|
self.index = index
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
Pretty prints the error.
|
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
return f'There is no vector {self.index} (line {self.line})'
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
class FaceError(Exception):
|
|
|
|
"""
|
|
|
|
An operation references a face that does not exist.
|
|
|
|
"""
|
|
|
|
def __init__(self, index, line):
|
|
|
|
"""
|
|
|
|
Creates the error from index of the referenced face and the line where the error occured.
|
|
|
|
"""
|
|
|
|
self.line = line
|
|
|
|
self.index = index
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
Pretty prints the error.
|
|
|
|
"""
|
|
|
|
return f'There is no face {self.index} (line {self.line})'
|
|
|
|
|
|
|
|
class FaceVertexError(Exception):
|
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
An operation references a face vector that does not exist.
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
|
|
|
def __init__(self, index, line):
|
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
Creates the error from index of the referenced face vector and the line where the error occured.
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
|
|
|
self.line = line
|
|
|
|
self.index = index
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
Pretty prints the error.
|
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
return f'Face has no vector {self.index} (line {self.line})'
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
class UnknownInstruction(Exception):
|
|
|
|
"""
|
|
|
|
An instruction is unknown.
|
|
|
|
"""
|
|
|
|
def __init__(self, instruction, line):
|
|
|
|
"""
|
|
|
|
Creates the error from instruction and the line where the error occured.
|
|
|
|
"""
|
|
|
|
self.line = line
|
|
|
|
self.instruction = instruction
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
Pretty prints the error.
|
|
|
|
"""
|
|
|
|
return f'Instruction {self.instruction} unknown (line {self.line})'
|
|
|
|
|
2020-12-09 15:05:40 +01:00
|
|
|
class Model:
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
2020-12-09 15:05:40 +01:00
|
|
|
The OBJA model.
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
"""
|
2020-12-09 15:05:40 +01:00
|
|
|
Intializes an empty model.
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
|
|
|
self.vertices = []
|
|
|
|
self.faces = []
|
|
|
|
self.line = 0
|
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
def get_vector_from_string(self, string):
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
2020-12-09 15:31:45 +01:00
|
|
|
Gets a vector from a string representing the index of the vector, starting at 1.
|
|
|
|
|
|
|
|
To get the vector from its index, simply use model.vertices[i].
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
|
|
|
index = int(string) - 1
|
|
|
|
if index >= len(self.vertices):
|
|
|
|
raise FaceError(index + 1, self.line)
|
|
|
|
return self.vertices[index]
|
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
def get_face_from_string(self, string):
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
|
|
|
Gets a face from a string representing the index of the face, starting at 1.
|
2020-12-09 15:31:45 +01:00
|
|
|
|
|
|
|
To get the face from its index, simply use model.faces[i].
|
2020-12-07 17:57:45 +01:00
|
|
|
"""
|
|
|
|
index = int(string) - 1
|
|
|
|
if index >= len(self.faces):
|
|
|
|
raise FaceError(index + 1, self.line)
|
|
|
|
return self.faces[index]
|
|
|
|
|
2020-12-09 15:05:40 +01:00
|
|
|
def parse_file(self, path):
|
|
|
|
"""
|
|
|
|
Parses an OBJA file.
|
|
|
|
"""
|
|
|
|
with open(path, "r") as file:
|
|
|
|
for line in file.readlines():
|
|
|
|
self.parse_line(line)
|
|
|
|
|
2020-12-07 17:57:45 +01:00
|
|
|
def parse_line(self, line):
|
|
|
|
"""
|
|
|
|
Parses a line of obja file.
|
|
|
|
"""
|
|
|
|
self.line += 1
|
|
|
|
|
|
|
|
split = line.split()
|
|
|
|
|
|
|
|
if len(split) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if split[0] == "v":
|
2020-12-09 15:31:45 +01:00
|
|
|
self.vertices.append(Vector.from_array(split[1:]))
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
elif split[0] == "ev":
|
2020-12-09 15:31:45 +01:00
|
|
|
self.get_vector_from_string(split[1]).set(split[2:])
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
elif split[0] == "tv":
|
2020-12-09 15:31:45 +01:00
|
|
|
self.get_vector_from_string(split[1]).translate(split[2:])
|
2020-12-07 17:57:45 +01:00
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
elif split[0] == "f" or split[0] == "tf":
|
|
|
|
for i in range(1, len(split) - 2):
|
|
|
|
face = Face(split[i:i+3])
|
|
|
|
face.test(self.vertices, self.line)
|
|
|
|
self.faces.append(face)
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
elif split[0] == "ts":
|
|
|
|
for i in range(1, len(split) - 2):
|
|
|
|
if i % 2 == 1:
|
|
|
|
face = Face([split[i], split[i + 1], split[i + 2]])
|
|
|
|
else:
|
|
|
|
face = Face([split[i], split[i + 2], split[i + 1]])
|
|
|
|
face.test(self.vertices, self.line)
|
|
|
|
self.faces.append(face)
|
|
|
|
|
|
|
|
elif split[0] == "ef":
|
2020-12-09 15:31:45 +01:00
|
|
|
self.get_face_from_string(split[1]).set(split[2:])
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
elif split[0] == "efv":
|
2020-12-09 15:31:45 +01:00
|
|
|
face = self.get_face_from_string(split[1])
|
|
|
|
vector = int(split[2])
|
2020-12-07 17:57:45 +01:00
|
|
|
new_index = int(split[3]) - 1
|
2020-12-09 15:31:45 +01:00
|
|
|
if vector == 1:
|
2020-12-07 17:57:45 +01:00
|
|
|
face.a = new_index
|
2020-12-09 15:31:45 +01:00
|
|
|
elif vector == 2:
|
2020-12-07 17:57:45 +01:00
|
|
|
face.b = new_index
|
2020-12-09 15:31:45 +01:00
|
|
|
elif vector == 3:
|
2020-12-07 17:57:45 +01:00
|
|
|
face.c = new_index
|
|
|
|
else:
|
2020-12-09 15:31:45 +01:00
|
|
|
raise FaceVertexError(vector, self.line)
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
elif split[0] == "df":
|
2020-12-09 15:31:45 +01:00
|
|
|
self.get_face_from_string(split[1]).visible = False
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
elif split[0] == "#":
|
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
2020-12-09 15:05:40 +01:00
|
|
|
return
|
|
|
|
# raise UnknownInstruction(split[0], self.line)
|
2020-12-07 17:57:45 +01:00
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
def parse_file(path):
|
|
|
|
"""
|
|
|
|
Parses a file and returns the model.
|
|
|
|
"""
|
|
|
|
model = Model()
|
|
|
|
model.parse_file(path)
|
|
|
|
return model
|
|
|
|
|
2020-12-07 17:57:45 +01:00
|
|
|
def main():
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
print("obja needs a path to an obja file")
|
|
|
|
return
|
|
|
|
|
2020-12-09 15:31:45 +01:00
|
|
|
model = parse_file(sys.argv[1])
|
|
|
|
print(model.vertices)
|
|
|
|
print(model.faces)
|
2020-12-07 17:57:45 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|