Added dynamic module loading
This commit is contained in:
parent
47044cb92a
commit
e762759216
|
@ -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)]
|
|
@ -1,5 +1,5 @@
|
|||
from .basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face
|
||||
from .mesh import Material, MeshPart
|
||||
from ..basemodel import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face
|
||||
from ..mesh import Material, MeshPart
|
||||
from functools import reduce
|
||||
import os.path
|
||||
import PIL.Image
|
|
@ -1,4 +1,4 @@
|
|||
from .basemodel import ModelParser, Exporter, Vertex, Face, FaceVertex
|
||||
from ..basemodel import ModelParser, Exporter, Vertex, Face, FaceVertex
|
||||
|
||||
def is_ply(filename):
|
||||
return filename[-4:] == '.ply'
|
|
@ -1,5 +1,5 @@
|
|||
from .basemodel import ModelParser, Exporter, Vertex, FaceVertex, Face
|
||||
from .mesh import MeshPart
|
||||
from ..basemodel import ModelParser, Exporter, Vertex, FaceVertex, Face
|
||||
from ..mesh import MeshPart
|
||||
|
||||
import os.path
|
||||
|
|
@ -1,36 +1,58 @@
|
|||
from .obj import is_obj, OBJParser, OBJExporter
|
||||
from .ply import is_ply, PLYParser, PLYExporter
|
||||
from .stl import is_stl, STLParser, STLExporter
|
||||
import os
|
||||
from importlib import import_module
|
||||
|
||||
from . import formats
|
||||
from .formats import *
|
||||
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):
|
||||
parser = None
|
||||
type = find_type(path, supported_formats)
|
||||
|
||||
if is_obj(path):
|
||||
parser = OBJParser(up_conversion)
|
||||
elif is_ply(path):
|
||||
parser = PLYParser(up_conversion)
|
||||
elif is_stl(path):
|
||||
parser = STLParser(up_conversion)
|
||||
else:
|
||||
if type is None:
|
||||
raise Exception("File format not supported")
|
||||
|
||||
parser = type.create_parser(up_conversion)
|
||||
parser.parse_file(path)
|
||||
|
||||
return parser
|
||||
|
||||
def export_model(model, path):
|
||||
exporter = None
|
||||
type = find_type(path, supported_formats)
|
||||
|
||||
if is_obj(path):
|
||||
exporter = OBJExporter(model)
|
||||
elif is_ply(path):
|
||||
exporter = PLYExporter(model)
|
||||
elif is_stl(path):
|
||||
exporter = STLExporter(model)
|
||||
else:
|
||||
raise Exception("File format not supported")
|
||||
if type is None:
|
||||
raise Exception('File format is not supported')
|
||||
|
||||
exporter = type.create_exporter(model)
|
||||
return exporter
|
||||
|
||||
def convert(input, output, up_conversion = None):
|
||||
|
|
Loading…
Reference in New Issue