2016-12-02 14:52:01 +00:00
|
|
|
import os
|
|
|
|
from importlib import import_module
|
|
|
|
|
|
|
|
from . import formats
|
|
|
|
from .formats import *
|
2016-11-25 13:56:37 +00:00
|
|
|
from .basemodel import ModelParser, Exporter
|
2016-11-22 10:27:42 +00:00
|
|
|
|
2016-12-02 14:52:01 +00:00
|
|
|
from types import ModuleType
|
|
|
|
|
|
|
|
supported_formats = []
|
|
|
|
|
|
|
|
class ModelType:
|
2017-01-18 14:07:22 +00:00
|
|
|
"""Represents a type of coding of 3D object, and the module enabling
|
|
|
|
parsing and exporting
|
|
|
|
"""
|
2016-12-02 14:52:01 +00:00
|
|
|
def __init__(self, typename, inner_module):
|
2017-01-18 14:07:22 +00:00
|
|
|
"""Creates a ModelType
|
|
|
|
|
|
|
|
:param typename: the name of the 3D format
|
|
|
|
:param inner_module: the module that will parse and export the format
|
|
|
|
"""
|
2016-12-02 14:52:01 +00:00
|
|
|
self.typename = typename
|
|
|
|
self.inner_module = inner_module
|
|
|
|
|
|
|
|
def test_type(self, file):
|
2017-01-18 14:07:22 +00:00
|
|
|
"""Tests if a file has the correct type
|
|
|
|
|
|
|
|
:param file: path to the file to test
|
|
|
|
"""
|
2016-12-02 14:52:01 +00:00
|
|
|
return getattr(self.inner_module, 'is_' + self.typename)(file)
|
|
|
|
|
|
|
|
def create_parser(self, *args, **kwargs):
|
2017-01-18 14:07:22 +00:00
|
|
|
"""Creates a parser of the current type
|
|
|
|
"""
|
2016-12-02 14:52:01 +00:00
|
|
|
return getattr(self.inner_module, self.typename.upper() + 'Parser')(*args, **kwargs)
|
|
|
|
|
|
|
|
def create_exporter(self, *args, **kwargs):
|
2017-01-18 14:07:22 +00:00
|
|
|
"""Creates an exporter of the current type
|
|
|
|
"""
|
2016-12-02 14:52:01 +00:00
|
|
|
return getattr(self.inner_module, self.typename.upper() + 'Exporter')(*args, **kwargs)
|
|
|
|
|
|
|
|
def find_type(filename, supported_formats):
|
2017-01-18 14:07:22 +00:00
|
|
|
"""Find the correct type from a filename
|
|
|
|
|
|
|
|
:param filename: path to the file
|
|
|
|
:param supported_formats: list of formats that we have modules for
|
|
|
|
"""
|
2016-12-02 14:52:01 +00:00
|
|
|
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)
|
|
|
|
|
2016-11-30 14:37:58 +00:00
|
|
|
def load_model(path, up_conversion = None):
|
2017-01-18 14:07:22 +00:00
|
|
|
"""Loads a model from a path
|
|
|
|
|
|
|
|
:param path: path to the file to load
|
|
|
|
:param up_conversion: conversion of up vectors
|
|
|
|
"""
|
2016-11-22 10:27:42 +00:00
|
|
|
parser = None
|
2016-12-02 14:52:01 +00:00
|
|
|
type = find_type(path, supported_formats)
|
2016-11-22 10:27:42 +00:00
|
|
|
|
2016-12-02 14:52:01 +00:00
|
|
|
if type is None:
|
2018-02-23 10:31:58 +00:00
|
|
|
raise Exception("File format not supported \"" + str(type) + "\"")
|
2016-11-22 10:27:42 +00:00
|
|
|
|
2016-12-02 14:52:01 +00:00
|
|
|
parser = type.create_parser(up_conversion)
|
2016-11-22 10:27:42 +00:00
|
|
|
parser.parse_file(path)
|
|
|
|
|
|
|
|
return parser
|
2016-11-22 14:23:53 +00:00
|
|
|
|
|
|
|
def export_model(model, path):
|
2017-01-18 14:07:22 +00:00
|
|
|
"""Exports a model to a path
|
|
|
|
|
|
|
|
:param model: model to export
|
|
|
|
:param path: path to save the model
|
|
|
|
"""
|
2016-11-22 14:23:53 +00:00
|
|
|
exporter = None
|
2016-12-02 14:52:01 +00:00
|
|
|
type = find_type(path, supported_formats)
|
2016-11-22 14:23:53 +00:00
|
|
|
|
2016-12-02 14:52:01 +00:00
|
|
|
if type is None:
|
|
|
|
raise Exception('File format is not supported')
|
2016-11-22 14:23:53 +00:00
|
|
|
|
2016-12-02 14:52:01 +00:00
|
|
|
exporter = type.create_exporter(model)
|
2016-11-22 14:23:53 +00:00
|
|
|
return exporter
|
|
|
|
|
2016-11-30 14:37:58 +00:00
|
|
|
def convert(input, output, up_conversion = None):
|
2017-01-18 14:07:22 +00:00
|
|
|
"""Converts a model
|
|
|
|
|
|
|
|
:param input: path of the input model
|
|
|
|
:param output: path to the output
|
|
|
|
:param up_conversion: convert the up vector
|
|
|
|
"""
|
2016-11-30 14:37:58 +00:00
|
|
|
model = load_model(input, up_conversion)
|
2016-11-22 14:23:53 +00:00
|
|
|
exporter = export_model(model, output)
|
|
|
|
return str(exporter)
|
|
|
|
|