From 16e0e1ae005646a6be995d784d10a6beb2d98f22 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Tue, 22 Nov 2016 15:23:53 +0100 Subject: [PATCH] Added convert.py --- README.md | 8 ++++---- conv3d/loadmodel.py | 22 +++++++++++++++++++-- obj2ply.py => convert.py | 9 +++++---- examples/cube.obj | 15 +++++++-------- ply2obj.py | 41 ---------------------------------------- 5 files changed, 36 insertions(+), 59 deletions(-) rename obj2ply.py => convert.py (80%) delete mode 100755 ply2obj.py diff --git a/README.md b/README.md index fa4a9dd..0900ddb 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ # model-converter -This project aims to be a simple, lightweight, and useful 3D model editor +This project aims to be a simple, lightweight, and useful 3D model editor. +For the moment, only `obj` and `ply` ascii models are supported. # Scripts A few utilities to manage 3D models : - - `ply2obj.py` that converts a `.ply` model to a `.obj` model - - `obj2ply.py` that converts a `.obj` model to a `.ply` model + - `convert.py` that converts any type of model to any other - `modelviewer.py` which is a simple script that renders a 3d model -## Formats supported +## Supported format - Wavefront `.obj` - Stanford `.ply` diff --git a/conv3d/loadmodel.py b/conv3d/loadmodel.py index 6271bd8..74f7f92 100644 --- a/conv3d/loadmodel.py +++ b/conv3d/loadmodel.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -from .obj import is_obj, OBJParser -from .ply import is_ply, PLYParser +from .obj import is_obj, OBJParser, OBJExporter +from .ply import is_ply, PLYParser, PLYExporter def load_model(path): parser = None @@ -16,3 +16,21 @@ def load_model(path): parser.parse_file(path) return parser + +def export_model(model, path): + exporter = None + + if is_obj(path): + exporter = OBJExporter(model) + elif is_ply(path): + exporter = PLYExporter(model) + else: + raise Exception("File format not supported") + + return exporter + +def convert(input, output): + model = load_model(input) + exporter = export_model(model, output) + return str(exporter) + diff --git a/obj2ply.py b/convert.py similarity index 80% rename from obj2ply.py rename to convert.py index 68c92a9..e5da46d 100755 --- a/obj2ply.py +++ b/convert.py @@ -4,7 +4,7 @@ import argparse import os from conv3d.ply import PLYExporter -from conv3d.loadmodel import load_model +from conv3d.loadmodel import convert from functools import partial def check_path(path, should_exist): @@ -17,9 +17,8 @@ def check_path(path, should_exist): return path def main(args): - parser = load_model(args.input) - exporter = PLYExporter(parser) - result = str(exporter) + output = args.output if args.output is not None else '.' + args.type + result = convert(args.input, output) if args.output is None: print(result) @@ -36,6 +35,8 @@ if __name__ == '__main__': help='Input file (.obj)') parser.add_argument('-o', '--output', metavar='output', help='Output path') + parser.add_argument('-t', '--type', metavar='type', + help='Export type, useless if output is specified') args = parser.parse_args() args.func(args) diff --git a/examples/cube.obj b/examples/cube.obj index 58e70e9..905ecdb 100644 --- a/examples/cube.obj +++ b/examples/cube.obj @@ -1,11 +1,11 @@ -v 0.5 -0.5 -0.5 -v 0.5 0.5 -0.5 -v -0.5 0.5 -0.5 +v 0.5 -0.5 -0.5 +v 0.5 0.5 -0.5 +v -0.5 0.5 -0.5 v -0.5 -0.5 -0.5 -v 0.5 -0.5 0.5 -v 0.5 0.5 0.5 -v -0.5 0.5 0.5 -v -0.5 -0.5 0.5 +v 0.5 -0.5 0.5 +v 0.5 0.5 0.5 +v -0.5 0.5 0.5 +v -0.5 -0.5 0.5 f 1 2 5 f 2 6 5 @@ -19,4 +19,3 @@ f 3 4 8 f 3 8 7 f 4 1 5 f 4 5 8 - diff --git a/ply2obj.py b/ply2obj.py deleted file mode 100755 index b442d42..0000000 --- a/ply2obj.py +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env python3 - -import argparse -import os - -from conv3d.obj import OBJExporter -from conv3d.loadmodel import load_model -from functools import partial - -def check_path(path, should_exist): - """ Check that a path (file or folder) exists or not and return it. - """ - path = os.path.normpath(path) - if should_exist != os.path.exists(path): - msg = "path " + ("does not" if should_exist else "already") + " exist: " + path - raise argparse.ArgumentTypeError(msg) - return path - -def main(args): - parser = load_model(args.input) - exporter = OBJExporter(parser) - result = str(exporter) - - if args.output is None: - print(result) - else: - with open(args.output, 'w') as f: - f.write(result) - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.set_defaults(func=main) - parser.add_argument('-v', '--version', action='version', version='1.0') - parser.add_argument('-i', '--input', metavar='input', - type=partial(check_path, should_exist=True), default=None, - help='Input file (.obj)') - parser.add_argument('-o', '--output', metavar='output', - help='Output path') - args = parser.parse_args() - args.func(args) -