Skip to content

Commit

Permalink
Bundle shaders into a shader.py when building wheels
Browse files Browse the repository at this point in the history
simplepbr will automatically handle bundled vs non-bundled. The  main
benefit of this is that users no longer need to take special action to
copy shader files when making applications using Panda's build_apps.
  • Loading branch information
Moguri committed Mar 22, 2022
1 parent 6bba4c3 commit b55fadb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,6 @@ simplepbr expects the following textures are assigned to the following texture s

For an example application using `panda3d-simplepbr` check out the [viewer](https://github.com/Moguri/panda3d-gltf/blob/master/gltf/viewer.py) in the [panda3d-gltf repo](https://github.com/Moguri/panda3d-gltf).

## Distributing

When using Panda3D's `build_apps` the data files (i.e., shader files) will not be copied by default.
Options are being explored to make this more automatic, but for the time being, add the following to `setup.py`:

```python
setup(
# ...
'package_data_dirs': {
'simplepbr': [
('simplepbr/shaders*', '', {}),
],
}
# ...
)
```

## Running Tests

First install `panda3d-simplepbr` in editable mode along with `test` extras:
Expand Down
33 changes: 33 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import os
import shutil

from distutils.command.install import install
from distutils import log
import setuptools


class BundleShaders(setuptools.Command):
def initialize_options(self):
pass

def finalize_options(self):
pass

def run(self):
log.info('Bundling shaders')
installcmd = self.distribution.get_command_obj('install')
builddir = os.path.join(installcmd.root, 'simplepbr')
shaderdir = os.path.join(builddir, 'shaders')
shaders = {}
for shaderpath in os.listdir(shaderdir):
with open(os.path.join(shaderdir, shaderpath)) as shaderfile:
shaders[shaderpath] = shaderfile.read()
with open(os.path.join(builddir, 'shaders.py'), 'w') as shaderfile:
shaderfile.write(f'shaders={repr(shaders)}')
shutil.rmtree(shaderdir)

install.sub_commands.append(('bundle_shaders', None))

setuptools.setup(
cmdclass={'bundle_shaders': BundleShaders}
)
15 changes: 10 additions & 5 deletions simplepbr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

from .version import __version__

try:
from .shaders import shaders
except ImportError:
shaders = None


__all__ = [
'init',
Expand Down Expand Up @@ -40,13 +45,13 @@ def _add_shader_defines(shaderstr, defines):


def _load_shader_str(shaderpath, defines=None):
shader_dir = os.path.join(os.curdir, "shaders")

if not os.path.exists(shader_dir):
if shaders:
shaderstr = shaders[shaderpath]
else:
shader_dir = os.path.join(os.path.dirname(__file__), 'shaders')

with open(os.path.join(shader_dir, shaderpath)) as shaderfile:
shaderstr = shaderfile.read()
with open(os.path.join(shader_dir, shaderpath)) as shaderfile:
shaderstr = shaderfile.read()

if defines is None:
defines = {}
Expand Down

0 comments on commit b55fadb

Please sign in to comment.