From 3f3fd74f869b21b249e76d3068cb1735afa807ae Mon Sep 17 00:00:00 2001 From: rdb Date: Tue, 6 Dec 2022 19:38:37 +0100 Subject: [PATCH] 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 --- direct/src/dist/commands.py | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/direct/src/dist/commands.py b/direct/src/dist/commands.py index 0a6beff5c5..44243ee8b7 100644 --- a/direct/src/dist/commands.py +++ b/direct/src/dist/commands.py @@ -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