From 913583aeba14ad3f3944eeb4f103fbacc60ea728 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Wed, 18 Jan 2017 15:18:31 +0100 Subject: [PATCH] Update formats --- d3/model/formats/off.py | 21 +++++++++++++++++++-- d3/model/formats/ply.py | 25 +++++++++++++++++++++++++ d3/model/formats/stl.py | 19 +++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/d3/model/formats/off.py b/d3/model/formats/off.py index af5b573..9bf0882 100644 --- a/d3/model/formats/off.py +++ b/d3/model/formats/off.py @@ -2,9 +2,16 @@ from ..basemodel import TextModelParser, Exporter, Vertex, TexCoord, Normal, Fac from ..mesh import Material, MeshPart def is_off(filename): + """Checks that the file is a .off file + + Only checks the extension of the file + :param filename: path to the file + """ return filename[-4:] == '.off' class OFFParser(TextModelParser): + """Parser that parses a .off file + """ def __init__(self, up_conversion = None): super().__init__(up_conversion) self.vertex_number = None @@ -12,10 +19,13 @@ class OFFParser(TextModelParser): self.edge_number = None def parse_line(self, string): + """Parses a line of .off file + :param string: the line to parse + """ split = string.split() - if string == 'OFF': + if string == '' or string == 'OFF': pass elif self.vertex_number is None: # The first will be the header @@ -30,11 +40,18 @@ class OFFParser(TextModelParser): class OFFExporter(Exporter): + """Exporter to .off format + """ def __init__(self, model): + """Creates an exporter from the model + + :param model: Model to export + """ super().__init__(model) def __str__(self): - + """Exports the model + """ faces = sum(map(lambda x: x.faces, self.model.parts), []) string = "OFF\n{} {} {}".format(len(self.model.vertices), len(faces), 0) + '\n' diff --git a/d3/model/formats/ply.py b/d3/model/formats/ply.py index b63b5ba..cfa4d04 100644 --- a/d3/model/formats/ply.py +++ b/d3/model/formats/ply.py @@ -9,10 +9,19 @@ class UnkownTypeError(Exception): self.message = message def is_ply(filename): + """Checks that the file is a .ply file + + Only checks the extension of the file + :param filename: path to the file + """ return filename[-4:] == '.ply' # List won't work with this function def _ply_type_size(type): + """Returns the size of a ply property + + :param type: a string that is in a ply element + """" if type == 'char' or type == 'uchar': return 1 elif type == 'short' or type == 'ushort': @@ -27,6 +36,10 @@ def _ply_type_size(type): raise UnkownTypeError('Type ' + type + ' is unknown') def ply_type_size(type): + """Returns the list containing the sizes of the elements + + :param type: a string that is in a ply element + """ split = type.split() if len(split) == 1: @@ -40,6 +53,12 @@ def ply_type_size(type): def bytes_to_element(type, bytes, byteorder = 'little'): + """Returns a python object parsed from bytes + + :param type: the type of the object to parse + :param bytes: the bytes to read + :param byteorder: little or big endian + """ if type == 'char': return ord(struct.unpack('