Added convert.py

This commit is contained in:
Thomas FORGIONE 2016-11-22 15:23:53 +01:00
parent 14c27eb33b
commit 16e0e1ae00
5 changed files with 36 additions and 59 deletions

View File

@ -1,15 +1,15 @@
# model-converter # 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 # Scripts
A few utilities to manage 3D models : A few utilities to manage 3D models :
- `ply2obj.py` that converts a `.ply` model to a `.obj` model - `convert.py` that converts any type of model to any other
- `obj2ply.py` that converts a `.obj` model to a `.ply` model
- `modelviewer.py` which is a simple script that renders a 3d model - `modelviewer.py` which is a simple script that renders a 3d model
## Formats supported ## Supported format
- Wavefront `.obj` - Wavefront `.obj`
- Stanford `.ply` - Stanford `.ply`

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from .obj import is_obj, OBJParser from .obj import is_obj, OBJParser, OBJExporter
from .ply import is_ply, PLYParser from .ply import is_ply, PLYParser, PLYExporter
def load_model(path): def load_model(path):
parser = None parser = None
@ -16,3 +16,21 @@ def load_model(path):
parser.parse_file(path) parser.parse_file(path)
return parser 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)

View File

@ -4,7 +4,7 @@ import argparse
import os import os
from conv3d.ply import PLYExporter from conv3d.ply import PLYExporter
from conv3d.loadmodel import load_model from conv3d.loadmodel import convert
from functools import partial from functools import partial
def check_path(path, should_exist): def check_path(path, should_exist):
@ -17,9 +17,8 @@ def check_path(path, should_exist):
return path return path
def main(args): def main(args):
parser = load_model(args.input) output = args.output if args.output is not None else '.' + args.type
exporter = PLYExporter(parser) result = convert(args.input, output)
result = str(exporter)
if args.output is None: if args.output is None:
print(result) print(result)
@ -36,6 +35,8 @@ if __name__ == '__main__':
help='Input file (.obj)') help='Input file (.obj)')
parser.add_argument('-o', '--output', metavar='output', parser.add_argument('-o', '--output', metavar='output',
help='Output path') help='Output path')
parser.add_argument('-t', '--type', metavar='type',
help='Export type, useless if output is specified')
args = parser.parse_args() args = parser.parse_args()
args.func(args) args.func(args)

View File

@ -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 v -0.5 -0.5 0.5
f 1 2 5 f 1 2 5
f 2 6 5 f 2 6 5
@ -19,4 +19,3 @@ f 3 4 8
f 3 8 7 f 3 8 7
f 4 1 5 f 4 1 5
f 4 5 8 f 4 5 8

View File

@ -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)