dist: Use Panda loader system for converting models to .bam

Set bam_model_extensions in order to specify which formats are automatically converted to .bam

Fixes #714
This commit is contained in:
rdb 2022-12-06 19:38:37 +01:00
parent 765d76740d
commit 3f3fd74f86
1 changed files with 48 additions and 0 deletions

View File

@ -67,6 +67,44 @@ def _parse_dict(input):
return d
def _register_python_loaders():
# We need this method so that we don't depend on direct.showbase.Loader.
if getattr(_register_python_loaders, 'done', None):
return
_register_python_loaders.done = True
try:
import pkg_resources
except ImportError:
return
registry = p3d.LoaderFileTypeRegistry.getGlobalPtr()
for entry_point in pkg_resources.iter_entry_points('panda3d.loaders'):
registry.register_deferred_type(entry_point)
def _model_to_bam(_build_cmd, srcpath, dstpath):
if dstpath.endswith('.gz') or dstpath.endswith('.pz'):
dstpath = dstpath[:-3]
dstpath = dstpath + '.bam'
src_fn = p3d.Filename.from_os_specific(srcpath)
dst_fn = p3d.Filename.from_os_specific(dstpath)
_register_python_loaders()
loader = p3d.Loader.get_global_ptr()
options = p3d.LoaderOptions(p3d.LoaderOptions.LF_report_errors |
p3d.LoaderOptions.LF_no_ram_cache)
node = loader.load_sync(src_fn, options)
if not node:
raise IOError('Failed to load model: %s' % (srcpath))
if not p3d.NodePath(node).write_bam_file(dst_fn):
raise IOError('Failed to write .bam file: %s' % (dstpath))
def egg2bam(_build_cmd, srcpath, dstpath):
if dstpath.endswith('.gz') or dstpath.endswith('.pz'):
@ -259,6 +297,7 @@ class build_apps(setuptools.Command):
'https://archive.panda3d.org/thirdparty',
]
self.file_handlers = {}
self.bam_model_extensions = []
self.exclude_dependencies = [
# Windows
'kernel32.dll', 'user32.dll', 'wsock32.dll', 'ws2_32.dll',
@ -414,6 +453,15 @@ class build_apps(setuptools.Command):
for glob in self.exclude_dependencies:
glob.case_sensitive = False
# bam_model_extensions registers a 2bam handler for each given extension.
# They can override a default handler, but not a custom handler.
if self.bam_model_extensions:
for ext in self.bam_model_extensions:
ext = '.' + ext.lstrip('.')
assert ext not in self.file_handlers, \
'Extension {} occurs in both file_handlers and bam_model_extensions!'.format(ext)
self.file_handlers[ext] = _model_to_bam
tmp = self.default_file_handlers.copy()
tmp.update(self.file_handlers)
self.file_handlers = tmp