from logging import info
from os import system, walk, makedirs
-from os.path import exists, dirname, join, basename
+from os.path import exists, dirname, join, basename, splitext
from sys import executable
+from functools import reduce
from multiprocessing import Pool
from hashlib import md5
from shutil import copyfile, rmtree
self.__manifest = ModelsManifest()
def build(self):
- blend2bam_args = []
+ self.__compute_files_to_build()
+ return self.__run_build()
+
+ def __compute_files_to_build(self):
+ self.__blend_files = []
for root, _, file_names in walk(self.__blend_path):
- blend_files = [f for f in file_names if f.endswith('.blend')]
+ b = [f for f in file_names if f.endswith('.blend')]
if '/prototypes/' in root:
- blend_files = []
- blend_files = [join(root, f) for f in blend_files if self.__to_be_built(join(root, f))]
- blend2bam_args += blend_files
- with Pool() as p:
- just_built = p.map(self.blend2bam, blend2bam_args)
+ b = []
+ b = [join(root, f)
+ for f in b
+ if to_be_built(join(root, f), [join(root, f)])]
+ self.__blend_files += b
+
+ def __run_build(self):
+ with Pool(self.__cores) as p:
+ just_built = p.map(self.blend2bam, self.__blend_files)
p.close()
p.join()
- just_built = [assets for blend_list in just_built for assets in blend_list]
+ just_built = [assets for b in just_built for assets in b]
self.__manifest.update(just_built)
return just_built
def blend2bam(self, file_path):
built = []
- if self.__to_be_built(file_path):
+ if to_be_built(self.gltf_name(file_path), [file_path]):
+ self.__clean_gltf_dir(file_path)
built += self.__blend2gltf(file_path)
built += self.__gltf2bam(file_path)
return built
- def __to_be_built(self, file_path):
- self.__clean_gltf_dir(file_path)
- return to_be_built(self.gltf_name(file_path), [file_path])
-
def __clean_gltf_dir(self, file_path):
gltf_dir = dirname(file_path)
- gltf_dir = file_path.replace('assets/models/blend/', 'assets/models/gltf/')
+ gltf_dir = gltf_dir.replace('assets/models/blend/', 'assets/models/gltf/')
rmtree(gltf_dir, ignore_errors=True)
@staticmethod
self.__file_path = file_path
def export(self):
- gltf_dir, filename = dirname(self.__file_path), basename(self.__file_path)
+ gltf_dir, file_name = dirname(self.__file_path), basename(self.__file_path)
gltf_dir = gltf_dir.replace('assets/models/blend/', 'assets/models/gltf/')
- command = 'blender %s --background --python ya2/build/blend2gltf.py -- %s.gltf'
- command = command % (self.__file_path, gltf_dir + '/' + filename[:-6])
makedirs(gltf_dir, exist_ok=True)
+ file_path = splitext(gltf_dir + '/' + file_name)[0]
+ command = f'blender {self.__file_path} --background --python ya2/build/blend2gltf.py -- {file_path}.gltf'
system(command)
return [self.__file_path, ModelsBuilder.gltf_name(self.__file_path)]
class Gltf2Bam:
def __init__(self, file_path):
- self.__file_path = file_path
+ self.__gltf_name = (file_path[:-5] + 'gltf').replace('/blend/', '/gltf/', 1)
+ self.__bam_name = (file_path[:-5] + 'bam').replace('/blend/', '/bam/', 1)
def export(self):
self.__build_dds_from_jpg_and_png()
return self.__export_bam()
def __build_dds_from_jpg_and_png(self):
- self.__gltf_name = (self.__file_path[:-5] + 'gltf').replace('/blend/', '/gltf/', 1)
- gltf_dir = dirname(self.__gltf_name)
- self.__bam_name = (self.__file_path[:-5] + 'bam').replace('/blend/', '/bam/', 1)
- copyfile(self.__gltf_name, self.__gltf_name + '.tmp')
with open(self.__gltf_name) as f: self.__lines = f.readlines()
- self.__jpg_and_png = []
+ self.__jpg_png_files = []
for l in self.__lines:
if ('.png' in l or '.jpg' in l) and '"uri"' in l:
- rln = l[l.index('"uri"') + 9:].rstrip(',\n"')
- tname = f'{gltf_dir}/{rln}'
- self.__jpg_and_png += [tname.replace('/models/blend/', '/models/gltf/', 1)]
- for d in self.__jpg_and_png:
- target = d.replace('/gltf/', '/bam/', 1)
- target = target.replace('.png', '.dds').replace('.jpg', '.dds')
- makedirs(dirname(target), exist_ok=True)
- info('convert %s %s' % (d, target))
- system('convert %s %s' % (d, target))
+ r = l[l.index('"uri"') + 9:].rstrip(',\n"')
+ t = f'{dirname(self.__gltf_name)}/{r}'
+ self.__jpg_png_files += [t.replace('/models/blend/', '/models/gltf/', 1)]
+ self.__create_dds_files()
+
+ def __create_dds_files(self):
+ for d in self.__jpg_png_files:
+ t = d.replace('/gltf/', '/bam/', 1)
+ t = t.replace('.png', '.dds').replace('.jpg', '.dds')
+ makedirs(dirname(t), exist_ok=True)
+ info(f'convert {d} {t}')
+ system(f'convert {d} {t}')
def __rewrite_gltf(self):
- def rpl(lin):
- lin = lin.replace('.png', '.dds')
- lin = lin.replace('.jpg', '.dds')
- lin = lin.replace('/png', '/dds')
- return lin.replace('/jpg', '/dds')
- with open(self.__gltf_name, 'w') as f: f.write(''.join([rpl(line) for line in self.__lines]))
+ def r(lin):
+ e = [('.png', '.dds'), ('.jpg', '.dds'),
+ ('/png', '/dds'), ('/jpg', '/dds')]
+ return reduce(lambda s, p: s.replace(*p), e, lin)
+ with open(self.__gltf_name, 'w') as f:
+ f.write(''.join([r(line) for line in self.__lines]))
def __export_bam(self):
makedirs(dirname(self.__bam_name), exist_ok=True)
- if to_be_built(self.__bam_name, self.__jpg_and_png):
+ if to_be_built(self.__bam_name, self.__jpg_png_files):
venv_path = dirname(executable)
gltf2bam_path = join(venv_path, 'gltf2bam')
system(f'{gltf2bam_path} {self.__gltf_name} {self.__bam_name}')
- return [self.__bam_name] + self.__jpg_and_png
+ return [self.__bam_name] + self.__jpg_png_files