Skip to content

Commit

Permalink
Move texture copying from Converter to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Moguri committed Dec 31, 2022
1 parent b746ed5 commit 27ef4ae
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
20 changes: 20 additions & 0 deletions gltf/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import argparse
import os
import shutil

import panda3d.core as p3d

Expand Down Expand Up @@ -98,9 +100,27 @@ def main():
gltf_data = parse_gltf_file(src)
converter.update(gltf_data)

os.makedirs(outdir, exist_ok=True)

if args.print_scene:
converter.active_scene.ls()

if args.textures == 'copy':
textures = [
texture
for scene in converter.scenes.values()
for texture in scene.find_all_textures()
if texture.filename
]

for texture in textures:
fname = texture.filename
texsrc = os.path.join(indir.to_os_specific(), fname)
texdst = os.path.join(outdir.to_os_specific(), fname)

os.makedirs(os.path.dirname(texdst), exist_ok=True)
shutil.copy(texsrc, texdst)

if args.animations == 'separate':
for bundlenode in converter.active_scene.find_all_matches('**/+AnimBundleNode'):
anim_name = bundlenode.node().bundle.name
Expand Down
13 changes: 2 additions & 11 deletions gltf/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import itertools
import os
import math
import shutil
import struct
import pprint # pylint: disable=unused-import

Expand Down Expand Up @@ -505,17 +504,9 @@ def load_embedded_image(name, ext, data):
texture = load_embedded_image(name, ext, data)
else:
uri = Filename.from_os_specific(uri)
uridir = self.indir
if self.settings.textures == 'copy':
uridir = self.outdir
src = os.path.join(self.indir.to_os_specific(), uri)
dst = os.path.join(self.outdir.to_os_specific(), uri)
outdir = os.path.dirname(dst)
os.makedirs(outdir, exist_ok=True)
shutil.copy(src, dst)
fulluri = Filename(uridir, uri)
fulluri = Filename(self.indir, uri)
texture = TexturePool.load_texture(fulluri, 0, False, LoaderOptions())
texture.filename = uri
texture.filename = texture.fullpath = uri
else:
name = source.get('name', '')
ext = source['mimeType'].split('/')[1]
Expand Down
14 changes: 14 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@ def test_cli_basic(modelroot, tmp_path):
])

assert os.path.exists(dst)


def test_cli_copy_textures(modelroot, tmp_path):
src = (modelroot / 'BoxTextured.gltf').to_os_specific()
dst = tmp_path / 'tmp.bam'
subprocess.check_call([
'gltf2bam',
'--textures', 'copy',
src,
dst,
])

assert os.path.exists(dst)
assert os.path.exists(tmp_path / 'CesiumLogoFlat.png')

0 comments on commit 27ef4ae

Please sign in to comment.