Added dynamic module loading

This commit is contained in:
Thomas FORGIONE 2016-12-02 15:52:01 +01:00
parent 47044cb92a
commit e762759216
No known key found for this signature in database
GPG Key ID: 2A210FFC062E00C3
5 changed files with 49 additions and 23 deletions

View File

@ -0,0 +1,4 @@
from os.path import dirname, basename, isfile
import glob
modules = glob.glob(dirname(__file__)+"/*.py")
__all__ = [ basename(f)[:-3] for f in modules if isfile(f)]

View File

@ -1,5 +1,5 @@
from .basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face from ..basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face
from .mesh import Material, MeshPart from ..mesh import Material, MeshPart
from functools import reduce from functools import reduce
import os.path import os.path
import PIL.Image import PIL.Image

View File

@ -1,4 +1,4 @@
from .basemodel import ModelParser, Exporter, Vertex, Face, FaceVertex from ..basemodel import ModelParser, Exporter, Vertex, Face, FaceVertex
def is_ply(filename): def is_ply(filename):
return filename[-4:] == '.ply' return filename[-4:] == '.ply'

View File

@ -1,5 +1,5 @@
from .basemodel import ModelParser, Exporter, Vertex, FaceVertex, Face from ..basemodel import ModelParser, Exporter, Vertex, FaceVertex, Face
from .mesh import MeshPart from ..mesh import MeshPart
import os.path import os.path

View File

@ -1,36 +1,58 @@
from .obj import is_obj, OBJParser, OBJExporter import os
from .ply import is_ply, PLYParser, PLYExporter from importlib import import_module
from .stl import is_stl, STLParser, STLExporter
from . import formats
from .formats import *
from .basemodel import ModelParser, Exporter from .basemodel import ModelParser, Exporter
from types import ModuleType
supported_formats = []
class ModelType:
def __init__(self, typename, inner_module):
self.typename = typename
self.inner_module = inner_module
def test_type(self, file):
return getattr(self.inner_module, 'is_' + self.typename)(file)
def create_parser(self, *args, **kwargs):
return getattr(self.inner_module, self.typename.upper() + 'Parser')(*args, **kwargs)
def create_exporter(self, *args, **kwargs):
return getattr(self.inner_module, self.typename.upper() + 'Exporter')(*args, **kwargs)
def find_type(filename, supported_formats):
for type in supported_formats:
if type.test_type(filename):
return type
for name in formats.__dict__:
if isinstance(formats.__dict__[name], ModuleType) and name != 'glob':
type = ModelType(name, formats.__dict__[name])
supported_formats.append(type)
def load_model(path, up_conversion = None): def load_model(path, up_conversion = None):
parser = None parser = None
type = find_type(path, supported_formats)
if is_obj(path): if type is None:
parser = OBJParser(up_conversion)
elif is_ply(path):
parser = PLYParser(up_conversion)
elif is_stl(path):
parser = STLParser(up_conversion)
else:
raise Exception("File format not supported") raise Exception("File format not supported")
parser = type.create_parser(up_conversion)
parser.parse_file(path) parser.parse_file(path)
return parser return parser
def export_model(model, path): def export_model(model, path):
exporter = None exporter = None
type = find_type(path, supported_formats)
if is_obj(path): if type is None:
exporter = OBJExporter(model) raise Exception('File format is not supported')
elif is_ply(path):
exporter = PLYExporter(model)
elif is_stl(path):
exporter = STLExporter(model)
else:
raise Exception("File format not supported")
exporter = type.create_exporter(model)
return exporter return exporter
def convert(input, output, up_conversion = None): def convert(input, output, up_conversion = None):