From e5da208ae78560d6d6610897bcb6729870810044 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Tue, 22 Nov 2016 11:27:42 +0100 Subject: [PATCH] Added function load_model --- conv3d/loadmodel.py | 18 ++++++++++++++++++ conv3d/model.py | 1 + conv3d/obj.py | 5 ++++- conv3d/ply.py | 4 +++- obj2ply.py | 5 ++--- ply2obj.py | 5 ++--- 6 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 conv3d/loadmodel.py diff --git a/conv3d/loadmodel.py b/conv3d/loadmodel.py new file mode 100644 index 0000000..6271bd8 --- /dev/null +++ b/conv3d/loadmodel.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +from .obj import is_obj, OBJParser +from .ply import is_ply, PLYParser + +def load_model(path): + parser = None + + if is_obj(path): + parser = OBJParser() + elif is_ply(path): + parser = PLYParser() + else: + raise Exception("File format not supported") + + parser.parse_file(path) + + return parser diff --git a/conv3d/model.py b/conv3d/model.py index fbd0fbc..4649747 100644 --- a/conv3d/model.py +++ b/conv3d/model.py @@ -74,3 +74,4 @@ class Exporter: def __init__(self, model): self.model = model + diff --git a/conv3d/obj.py b/conv3d/obj.py index c1c941c..943ae00 100644 --- a/conv3d/obj.py +++ b/conv3d/obj.py @@ -3,6 +3,9 @@ from .model import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face from functools import reduce +def is_obj(filename): + return filename[-4:] == '.obj' + class OBJParser(ModelParser): def __init__(self): @@ -69,7 +72,7 @@ class OBJExporter(Exporter): elif v.texture is not None: sub_arr.append(str(int(v.texture) + 1)) if v.normal is not None: - sub_arr.append(str(int(v.normal + 1))) + sub_arr.append(str(int(v.normal) + 1)) arr.append('/'.join(sub_arr)) string += ' '.join(arr) + '\n' diff --git a/conv3d/ply.py b/conv3d/ply.py index 7f09924..0cc2f48 100644 --- a/conv3d/ply.py +++ b/conv3d/ply.py @@ -2,6 +2,9 @@ from .model import ModelParser, Exporter, Vertex, Face, FaceVertex +def is_ply(filename): + return filename[-4:] == '.ply' + class PLYParser(ModelParser): def __init__(self): @@ -102,4 +105,3 @@ class PLYExporter(Exporter): return string - diff --git a/obj2ply.py b/obj2ply.py index 99342ed..68c92a9 100755 --- a/obj2ply.py +++ b/obj2ply.py @@ -3,8 +3,8 @@ import argparse import os -from conv3d.obj import OBJParser from conv3d.ply import PLYExporter +from conv3d.loadmodel import load_model from functools import partial def check_path(path, should_exist): @@ -17,8 +17,7 @@ def check_path(path, should_exist): return path def main(args): - parser = OBJParser() - parser.parse_file(args.input) + parser = load_model(args.input) exporter = PLYExporter(parser) result = str(exporter) diff --git a/ply2obj.py b/ply2obj.py index 03e4b56..b442d42 100755 --- a/ply2obj.py +++ b/ply2obj.py @@ -4,7 +4,7 @@ import argparse import os from conv3d.obj import OBJExporter -from conv3d.ply import PLYParser +from conv3d.loadmodel import load_model from functools import partial def check_path(path, should_exist): @@ -17,8 +17,7 @@ def check_path(path, should_exist): return path def main(args): - parser = PLYParser() - parser.parse_file(args.input) + parser = load_model(args.input) exporter = OBJExporter(parser) result = str(exporter)