diff -r 3555679d276b -r bec55b021ae7 parse.py --- a/parse.py Thu Aug 26 19:16:25 2021 +0300 +++ b/parse.py Thu Aug 26 19:36:44 2021 +0300 @@ -9,13 +9,13 @@ class Model: def __init__( - self, header, body, *, ldraw_directories, \ + self, header, body, *, context, \ header_size = 0, line_ending_errors = None ): self.header = header self.body = body self.header_size = header_size - self.ldraw_directories = ldraw_directories + self.context = context # contains stuff like library paths self.line_ending_errors = line_ending_errors def filter_by_type(self, type): yield from [ @@ -39,7 +39,15 @@ def has_header(self): return self.header and not isinstance(self.header, header.BadHeader) def find_first_header_object(self, object_type): - return self.body[self.header.first_occurrence[object_type]] + return self.find_header_object(object_type, 0) + def find_header_object(self, object_type, n): + try: + return self.body[self.header.occurrences[object_type][n]] + except IndexError: + raise KeyError(str.format( + '{type} not found in header', + type = object_type + )) def model_vertices( model, @@ -50,7 +58,7 @@ transformation_matrix = complete_matrix(Matrix3x3(), Vertex(0, 0, 0)) if file_cache is None: import filecache - file_cache = filecache.SubfileCache(model.ldraw_directories) + file_cache = filecache.SubfileCache(ldraw_directories = model.context.libraries) for element in model.body: if isinstance(element, linetypes.BasePolygon): for point in element.geometry.vertices: @@ -65,7 +73,7 @@ point @= matrix_4x4 yield point @ transformation_matrix -def read_ldraw(file, *, ldraw_directories, name = ''): +def read_ldraw(file, *, context, name = ''): line_ending_errors = { 'count': 0, 'first-at': None, @@ -92,7 +100,7 @@ header = header_object, body = model_body, header_size = end, - ldraw_directories = ldraw_directories, + context = context, line_ending_errors = line_ending_errors, ) model.name = name